Exemple #1
0
        public void SurvivesListVsIList()
        {
            var manager = new EventStoreManager();

            manager.SetNewTypeNameMapper(new OverloadableTypeMapper(typeof(SomethingWithoutList), "Some"));
            var file     = new MemoryEventFileStorage();
            var file2    = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);

            var ev = new SomethingWithList();

            appender.Store(null, new object[] { ev });
            var ev2 = new SomethingWithNestedIList
            {
                B = new Dictionary <ulong, IList <string> > {
                    { 1, new List <string> {
                          "a1"
                      } }
                }
            };

            appender.Store(null, new object[] { ev2 });
            var appender2 = manager.AppendToStore(file);

            appender2.Store(null, new object[] { ev2 });
            manager = new EventStoreManager();
            manager.SetNewTypeNameMapper(new OverloadableTypeMapper(typeof(SomethingWithList), "Some"));
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            reader = manager.OpenReadOnlyStore(file2);
            reader.ReadFromStartToEnd(eventObserver);
        }
Exemple #2
0
        public void UpgradeToLessObjectProperties()
        {
            var manager = new EventStoreManager();

            manager.SetNewTypeNameMapper(new OverloadableTypeMapper(typeof(UserEvent), "UserEvent"));
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);
            var user     = new User {
                Name = "A", Age = 1
            };
            var userEvent = new UserEvent {
                Id = 10, User1 = user, User2 = user
            };

            appender.Store(null, new object[] { userEvent, new User {
                                                    Name = "B"
                                                } });

            manager = new EventStoreManager();
            manager.SetNewTypeNameMapper(new OverloadableTypeMapper(typeof(UserEventLess), "UserEvent"));
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            var readUserEvent = (UserEventLess)eventObserver.Events[0][0];

            Assert.Equal("A", readUserEvent.User2.Name);
            Assert.Equal(10, readUserEvent.Id);
            Assert.Equal("B", ((User)eventObserver.Events[0][1]).Name);
        }
        static object PassThroughEventStorage(object @event, ITypeNameMapper mapper)
        {
            var options = TypeSerializersOptions.Default;

            options.SymmetricCipher = new AesGcmSymmetricCipher(new byte[]
            {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
                28, 29, 30, 31
            });
            var manager  = new EventStoreManager(options);
            var storage  = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(storage);
            var events   = new[]
            {
                @event
            };

            appender.Store(null, events);
            manager = new EventStoreManager(options);
            manager.SetNewTypeNameMapper(mapper);
            var eventObserver = new EventStoreTest.StoringEventObserver();

            manager.OpenReadOnlyStore(storage).ReadFromStartToEnd(eventObserver);
            return(eventObserver.Events[0][0]);
        }
Exemple #4
0
        public void SupportsList()
        {
            var manager  = new EventStoreManager();
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);
            var userA    = new User {
                Name = "A", Age = 1
            };
            var userB = new User {
                Name = "B", Age = 2
            };
            var userEvent = new UserEventList {
                Id = 10, List = new List <User> {
                    userA, userB, userA
                }
            };

            appender.Store(null, new object[] { userEvent });

            manager = new EventStoreManager();
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            var readUserEvent = (UserEventList)eventObserver.Events[0][0];

            Assert.Equal(readUserEvent, userEvent);
        }
Exemple #5
0
        public void CanReadLongerEventsFromIncompleteFile()
        {
            var manager = new EventStoreManager();

            manager.CompressionStrategy = new NoCompressionStrategy();
            var file1    = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file1);

            appender.Store(null, new object[] { new byte[8000] });
            var file2 = new MemoryEventFileStorage();
            var buf   = ByteBuffer.NewSync(new byte[4096]);

            file1.Read(buf, 0);
            file2.Write(buf, 0);
            var reader = manager.OpenReadOnlyStore(file2);

            reader.ReadFromStartToEnd(new SkippingEventObserver());
            Assert.False(reader.IsKnownAsCorrupted());
            Assert.False(reader.IsKnownAsAppendable());
            Assert.False(reader.IsKnownAsFinished());
            buf = ByteBuffer.NewSync(new byte[4096]);
            file1.Read(buf, 4096);
            file2.Write(buf, 4096);
            reader.ReadToEnd(new SkippingEventObserver());
            Assert.False(reader.IsKnownAsCorrupted());
            Assert.True(reader.IsKnownAsAppendable());
            Assert.False(reader.IsKnownAsFinished());
        }
Exemple #6
0
        public void TypeMapperCanForceSkipEvents()
        {
            var manager = new EventStoreManager();

            manager.SetNewTypeNameMapper(new FullNameTypeMapper());
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);
            var user     = new User {
                Name = "ABC", Age = 88
            };
            var userEvent = new UserEvent {
                Id = 10, User1 = user, User2 = user
            };
            var userEventMore = new UserEventMore {
                Id = 11, User1 = user, User2 = user
            };

            appender.Store(null, new object[] { userEvent, userEventMore });

            manager = new EventStoreManager();
            manager.SetNewTypeNameMapper(new SelectiveTypeMapper("BTDBTest.EventStoreTest+UserEvent"));
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new SimpleEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            Assert.Single(eventObserver.Events[0]);
            var readUserEvent = (UserEventMore)eventObserver.Events[0][0];

            Assert.Same(readUserEvent.User1, readUserEvent.User2);
        }
Exemple #7
0
        public void SupportsDictionary()
        {
            var manager  = new EventStoreManager();
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);
            var userA    = new User {
                Name = "A", Age = 1
            };
            var userB = new User {
                Name = "B", Age = 2
            };
            var userEvent = new UserEventDictionary {
                Id = 10, Dict = new Dictionary <string, User> {
                    { "A", userA }, { "B", userB }
                }
            };

            appender.Store(null, new object[] { userEvent });

            manager = new EventStoreManager();
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            var readUserEvent = (UserEventDictionary)eventObserver.Events[0][0];

            Assert.Equal(readUserEvent, userEvent);
        }
Exemple #8
0
        public void CustomEventIsReadFromSecondSplit()
        {
            var manager = new EventStoreManager();

            manager.CompressionStrategy = new NoCompressionStrategy();
            manager.SetNewTypeNameMapper(new SimplePersonTypeMapper());
            var appender = manager.AppendToStore(new MemoryEventFileStorage(4096, 4096));
            var first    = appender.CurrentFileStorage;
            var user     = new User {
                Name = "A", Age = 1
            };

            while (appender.CurrentFileStorage == first)
            {
                appender.Store(null, new object[] { user });
            }
            var second = appender.CurrentFileStorage;

            manager = new EventStoreManager();
            manager.CompressionStrategy = new NoCompressionStrategy();
            manager.SetNewTypeNameMapper(new SimplePersonTypeMapper());
            var reader        = manager.OpenReadOnlyStore(second);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            Assert.Equal(new object[] { null }, eventObserver.Metadata);
            Assert.Equal(new[] { new object[] { user } }, eventObserver.Events);
        }
Exemple #9
0
        public void CanStopReadBatchesAfterFirst()
        {
            var manager  = new EventStoreManager();
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);
            var metadata = new User {
                Name = "A", Age = 1
            };
            var events = new object[]
            {
                new User {
                    Name = "B", Age = 2
                },
                new User {
                    Name = "C", Age = 3
                }
            };

            appender.Store(metadata, events);
            appender.Store(metadata, events);

            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserverWithStop();

            reader.ReadFromStartToEnd(eventObserver);
            Assert.False(reader.IsKnownAsCorrupted());
            Assert.False(reader.IsKnownAsFinished());
            Assert.False(reader.IsKnownAsAppendable());
            Assert.Equal(new List <object> {
                metadata
            }, eventObserver.Metadata);
            Assert.Equal(new[] { events }, eventObserver.Events);
        }
Exemple #10
0
        public void Run()
        {
            var manager = new EventStoreManager();

            //manager.CompressionStrategy = new NoCompressionStrategy();
            using (var stream = new FileStream("0.event", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 1))
            {
                var file = new StreamEventFileStorage(stream);
                _writeStore = manager.AppendToStore(file);
                var consumerTask = Task.Factory.StartNew(EventConsumer, TaskCreationOptions.LongRunning | TaskCreationOptions.HideScheduler);
                _sw.Start();
                var tasks = new Task[ParallelTasks];
                Parallel.For(0, tasks.Length, i =>
                {
                    tasks[i] = PublishSampleEvents(RepetitionCount);
                });
                Task.WaitAll(tasks);
                _bc.CompleteAdding();
                consumerTask.Wait();
                _sw.Stop();
                Console.WriteLine("Write {0}ms events per second:{1:f0} total len:{2}", _sw.ElapsedMilliseconds, tasks.Length * RepetitionCount / _sw.Elapsed.TotalSeconds, stream.Length);
                _sw.Restart();
                var allObserverCounter = new AllObserverCounter();
                manager.OpenReadOnlyStore(file).ReadFromStartToEnd(allObserverCounter);
                _sw.Stop();
                Console.WriteLine("Read {0}ms events per second:{1:f0} events:{2}", _sw.ElapsedMilliseconds, allObserverCounter.Count / _sw.Elapsed.TotalSeconds, allObserverCounter.Count);
            }
        }
Exemple #11
0
        public void SkipListOnUpgrade()
        {
            var manager = new EventStoreManager();

            manager.SetNewTypeNameMapper(new OverloadableTypeMapper(typeof(UserEventList), "UserEvent"));
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);
            var userA    = new User {
                Name = "A", Age = 1
            };
            var userB = new User {
                Name = "B", Age = 2
            };
            var userEvent = new UserEventList {
                Id = 10, List = new List <User> {
                    userA, userB, userA
                }
            };

            appender.Store(null, new object[] { userEvent });

            manager = new EventStoreManager();
            manager.SetNewTypeNameMapper(new OverloadableTypeMapper(typeof(UserEvent), "UserEvent"));
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            var readUserEvent = (UserEvent)eventObserver.Events[0][0];

            Assert.Equal(10, readUserEvent.Id);
            Assert.Null(readUserEvent.User1);
        }
        public void WhenReadOnlyStoreIsCreatedFromNewEventStoreManagerThenItShouldNotLeakMemory()
        {
            var storage = new MemoryEventFileStorage();

            var manager  = new EventStoreManager();
            var appender = manager.AppendToStore(storage);
            var metadata = new User {
                Name = "A", Age = 1
            };
            var events = new object[]
            {
                new User {
                    Name = "B", Age = 2
                },
                new User {
                    Name = "C", Age = 3
                }
            };

            appender.Store(metadata, events);
            appender.FinalizeStore();

            manager = new EventStoreManager();
            var  eventObserver  = new StoringEventObserver();
            long baselineMemory = 0;

            for (int i = 0; i <= 10000; i++)
            {
                var reader = manager.OpenReadOnlyStore(storage);
                reader.ReadToEnd(eventObserver);

                Assert.Single(eventObserver.Events);
                eventObserver.Events.Clear();
                eventObserver.Metadata.Clear();

                if (i == 10)
                {
                    GC.Collect(2);
                    GC.WaitForPendingFinalizers();
                    GC.Collect(2);
                    baselineMemory = GC.GetTotalMemory(false);
                }
            }

            GC.Collect(2);
            GC.WaitForPendingFinalizers();
            GC.Collect(2);
            Assert.InRange(GC.GetTotalMemory(false), 0, baselineMemory * 3F);
        }
Exemple #13
0
        public void CompressionShortensData()
        {
            var manager          = new EventStoreManager();
            var file             = new MemoryEventFileStorage();
            var appender         = manager.AppendToStore(file);
            var compressibleData = new byte[20000];

            appender.Store(null, new object[] { compressibleData });
            Assert.True(2000 > appender.KnownAppendablePosition());

            manager = new EventStoreManager();
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            Assert.Equal(new object[] { null }, eventObserver.Metadata);
            Assert.Equal(new[] { new object[] { compressibleData } }, eventObserver.Events);
        }
        static object PassThroughEventStorage(object @event, ITypeNameMapper mapper)
        {
            var manager  = new EventStoreManager();
            var storage  = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(storage);
            var events   = new[]
            {
                @event
            };

            appender.Store(null, events);
            manager = new EventStoreManager();
            manager.SetNewTypeNameMapper(mapper);
            var eventObserver = new EventStoreTest.StoringEventObserver();

            manager.OpenReadOnlyStore(storage).ReadFromStartToEnd(eventObserver);
            return(eventObserver.Events[0][0]);
        }
Exemple #15
0
        public void SupportsDataOverMaxBlockSize()
        {
            var manager    = new EventStoreManager();
            var file       = new MemoryEventFileStorage();
            var appender   = manager.AppendToStore(file);
            var randomData = new byte[20000];

            new Random().NextBytes(randomData);
            appender.Store(null, new object[] { randomData });
            Assert.True(10000 < appender.KnownAppendablePosition());

            manager = new EventStoreManager();
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            Assert.Equal(new object[] { null }, eventObserver.Metadata);
            Assert.Equal(new[] { new object[] { randomData } }, eventObserver.Events);
        }
Exemple #16
0
        public void SkipsIIndirect()
        {
            var manager  = new EventStoreManager();
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);

            var ev = new EventWithIIndirect
            {
                Name = "A",
                Ind1 = new DBIndirect <User>(),
                Ind2 = new List <IIndirect <User> >()
            };

            appender.Store(null, new object[] { ev });
            manager = new EventStoreManager();
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
        }
Exemple #17
0
        public void CanFinalizeEventStoreAfterReadFromStart()
        {
            var manager = new EventStoreManager();

            manager.CompressionStrategy = new NoCompressionStrategy();
            var file1    = new MemoryEventFileStorage(4096, 4096);
            var appender = manager.AppendToStore(file1);

            appender.Store(null, new object[] { new byte[4000] });
            appender.Store(null, new object[] { new byte[4000] });
            Assert.NotSame(file1, appender.CurrentFileStorage);
            var reader = manager.OpenReadOnlyStore(file1);

            reader.ReadFromStartToEnd(new SkippingEventObserver());
            Assert.False(reader.IsKnownAsAppendable());
            Assert.False(reader.IsKnownAsCorrupted());
            Assert.True(reader.IsKnownAsFinished());
            Assert.True(appender.IsKnownAsAppendable());
            Assert.False(appender.IsKnownAsCorrupted());
            Assert.False(appender.IsKnownAsFinished());
        }
Exemple #18
0
        public void NestedObjectsTest()
        {
            var manager   = new EventStoreManager();
            var file      = new MemoryEventFileStorage();
            var appender  = manager.AppendToStore(file);
            var userEvent = new UserEvent {
                Id = 10, User1 = new User {
                    Name = "A", Age = 1
                }
            };

            appender.Store(null, new object[] { userEvent });

            manager = new EventStoreManager();
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            Assert.Equal(new object[] { null }, eventObserver.Metadata);
            Assert.Equal(new[] { new object[] { userEvent } }, eventObserver.Events);
        }
Exemple #19
0
        public void CanWriteSimpleEventAndReadItIndependently()
        {
            var manager = new EventStoreManager();

            manager.SetNewTypeNameMapper(new SimplePersonTypeMapper());
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);
            var user     = new User {
                Name = "A", Age = 1
            };

            appender.Store(null, new object[] { user });

            manager = new EventStoreManager();
            manager.SetNewTypeNameMapper(new SimplePersonTypeMapper());
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            Assert.Equal(new object[] { null }, eventObserver.Metadata);
            Assert.Equal(new[] { new object[] { user } }, eventObserver.Events);
        }
Exemple #20
0
        public void UpgradeToDifferentEnumProperties()
        {
            var manager = new EventStoreManager();

            manager.SetNewTypeNameMapper(new OverloadableTypeMapper(typeof(ApplicationInfo), "ApplicationInfo"));
            var file            = new MemoryEventFileStorage();
            var appender        = manager.AppendToStore(file);
            var applicationInfo = new ApplicationInfo {
                Type = ApplicationsType.First
            };

            appender.Store(null, new object[] { applicationInfo });

            manager = new EventStoreManager();
            manager.SetNewTypeNameMapper(new OverloadableTypeMapper(typeof(ApplicationInfoPropertyEnumTypeChanged), "ApplicationInfo"));
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            var readApplicationInfo = (ApplicationInfoPropertyEnumTypeChanged)eventObserver.Events[0][0];

            Assert.Equal(readApplicationInfo.Type, ApplicationsRenamedType.First);
        }
Exemple #21
0
        public void SupportsDictionaryInDictionary()
        {
            var manager  = new EventStoreManager();
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);

            var dictInDictEvent = new EventDictionaryInDictionary
            {
                DictInDict = new Dictionary <string, IDictionary <string, string> >
                {
                    { "level-A", new Dictionary <string, string> {
                          { "level-B", "level-C" }
                      } }
                }
            };

            appender.Store(null, new object[] { dictInDictEvent });
            manager = new EventStoreManager();
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
        }
Exemple #22
0
        public void SameReferenceTest()
        {
            var manager  = new EventStoreManager();
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);
            var user     = new User {
                Name = "A", Age = 1
            };
            var userEvent = new UserEvent {
                Id = 10, User1 = user, User2 = user
            };

            appender.Store(null, new object[] { userEvent });

            manager = new EventStoreManager();
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            var readUserEvent = (UserEvent)eventObserver.Events[0][0];

            Assert.Same(readUserEvent.User1, readUserEvent.User2);
        }
Exemple #23
0
        public void Run()
        {
            _ring            = new RingBuffer <ValueEvent>(() => new ValueEvent(), new MultiThreadedClaimStrategy(2048), new YieldingWaitStrategy());
            _sequenceBarrier = _ring.NewBarrier();
            _sequence        = new Sequence(Sequencer.InitialCursorValue);
            _ring.SetGatingSequences(_sequence);
            var manager = new EventStoreManager();

            //manager.CompressionStrategy = new NoCompressionStrategy();
            using (var stream = new FileStream("0.event", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 1))
            {
                var file = new StreamEventFileStorage(stream);
                _writeStore = manager.AppendToStore(file);
                var consumerTask = Task.Factory.StartNew(EventConsumer,
                                                         TaskCreationOptions.LongRunning |
                                                         TaskCreationOptions.HideScheduler);
                _sw.Start();
                var tasks = new Task[ParallelTasks];
                Parallel.For(0, tasks.Length, i =>
                {
                    tasks[i] = PublishSampleEvents(RepetitionCount);
                });
                Task.WaitAll(tasks);
                _sequenceBarrier.Alert();
                consumerTask.Wait();
                _sw.Stop();
                Console.WriteLine("Write {0}ms events per second:{1:f0} total len:{2}", _sw.ElapsedMilliseconds,
                                  tasks.Length * RepetitionCount / _sw.Elapsed.TotalSeconds, stream.Length);
                _sw.Restart();
                var allObserverCounter = new AllObserverCounter();
                manager.OpenReadOnlyStore(file).ReadFromStartToEnd(allObserverCounter);
                _sw.Stop();
                Console.WriteLine("Read {0}ms events per second:{1:f0} events:{2}", _sw.ElapsedMilliseconds,
                                  allObserverCounter.Count / _sw.Elapsed.TotalSeconds, allObserverCounter.Count);
            }
        }
Exemple #24
0
        public void DeserializeErrorInfoWorks()
        {
            var manager  = new EventStoreManager();
            var file     = new MemoryEventFileStorage();
            var appender = manager.AppendToStore(file);
            var obj      = new ErrorInfo();

            obj.PropertyErrors = new Dictionary <string, IList <ErrorInfo> >();
            var items = obj.PropertyErrors;

            items["a"] = new List <ErrorInfo> {
                new ErrorInfo()
            };
            appender.Store(null, new object[] { obj });

            manager = new EventStoreManager();
            var reader        = manager.OpenReadOnlyStore(file);
            var eventObserver = new StoringEventObserver();

            reader.ReadFromStartToEnd(eventObserver);
            var readUserEvent = (ErrorInfo)eventObserver.Events[0][0];

            Assert.Equal(1, readUserEvent.PropertyErrors.Count);
        }