Пример #1
0
        public void a_record_can_be_written()
        {
            var filename = Path.Combine(PathName, "prefix.tf0");

            File.WriteAllBytes(filename, new byte[10000]);
            _checkpoint = new InMemoryCheckpoint(137);
            var tf = new MultifileTransactionFileWriter(
                new TransactionFileDatabaseConfig(PathName, "prefix.tf", 100000, _checkpoint, new List <ICheckpoint>()));

            tf.Open();
            var record = new PrepareLogRecord(logPosition: 0,
                                              correlationId: _correlationId,
                                              eventId: _eventId,
                                              transactionPosition: 0,
                                              transactionOffset: 0,
                                              eventStreamId: "WorldEnding",
                                              expectedVersion: 1234,
                                              timeStamp: new DateTime(2012, 12, 21),
                                              flags: PrepareFlags.None,
                                              eventType: "type",
                                              data: new byte[8000],
                                              metadata: new byte[] { 7, 17 });
            long tmp;

            tf.Write(record, out tmp);
            tf.Close();
            Assert.AreEqual(record.GetSizeWithLengthPrefixAndSuffix() + 137, _checkpoint.Read());
            using (var filestream = File.Open(filename, FileMode.Open, FileAccess.Read))
            {
                filestream.Seek(137 + sizeof(int), SeekOrigin.Begin);
                var reader = new BinaryReader(filestream);
                var read   = LogRecord.ReadFrom(reader);
                Assert.AreEqual(record, read);
            }
        }
        public void a_record_can_be_written()
        {
            _checkpoint = new InMemoryCheckpoint(0);
            var tf = new MultifileTransactionFileWriter(
                new TransactionFileDatabaseConfig(PathName, "prefix.tf", 10000, _checkpoint, new List <ICheckpoint>()));

            tf.Open();
            var record = new PrepareLogRecord(logPosition: 0,
                                              correlationId: _correlationId,
                                              eventId: _eventId,
                                              transactionPosition: 0,
                                              eventStreamId: "WorldEnding",
                                              expectedVersion: 1234,
                                              timeStamp: new DateTime(2012, 12, 21),
                                              flags: PrepareFlags.None,
                                              eventType: "type",
                                              data: new byte[] { 1, 2, 3, 4, 5 },
                                              metadata: new byte[] { 7, 17 });
            long tmp;

            tf.Write(record, out tmp);
            tf.Close();
            Assert.AreEqual(record.GetSizeWithLengthPrefix(), _checkpoint.Read());
            using (var filestream = File.Open(Path.Combine(PathName, "prefix.tf0"), FileMode.Open, FileAccess.Read))
            {
                var reader = new BinaryReader(filestream);
                reader.ReadInt32();
                var read = LogRecord.ReadFrom(reader);
                Assert.AreEqual(record, read);
            }
        }
        public void try_read_does_not_cache_anything_and_returns_record_once_it_is_written_later()
        {
            var writerchk = new InMemoryCheckpoint(0);
            var config    = new TransactionFileDatabaseConfig(PathName, "prefix.tf", 10000, writerchk, new ICheckpoint[0]);

            // create db
            var writer = new MultifileTransactionFileWriter(config);

            writer.Open();

            var reader = new MultifileTransactionFileChaser(config);

            reader.Open();
            LogRecord record;

            Assert.IsFalse(reader.TryReadNext(out record));

            var  rec = LogRecord.SingleWrite(0, Guid.NewGuid(), Guid.NewGuid(), "ES", -1, "ET", new byte[] { 7 }, null);
            long tmp;

            Assert.IsTrue(writer.Write(rec, out tmp));
            writer.Flush();
            writer.Close();

            Assert.IsTrue(reader.TryReadNext(out record));
            Assert.AreEqual(rec, record);

            reader.Close();
        }
        public void a_record_can_be_written()
        {
            var filename       = Path.Combine(PathName, "prefix.tf0");
            var secondfilename = Path.Combine(PathName, "prefix.tf1");
            var thirdfilename  = Path.Combine(PathName, "prefix.tf2");

            File.WriteAllBytes(filename, new byte[50]);
            _checkpoint = new InMemoryCheckpoint(40);
            var tf = new MultifileTransactionFileWriter(
                new TransactionFileDatabaseConfig(PathName, "prefix.tf", 50, _checkpoint, new ICheckpoint[0]));

            tf.Open();
            var record = new PrepareLogRecord(logPosition: 0,
                                              correlationId: _correlationId,
                                              eventId: _eventId,
                                              transactionPosition: 0,
                                              transactionOffset: 0,
                                              eventStreamId: "WorldEnding",
                                              expectedVersion: 1234,
                                              timeStamp: new DateTime(2012, 12, 21),
                                              flags: PrepareFlags.None,
                                              eventType: "type",
                                              data: new byte[] { 1, 2, 3, 4, 5 },
                                              metadata: new byte[] { 7, 17 });
            long tmp;

            tf.Write(record, out tmp);
            tf.Close();
            Assert.AreEqual(record.GetSizeWithLengthPrefixAndSuffix() + 40, _checkpoint.Read()); //+orginal checkpoint position
            Assert.IsTrue(File.Exists(secondfilename));
            var stream = new MemoryStream();
            var buffer = new byte[256];

            using (var filestream = File.Open(filename, FileMode.Open, FileAccess.Read))
            {
                filestream.Seek(40, SeekOrigin.Begin);
                filestream.Read(buffer, 0, 10);
                stream.Write(buffer, 0, 10);
            }
            using (var filestream = File.Open(secondfilename, FileMode.Open, FileAccess.Read))
            {
                filestream.Seek(0, SeekOrigin.Begin);
                filestream.Read(buffer, 0, 50);
                stream.Write(buffer, 0, 50);
            }
            using (var filestream = File.Open(thirdfilename, FileMode.Open, FileAccess.Read))
            {
                filestream.Seek(0, SeekOrigin.Begin);
                filestream.Read(buffer, 0, 50);
                stream.Write(buffer, 0, 50);
            }
            stream.Seek(0 + sizeof(int), SeekOrigin.Begin);
            var reader = new BinaryReader(stream);
            var read   = LogRecord.ReadFrom(reader);

            Assert.AreEqual(record, read);
        }
 public void with_a_writer_checksum_of_zero_the_first_chunk_is_created_with_correct_name()
 {
     var tf = new MultifileTransactionFileWriter(
         new TransactionFileDatabaseConfig(PathName, "prefix.tf", 10000, new InMemoryCheckpoint(0), new ICheckpoint[0]));
     tf.Open();
     Assert.AreEqual(1, Directory.GetFiles(PathName).Length);
     Assert.IsTrue(File.Exists(Path.Combine(PathName, "prefix.tf0")));
     Assert.AreEqual(10000, new FileInfo(Path.Combine(PathName, "prefix.tf0")).Length);
     tf.Close();
 }
        public void try_read_returns_false_when_writer_checksum_is_zero()
        {
            var writerchk = new InMemoryCheckpoint(0);
            var config    = new TransactionFileDatabaseConfig(PathName, "prefix.tf", 10000, writerchk, new ICheckpoint[0]);

            // create db
            var writer = new MultifileTransactionFileWriter(config);

            writer.Open();
            writer.Close();

            var reader = new MultifileTransactionFileChaser(config);

            reader.Open();
            LogRecord record;

            Assert.IsFalse(reader.TryReadNext(out record));
            reader.Close();
        }