public void creates_new_root_partition_on_first_epoch()
        {
            var reader = new FakeReader(withoutRecords: true);
            var writer = new FakeWriter();

            IPartitionManager partitionManager = new PartitionManager(reader, writer, new LogV3RecordFactory());

            partitionManager.Initialize();

            Assert.Collection(writer.WrittenRecords,
                              r => {
                Assert.Equal(LogRecordType.PartitionType, r.RecordType);
                Assert.IsType <PartitionTypeLogRecord>(r);
                Assert.Equal("Root", ((PartitionTypeLogRecord)r).Record.StringPayload);
                Assert.Equal(partitionManager.RootTypeId, ((PartitionTypeLogRecord)r).Record.Header.RecordId);
                Assert.Equal(Guid.Empty, ((PartitionTypeLogRecord)r).Record.SubHeader.PartitionId);
            },
                              r => {
                Assert.Equal(LogRecordType.Partition, r.RecordType);
                Assert.IsType <PartitionLogRecord>(r);
                Assert.Equal("Root", ((PartitionLogRecord)r).Record.StringPayload);
                Assert.Equal(partitionManager.RootId, ((PartitionLogRecord)r).Record.Header.RecordId);
                Assert.Equal(partitionManager.RootTypeId, ((PartitionLogRecord)r).Record.SubHeader.PartitionTypeId);
                Assert.Equal(Guid.Empty, ((PartitionLogRecord)r).Record.SubHeader.ParentPartitionId);
            });

            Assert.True(writer.IsFlushed);
        }
        public void throws_on_unexpected_system_log_record_type()
        {
            var reader = new FakeReader(UnexpectedSystemLogRecord);

            IPartitionManager partitionManager = new PartitionManager(reader, new FakeWriter(), new LogV3RecordFactory());

            Assert.Throws <ArgumentOutOfRangeException>(() => partitionManager.Initialize());
        }
        public void reads_root_partition_only_once()
        {
            var reader = new FakeReader();
            var writer = new FakeWriter();

            IPartitionManager partitionManager = new PartitionManager(reader, writer, new LogV3RecordFactory());

            partitionManager.Initialize();
            partitionManager.Initialize();

            Assert.Empty(writer.WrittenRecords);
            Assert.Equal(2, reader.ReadCount);
        }
        public void configures_record_factory_with_root_partition_id()
        {
            var reader        = new FakeReader();
            var recordFactory = new LogV3RecordFactory();

            IPartitionManager partitionManager = new PartitionManager(reader, new FakeWriter(), recordFactory);

            partitionManager.Initialize();

            var streamRecord = (LogV3StreamRecord)recordFactory.CreateStreamRecord(Guid.NewGuid(), 1, DateTime.UtcNow, 1, "stream-1");

            Assert.Equal(partitionManager.RootId, streamRecord.Record.SubHeader.PartitionId);
        }
        public void reads_root_partition_once_initialized()
        {
            var rootPartitionId     = Guid.NewGuid();
            var rootPartitionTypeId = Guid.NewGuid();
            var reader = new FakeReader(rootPartitionId, rootPartitionTypeId);
            var writer = new FakeWriter();

            IPartitionManager partitionManager = new PartitionManager(reader, writer, new LogV3RecordFactory());

            partitionManager.Initialize();

            Assert.Empty(writer.WrittenRecords);
            Assert.Equal(rootPartitionId, partitionManager.RootId);
            Assert.Equal(rootPartitionTypeId, partitionManager.RootTypeId);
        }
        public void creates_root_partition_in_case_it_partially_failed_previously()
        {
            var rootPartitionTypeId = Guid.NewGuid();
            var reader = new FakeReader(rootPartitionId: null, rootPartitionTypeId);
            var writer = new FakeWriter();

            IPartitionManager partitionManager = new PartitionManager(reader, writer, new LogV3RecordFactory());

            partitionManager.Initialize();

            Assert.True(partitionManager.RootId.HasValue);
            Assert.Equal(rootPartitionTypeId, partitionManager.RootTypeId);
            Assert.Collection(writer.WrittenRecords,
                              r => {
                Assert.Equal(LogRecordType.Partition, r.RecordType);
                Assert.IsType <PartitionLogRecord>(r);
                Assert.Equal("Root", ((PartitionLogRecord)r).Record.StringPayload);
                Assert.Equal(partitionManager.RootId, ((PartitionLogRecord)r).Record.Header.RecordId);
                Assert.Equal(partitionManager.RootTypeId, ((PartitionLogRecord)r).Record.SubHeader.PartitionTypeId);
                Assert.Equal(Guid.Empty, ((PartitionLogRecord)r).Record.SubHeader.ParentPartitionId);
            });
        }