Example #1
0
        public void ItWorksWithSeveralViewsAndDeletionOfRemovedChildren()
        {
            var root1 = "B8FC0210-B7E4-4279-8BC8-53F13A557751";
            var root2 = "394C50E4-245A-4C35-829D-B4625EDC59F4";

            _context.Save(root1, new Event {
                NumberOfChildren = 30
            });
            _context.Save(root2, new Event {
                NumberOfChildren = 20
            });

            _context.Save(root1, new Event {
                NumberOfChildren = 1
            });
            _context.Save(root2, new Event {
                NumberOfChildren = 2
            });

            var view1 = _factory.Load <ViewRoot>(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId(root1));
            var view2 = _factory.Load <ViewRoot>(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId(root2));

            Assert.That(view1.Children.Count, Is.EqualTo(1));
            Assert.That(view2.Children.Count, Is.EqualTo(2));
        }
Example #2
0
        public void BasicDispatchOfSomeEvents()
        {
            var allPotatoesView            = new MongoDbViewManager <AllPotatoesView>(_mongoDatabase);
            var potatoTimeToBeConsumedView = new MongoDbViewManager <PotatoTimeToBeConsumedView>(_mongoDatabase);

            _dispatcher.AddViewManager(allPotatoesView);
            _dispatcher.AddViewManager(potatoTimeToBeConsumedView);

            // act
            var firstPointInTime = new DateTime(1979, 3, 1, 12, 0, 0, DateTimeKind.Utc);

            TimeMachine.FixCurrentTimeTo(firstPointInTime);
            _commandProcessor.ProcessCommand(new BitePotato("potato1", 0.5m));
            _commandProcessor.ProcessCommand(new BitePotato("potato2", 0.3m));
            _commandProcessor.ProcessCommand(new BitePotato("potato2", 0.3m));
            _commandProcessor.ProcessCommand(new BitePotato("potato3", 0.3m));

            var nextPointInTime = new DateTime(1981, 6, 9, 12, 0, 0, DateTimeKind.Utc);

            TimeMachine.FixCurrentTimeTo(nextPointInTime);
            _commandProcessor.ProcessCommand(new BitePotato("potato1", 0.5m));
            _commandProcessor.ProcessCommand(new BitePotato("potato2", 0.5m));

            var lastPointInTime = new DateTime(1981, 6, 9, 12, 0, 0, DateTimeKind.Utc);

            _commandProcessor.ProcessCommand(new BitePotato("potato3", 0.8m));

            Thread.Sleep(1000);

            // assert
            var allPotatoes = allPotatoesView.Load(GlobalInstanceLocator.GetViewInstanceId());

            Assert.That(allPotatoes, Is.Not.Null);

            var potato1View = potatoTimeToBeConsumedView.Load(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId("potato1"));
            var potato2View = potatoTimeToBeConsumedView.Load(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId("potato2"));
            var potato3View = potatoTimeToBeConsumedView.Load(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId("potato3"));

            Assert.That(potato1View, Is.Not.Null);
            Assert.That(potato2View, Is.Not.Null);
            Assert.That(potato3View, Is.Not.Null);

            Assert.That(allPotatoes.NamesOfPotatoes.Count, Is.EqualTo(3));
            Assert.That(allPotatoes.NamesOfPotatoes["potato1"], Is.EqualTo("Jeff"));
            Assert.That(allPotatoes.NamesOfPotatoes["potato2"], Is.EqualTo("Bunny"));
            Assert.That(allPotatoes.NamesOfPotatoes["potato3"], Is.EqualTo("Walter"));

            Assert.That(potato1View.Name, Is.EqualTo("Jeff"));
            Assert.That(potato1View.TimeOfCreation.ToUniversalTime(), Is.EqualTo(firstPointInTime));
            Assert.That(potato1View.TimeToBeEaten, Is.EqualTo(nextPointInTime - firstPointInTime));

            Assert.That(potato2View.Name, Is.EqualTo("Bunny"));
            Assert.That(potato2View.TimeOfCreation.ToUniversalTime(), Is.EqualTo(firstPointInTime));
            Assert.That(potato2View.TimeToBeEaten, Is.EqualTo(nextPointInTime - firstPointInTime));

            Assert.That(potato3View.Name, Is.EqualTo("Walter"));
            Assert.That(potato3View.TimeOfCreation.ToUniversalTime(), Is.EqualTo(firstPointInTime));
            Assert.That(potato3View.TimeToBeEaten, Is.EqualTo(lastPointInTime - firstPointInTime));
        }
Example #3
0
        public void WorksWithChildren()
        {
            var root1 = "B8FC0210-B7E4-4279-8BC8-53F13A557751";

            _context.Save(root1, new Event {
                NumberOfChildren = 3
            });

            var view = _factory.Load <ViewRoot>(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId(root1));

            Assert.That(view.Children.Count, Is.EqualTo(3));
        }
Example #4
0
        public void VerifyDataTypes()
        {
            _viewManager.Dispatch(new ThrowingViewContext(), new DomainEvent[] { GetAnEvent("key") }, new NullProfiler());

            var view = _viewManager.Load(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId("key"));

            Assert.That(view, Is.Not.Null, "View was not properly generated");
            Assert.That(view.NullString, Is.Null);
            Assert.That(view.NullInt, Is.Null);
            Assert.That(view.Boolooleananana, Is.EqualTo(true));
            Assert.That(view.MaybeBoolooleananana, Is.EqualTo(true));
            Assert.That(view.String, Is.EqualTo("a string"));
            Assert.That(view.Guid, Is.EqualTo(SomeKnownGuid));
            Assert.That(view.NullableGuid, Is.EqualTo(SomeKnownGuid));
            Assert.That(view.NullableGuidWithNullValue, Is.EqualTo(null));
            Assert.That(view.Integer, Is.EqualTo(2));
            Assert.That(view.Shorty, Is.EqualTo(3));
            Assert.That(view.Long, Is.EqualTo(4));
            Assert.That(view.Double, Is.EqualTo(2.3));
            Assert.That(view.Decimal, Is.EqualTo(2.4m));
            Assert.That(view.Float, Is.EqualTo(1.2f));
            Assert.That(view.ListOfString, Is.EqualTo(new List <string> {
                "hello", "there"
            }));
            Assert.That(view.ListOfInt, Is.EqualTo(new List <int> {
                6, 7
            }));
            Assert.That(view.ListOfDouble, Is.EqualTo(new List <double> {
                6, 7
            }));
            Assert.That(view.ListOfDecimal, Is.EqualTo(new List <decimal> {
                6, 7
            }));
            Assert.That(view.HashOfStrings, Is.EqualTo(new HashSet <string> {
                "bim", "bom"
            }));
            Assert.That(view.HashOfInts, Is.EqualTo(new HashSet <int> {
                9, 3
            }));
            Assert.That(view.ArrayOfStrings, Is.EqualTo(new[] { "hej", "med", "dig", "min", "ven" }));

            Assert.That(view.DateTime, Is.EqualTo(new DateTime(1979, 3, 19, 13, 00, 00, DateTimeKind.Utc)));
            Assert.That(view.DateTime.Kind, Is.EqualTo(DateTimeKind.Utc));

            Assert.That(view.DateTimeOffset, Is.EqualTo(new DateTimeOffset(1979, 3, 19, 14, 00, 00, TimeSpan.FromHours(1))));

            Assert.That(view.TimeSpan, Is.EqualTo(new TimeSpan(2, 15, 20)));

            Assert.That(string.Join(" ", view.JustSomethingComplex.Children.Select(c => c.Message)), Is.EqualTo("oh my god"));
            Assert.That(string.Join(" ", view.ListOfComplexThings.SelectMany(t => t.Children.Select(c => c.Message))), Is.EqualTo("oh my god woota da f00k"));
        }
        public void CanAccessAggregateRootInView()
        {
            _eventDispatcher.AddViewManager(_viewManager1);

            _cirqus.ProcessCommand(new MyCommand("rootid"));
            _cirqus.ProcessCommand(new MyCommand("rootid"));
            _cirqus.ProcessCommand(new MyCommand("rootid"));
            _cirqus.ProcessCommand(new MyCommand("rootid"));
            var lastResult = _cirqus.ProcessCommand(new MyCommand("rootid"));

            _eventDispatcher.WaitUntilProcessed(lastResult, TimeSpan.FromSeconds(3)).Wait();

            var view = _viewManager1.Load(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId("rootid"));

            Assert.That(view.Calls.All(c => c.Item1 == c.Item2), "Registered calls contained a call where the version of the loaded aggregate root did not correspond to the version of the event that the view got to process: {0}",
                        string.Join(", ", view.Calls.Select(c => string.Format("{0}/{1}", c.Item1, c.Item2))));
        }
Example #6
0
        public void WorksWithInstancePerAggregateRootView()
        {
            _context.AddViewManager(_instancePerAggregateRootViewManager);

            _context.Save("id1", new ThisIsJustAnEvent());
            _context.Save("id1", new ThisIsJustAnEvent());
            _context.Save("id1", new ThisIsJustAnEvent());

            _context.Save("id2", new ThisIsJustAnEvent());
            _context.Save("id2", new ThisIsJustAnEvent());
            _context.Save("id2", new ThisIsJustAnEvent());

            _context.WaitForViewsToCatchUp();

            var view = _factory.Load <InstancePerAggregateRootView>(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId("id1"));

            Assert.That(view.EventCounter, Is.EqualTo(3));
        }
        void RunTest(string id)
        {
            _context.Save(id, new Event {
                NumberOfChildren = 1
            });
            _context.Save(id, new Event {
                NumberOfChildren = 2
            });
            _context.Save(id, new Event {
                NumberOfChildren = 3
            });
            _context.Save(id, new Event {
                NumberOfChildren = 2
            });
            _context.Save(id, new Event {
                NumberOfChildren = 2
            });
            _context.Save(id, new Event {
                NumberOfChildren = 3
            });
            _context.Save(id, new Event {
                NumberOfChildren = 2
            });
            _context.Save(id, new Event {
                NumberOfChildren = 2
            });
            _context.Save(id, new Event {
                NumberOfChildren = 3
            });
            _context.Save(id, new Event {
                NumberOfChildren = 2
            });

            using (var context = _viewManager.CreateContext())
            {
                var viewId = InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId(id);

                var parent = context.Views
                             .First(v => v.Id == viewId);

                Assert.That(parent.Children.Count, Is.EqualTo(2));
                Assert.That(parent.OtherChildren.Count, Is.EqualTo(4));
            }
        }
Example #8
0
        public void CanBlockUntilViewIsUpdated(BlockOption blockOption)
        {
            // arrange
            var slowView = new MongoDbViewManager <SlowView>(_mongoDatabase);

            _dispatcher.AddViewManager(slowView);

            _commandProcessor.ProcessCommand(new BitePotato("potato1", .1m));
            _commandProcessor.ProcessCommand(new BitePotato("potato1", .1m));
            _commandProcessor.ProcessCommand(new BitePotato("potato1", .1m));
            _commandProcessor.ProcessCommand(new BitePotato("potato1", .1m));

            var result = _commandProcessor.ProcessCommand(new BitePotato("potato1", 1));

            // act
            switch (blockOption)
            {
            case BlockOption.BlockOnViewManager:
                Console.WriteLine("Waiting for {0} on the view...", result.GetNewPosition());
                slowView.WaitUntilProcessed(result, TimeSpan.FromSeconds(2)).Wait();
                break;

            case BlockOption.BlockOnEventDispatcher:
                Console.WriteLine("Waiting for {0} on the dispatcher...", result.GetNewPosition());
                _dispatcher.WaitUntilProcessed <SlowView>(result, TimeSpan.FromSeconds(2)).Wait();
                break;
            }

            // assert
            var instance = slowView.Load(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId("potato1"));

            if (blockOption == BlockOption.NoBlock)
            {
                Assert.That(instance, Is.Null);
                Console.WriteLine("View instance was null, just as expected");
            }
            else
            {
                Assert.That(instance, Is.Not.Null);
                Console.WriteLine("View instance was properly updated, just as expected");
            }
        }
Example #9
0
        public async Task AutomaticallyCatchesUpAfterPurging()
        {
            // arrange
            Console.WriteLine("Adding view manager for GeneratedIds");
            var view = _factory.GetViewManager <GeneratedIds>();

            _context.AddViewManager(view);

            Console.WriteLine("Processing 2 commands");
            _context.ProcessCommand(new GenerateNewId(IdGenerator.InstanceId)
            {
                IdBase = "bim"
            });
            _context.ProcessCommand(new GenerateNewId(IdGenerator.InstanceId)
            {
                IdBase = "bim"
            });
            var last = _context.ProcessCommand(new GenerateNewId(IdGenerator.InstanceId)
            {
                IdBase = "bom"
            });

            view.WaitUntilProcessed(last, _defaultTimeout).Wait();

            // act
            Console.WriteLine("Purging view");
            _factory.PurgeView <GeneratedIds>();

            // assert
            await view.WaitUntilProcessed(last, _defaultTimeout);

            var idsView = _factory
                          .Load <GeneratedIds>(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId(IdGenerator.InstanceId));

            var storedIds = idsView.AllIds;

            Assert.That(storedIds.Count, Is.EqualTo(3));

            Assert.That(storedIds, Contains.Item("bim/0"));
            Assert.That(storedIds, Contains.Item("bim/1"));
            Assert.That(storedIds, Contains.Item("bom/0"));
        }
        public void CanDoIt()
        {
            const string id = "EB68A8A9-7660-46C1-A44A-48D3DD4A1308";

            Process(new Command(id));
            Process(new Command(id));
            var lastResult = Process(new Command(id));

            Console.WriteLine("Waiting until {0} has reached {1}... ", lastResult, _viewManager);

            _viewManager.WaitUntilProcessed(lastResult, TimeSpan.FromSeconds(10)).Wait();

            Console.WriteLine("Done! - waiting 2 more seconds..");

            Thread.Sleep(2000);

            var view = _viewManager.Load(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId(id));

            Assert.That(view.AppliedEventsAccordingToView, Is.EqualTo(3));
            Assert.That(view.AppliedEventsAccordingToRoot, Is.EqualTo(3));
        }
Example #11
0
        public void AutomaticallyCatchesUpWhenInitializing()
        {
            // arrange
            Console.WriteLine("Processing 2 commands");
            _context.ProcessCommand(new GenerateNewId(IdGenerator.InstanceId)
            {
                IdBase = "bim"
            });
            _context.ProcessCommand(new GenerateNewId(IdGenerator.InstanceId)
            {
                IdBase = "bim"
            });
            var last = _context.ProcessCommand(new GenerateNewId(IdGenerator.InstanceId)
            {
                IdBase = "bom"
            });

            // act
            Console.WriteLine("Adding view manager for GeneratedIds");
            var view = _factory.GetViewManager <GeneratedIds>();

            _context.AddViewManager(view);

            // assert
            Console.WriteLine("Waiting until dispatched: {0}", last.GetNewPosition());
            view.WaitUntilProcessed(last, _defaultTimeout).Wait();

            var idsView = _factory.Load <GeneratedIds>(InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId(IdGenerator.InstanceId));

            Assert.That(idsView, Is.Not.Null, "Could not find view!");

            var storedIds = idsView.AllIds;

            Assert.That(storedIds.Count, Is.EqualTo(3));

            Assert.That(storedIds, Contains.Item("bim/0"));
            Assert.That(storedIds, Contains.Item("bim/1"));
            Assert.That(storedIds, Contains.Item("bom/0"));
        }
Example #12
0
        public void ViewsCanSubscribeToBaseClasses()
        {
            // arrange
            var viewId = InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId("id");

            _context.Save("id", new Event());
            _context.Save("id", new Event());
            _context.Save("id", new AnotherEvent());
            _context.Save("id", new AnotherEvent());

            _context.WaitForViewsToCatchUp();

            // act
            var normalView = _viewManager1.Load(viewId);
            var viewWithAggregateRootSubscription      = _viewManager2.Load(viewId);
            var viewWithGeneralDomainEventSubscription = _viewManager3.Load(viewId);

            // assert
            Assert.That(normalView.ProcessedEvents, Is.EqualTo(4));
            Assert.That(viewWithAggregateRootSubscription.ProcessedEvents, Is.EqualTo(4), "Expected that the view could get all events from this particular aggregate root");
            Assert.That(viewWithGeneralDomainEventSubscription.ProcessedEvents, Is.EqualTo(4), "Expected that the view could get ALL events");
        }
Example #13
0
        public async Task YeyItWorks()
        {
            CirqusLoggerFactory.Current = new ConsoleLoggerFactory();

            var mongoDatabase = MongoHelper.InitializeTestDatabase();

            var firstView  = new MongoDbViewManager <HeyCounter>(mongoDatabase);
            var secondView = new MongoDbViewManager <WordCounter>(mongoDatabase);

            /*         ________.......------=====^^!^^=====------.......________             */
            var dependentView = new MongoDbViewManager <HeyPercentageCalculator>(mongoDatabase);
            /*         ________.......------=====^^!^^=====------.......________             */

            var waitHandle        = new ViewManagerWaitHandle();
            var specialWaitHandle = new ViewManagerWaitHandle();

            //Brett
            var commandProcessor = CreateCommandProcessor(config => config
                                                          .EventStore(e => e.UseInMemoryEventStore())
                                                          .EventDispatcher(e => {
                e.UseViewManagerEventDispatcher(firstView)
                .WithWaitHandle(waitHandle);

                e.UseViewManagerEventDispatcher(secondView)
                .WithWaitHandle(waitHandle);

                e.UseDependentViewManagerEventDispatcher(dependentView)
                .WithWaitHandle(specialWaitHandle)
                .DependentOn(firstView, secondView)
                .WithViewContext(new Dictionary <string, object>
                {
                    { "heys", mongoDatabase.GetCollection <HeyCounter>(typeof(HeyCounter).Name).AsQueryable() },
                    { "words", mongoDatabase.GetCollection <WordCounter>(typeof(WordCounter).Name).AsQueryable() },
                });
            }));

            //Orig
            //var commandProcessor = CommandProcessor.With()
            //    .EventStore(e => e.UseInMemoryEventStore())
            //    .EventDispatcher(e =>
            //    {
            //        e.UseViewManagerEventDispatcher(firstView)
            //            .WithWaitHandle(waitHandle);

            //        e.UseViewManagerEventDispatcher(secondView)
            //            .WithWaitHandle(waitHandle);

            //        e.UseDependentViewManagerEventDispatcher(dependentView)
            //            .WithWaitHandle(specialWaitHandle)
            //            .DependentOn(firstView, secondView)
            //            .WithViewContext(new Dictionary<string, object>
            //            {
            //                {"heys", mongoDatabase.GetCollection<HeyCounter>(typeof (HeyCounter).Name).AsQueryable()},
            //                {"words", mongoDatabase.GetCollection<WordCounter>(typeof (WordCounter).Name).AsQueryable()},
            //            });
            //    })
            //    .Create();

            RegisterForDisposal(commandProcessor);

            //Brett
            CommandProcessingResult result = null;

            Enumerable.Range(0, 100).ToList().ForEach(i => result = commandProcessor.ProcessCommand(new DoStuff("test", "hej meddig min ven " + i)));

            //orig
            //result = Enumerable.Range(0, 100)
            //    .Select(i => commandProcessor.ProcessCommand(new DoStuff("test", "hej meddig min ven " + i)))
            //    .Last();

            await waitHandle.WaitForAll(result, TimeSpan.FromSeconds(5));

            var viewId = InstancePerAggregateRootLocator.GetViewIdFromAggregateRootId("test");

            var firstViewInstance  = firstView.Load(viewId);
            var secondViewInstance = secondView.Load(viewId);

            Assert.That(firstViewInstance.Count, Is.EqualTo(100));
            Assert.That(secondViewInstance.Count, Is.EqualTo(500));

            Console.WriteLine("Waiting for dependent views to catch up...");
            await specialWaitHandle.WaitForAll(result, TimeSpan.FromSeconds(5));

            Console.WriteLine("DOne!");

            var heyPercentageCalculator = dependentView.Load(viewId);

            Assert.That(heyPercentageCalculator.HeyPercentage, Is.EqualTo(20));
        }