예제 #1
0
        public void CompositeIsDefault()
        {
            var config   = EngineConfiguration.Create();
            var strategy = config.CreateRolloverStrategy();

            Assert.IsInstanceOf <CompositeRolloverStrategy>(strategy);
        }
예제 #2
0
        public void RolledBackCommandsAreSkipped(Tuple <List <JournalEntry>, string> testCase)
        {
            ICommandStore target = new InMemoryCommandStore(EngineConfiguration.Create().ForIsolatedTest());

            string failureMessage = testCase.Item2;
            var    testEntries    = testCase.Item1;

            //Act
            var actualCommandEntries = CommandStore.CommittedCommandEntries(() => testEntries).ToArray();

            ulong[] rolledBackIds = testEntries.OfType <JournalEntry <RollbackMarker> >().Select(e => e.Id).ToArray();
            int     expectedNumberOfCommandEntries = testEntries.Count - rolledBackIds.Length * 2;

            //Assert
            Assert.AreEqual(expectedNumberOfCommandEntries, actualCommandEntries.Length, failureMessage);

            ulong sequentialId = 1;

            foreach (JournalEntry <Command> journalEntry in actualCommandEntries)
            {
                if (!rolledBackIds.Contains(sequentialId))
                {
                    //TODO: Maybe we should return no-op commands instead of skipping rolled back
                    ulong expectedId = sequentialId + (ulong)rolledBackIds.Count(id => id < journalEntry.Id);
                    Assert.AreEqual(expectedId, journalEntry.Id, failureMessage);
                }
                sequentialId++;
            }
        }
예제 #3
0
        public void Engine_self_configures_for_immutability_model()
        {
            var config = EngineConfiguration.Create().ForImmutability();

            Assert.AreEqual(Kernels.Immutability, config.Kernel);
            Assert.AreEqual(SynchronizationMode.None, config.Synchronization);
        }
예제 #4
0
        public void RevisionIsIncrementedWithEachCommand()
        {
            var config = EngineConfiguration.Create().ForIsolatedTest();
            var target = Db.For <TestModel>(config);

            Assert.AreEqual(0, target.Revision);
            target.AddCustomer("Homer");
            Assert.AreEqual(1, target.Revision);
        }
예제 #5
0
        public void Corrupting_command_effects_ignored()
        {
            var config = EngineConfiguration.Create().ForIsolatedTest();

            config.Kernel = Kernels.RoyalFoodTaster;
            var db = Engine.For <MyModel>(config).GetProxy();

            Assert.Catch(db.MutateAndThrow);
            Assert.AreEqual(ExpectedState, db.GetState());
        }
예제 #6
0
        public void CanGetModelReference()
        {
            var config = EngineConfiguration.Create().ForIsolatedTest();
            var engine = Engine.Create <TestModel>(config);

            engine.Execute(new TestCommandWithoutResult());
            var model = engine.GetModel();

            Assert.IsInstanceOfType(model, typeof(TestModel));
        }
예제 #7
0
        public void ImmutabilityEngineSmokeTest()
        {
            var config = EngineConfiguration.Create().ForImmutability().ForIsolatedTest();
            var engine = Engine.Create <ImmutableModel>(config);
            int actual = engine.Execute(new AppendNumberAndGetSumCommand(42));

            Assert.AreEqual(42, actual);
            engine.Execute(new AppendNumberCommand(58));
            actual = engine.Execute(new NumberSumQuery());
            Assert.AreEqual(actual, 42 + 58);
        }
예제 #8
0
        public void Setup()
        {
            var cfg = EngineConfiguration.Create().ForIsolatedTest();

            _engine                   = Engine.Create <ProxyExceptionTestModel>(cfg);
            _proxy                    = _engine.GetProxy();
            _callsToExecutíng         = 0;
            _callsToExecuted          = 0;
            _engine.CommandExecuting += (sender, args) => _callsToExecutíng++;
            _engine.CommandExecuted  += (sender, args) => _callsToExecuted++;
        }
예제 #9
0
        public void CommandExecuting_can_cancel()
        {
            var config = EngineConfiguration.Create().ForImmutability().ForIsolatedTest();
            var engine = Engine.Create <ImmutableModel>(config);

            engine.CommandExecuting += (s, e) =>
            {
                e.Cancel = true;
            };
            engine.Execute(new AppendNumberCommand(42));
            Config.Engines.CloseAll();
        }
예제 #10
0
        public void Timestamp_preserved_on_restore()
        {
            var command = new SetTimeCommand();
            var config  = EngineConfiguration.Create().ForIsolatedTest();
            var engine  = Engine.Create <TestModel>(config);

            engine.Execute(command);


            var store = config.CreateCommandStore();
            var entry = store.CommandEntries().Single();

            Assert.AreEqual(entry.Created, entry.Item.Timestamp);
            Assert.AreEqual(command.Timestamp, entry.Created);
        }
예제 #11
0
        public void CommandExecuting_is_fired()
        {
            var config = EngineConfiguration.Create().ForImmutability().ForIsolatedTest();
            var engine = Engine.Create <ImmutableModel>(config);

            bool wasFired = false;

            engine.CommandExecuting += (s, e) =>
            {
                wasFired = true;
            };
            engine.Execute(new AppendNumberCommand(42));
            Assert.AreEqual(true, wasFired);
            Config.Engines.CloseAll();
        }
예제 #12
0
        public void MyTestInitialize()
        {
            Directory.CreateDirectory(_path);
            _config = EngineConfiguration.Create();
            _config.Location.OfJournal          = _path;
            _config.MaxEntriesPerJournalSegment = 10;
            _store = new FileCommandStore(_config);
            _store.Initialize();

            var writer = _store.CreateJournalWriter(0);

            for (ulong i = 0; i < 30; i++)
            {
                writer.Write(new JournalEntry <Command>(i + 1, new TestCommandWithoutResult()));
            }
            writer.Close();
        }
예제 #13
0
        public void Snapshots_are_numbered_correctly()
        {
            var config = EngineConfiguration.Create().ForImmutability().ForIsolatedTest();
            var engine = Engine.Create <ImmutableModel>(config);

            engine.Execute(new AppendNumberCommand(42));
            engine.Execute(new AppendNumberCommand(42));
            engine.Execute(new AppendNumberCommand(42));
            engine.Execute(new AppendNumberCommand(42));
            engine.CreateSnapshot();

            var store = config.CreateSnapshotStore();


            Assert.AreEqual(4, store.Snapshots.First().Revision);
            Assert.AreEqual(1, store.Snapshots.Count());
        }
예제 #14
0
        public void ImmutabilityKernelSmokeTest()
        {
            var config = EngineConfiguration.Create().ForImmutability();

            config.Location.OfJournal = Guid.NewGuid().ToString();
            var model = new ImmutableModel();

            config.SetCommandStoreFactory(cfg => new InMemoryCommandStore(cfg));
            var kernel = new ImmutabilityKernel(config, model);

            int actual = (int)kernel.ExecuteCommand(new AppendNumberAndGetSumCommand(42));

            Assert.AreEqual(42, actual);
            kernel.ExecuteCommand(new AppendNumberCommand(58));
            actual = kernel.ExecuteQuery(new NumberSumQuery());
            Assert.AreEqual(actual, 42 + 58);
        }
예제 #15
0
        public void SnapshotPerTransactionCreatesSnapshotsAndNoJournalEntries()
        {
            var config = EngineConfiguration.Create().ForIsolatedTest().ForImmutability();

            config.PersistenceMode = PersistenceMode.SnapshotPerTransaction;
            var engine = Engine.Create <ImmutableModel>(config);

            engine.Execute(new AppendNumberCommand(2));
            engine.Execute(new AppendNumberCommand(42));
            engine.Execute(new AppendNumberCommand(12));
            engine.Close();

            var commandStore  = config.CreateCommandStore();
            var snapshotStore = config.CreateSnapshotStore();

            Assert.AreEqual(3, snapshotStore.Snapshots.Count());
            Assert.AreEqual(0, commandStore.GetJournalEntries().OfType <JournalEntry <Command> >().Count());
        }
예제 #16
0
        public void CanLoadAndCreateRepeatedly()
        {
            var config = EngineConfiguration.Create().WithRandomLocation();

            try
            {
                var engine = Engine.LoadOrCreate <TestModel>(config);
                engine.Execute(new TestCommandWithResult());
                engine.Close();
                engine = Engine.LoadOrCreate <TestModel>(config);
                engine.Close();
            }
            finally
            {
                if (config.CreateCommandStore() is FileCommandStore)
                {
                    Directory.Delete(config.Location.OfJournal, true);
                }
            }
        }
예제 #17
0
        public void EitherMaxTriggersRollover()
        {
            var config     = EngineConfiguration.Create();
            var strategy   = config.CreateRolloverStrategy();
            var maxBytes   = config.MaxBytesPerJournalSegment;
            var maxEntries = config.MaxEntriesPerJournalSegment;

            bool triggered = strategy.Rollover(maxBytes, 0);

            Assert.AreEqual(true, triggered);

            triggered = strategy.Rollover(0, maxEntries);
            Assert.AreEqual(true, triggered);

            triggered = strategy.Rollover(maxBytes - 1, maxEntries - 1);
            Assert.AreEqual(false, triggered);

            triggered = strategy.Rollover(maxBytes, maxEntries);
            Assert.IsTrue(triggered);
        }
예제 #18
0
        public void ManualSnaphots()
        {
            var config = EngineConfiguration.Create().ForIsolatedTest().ForImmutability();

            config.PersistenceMode = PersistenceMode.ManualSnapshots;
            var engine = Engine.Create <ImmutableModel>(config);

            engine.Execute(new AppendNumberCommand(2));
            engine.Execute(new AppendNumberCommand(42));
            engine.CreateSnapshot();
            engine.Execute(new AppendNumberCommand(12));
            engine.Close();

            var store = config.CreateSnapshotStore();

            Assert.AreEqual(1, store.Snapshots.Count());

            engine = Engine.Load <ImmutableModel>(config);
            var sum = engine.Execute(new NumberSumQuery());

            Assert.AreEqual(44, sum);
        }
예제 #19
0
        public void DomainEventsAreDispatched()
        {
            var config = EngineConfiguration.Create().ForIsolatedTest();
            var engine = Engine.Create <TestModel>(config);
            var @event = new Event();
            var events = new List <IEvent[]>();

            engine.CommandExecuted += (s, e) => events.Add(e.Events);

            engine.Execute(new EventEmittingCommand(@event));
            engine.Execute(new EventEmittingCommand(@event, 0));
            engine.Execute(new EventEmittingCommand(@event, 2));
            engine.Execute(new EventEmittingCommand(@event, 4));

            //each invocation should produce an array of events
            Assert.AreEqual(4, events.Count);
            CollectionAssert.AllItemsAreInstancesOfType(events, typeof(IEvent[]));
            Assert.AreEqual(1, events[0].Count());
            Assert.AreEqual(0, events[1].Count());
            Assert.AreEqual(2, events[2].Count());
            Assert.AreEqual(4, events[3].Count());
            Assert.IsTrue(events.SelectMany(e => e).All(e => e == @event));
        }
예제 #20
0
        public void Commands_executed_event_contains_sequential_entry_ids()
        {
            var config = EngineConfiguration.Create()
                         .ForImmutability()
                         .ForIsolatedTest();
            var engine = Engine.Create <ImmutableModel>(config);

            var sequence = new List <ulong>();

            engine.CommandExecuted += (s, e) => sequence.Add(e.JournalEntryId);

            for (int i = 1; i <= 100; i++)
            {
                engine.Execute(new AppendNumberCommand(i));
            }
            foreach (var entryId in sequence)
            {
                Console.WriteLine(entryId);
            }
            var sum = engine.Execute((ImmutableModel m) => m.Numbers().Sum());

            Assert.AreEqual((decimal)sum, sequence.Sum <ulong>(n => (decimal)n));
        }
예제 #21
0
        public void RollbackMarkerIsWrittenOnRollback()
        {
            //Arrange
            var config = EngineConfiguration.Create().ForIsolatedTest();
            var store  = new InMemoryCommandStore(config);

            store.Initialize();
            var target = new JournalAppender(1, new StreamJournalWriter(config, store.CreateJournalWriterStream));

            var command = new ACommand();

            command.Timestamp = DateTime.Now;
            target.Append(command);
            target.Append(command);

            //Act
            target.AppendRollbackMarker();

            //Assert
            Assert.AreEqual(3, store.GetJournalEntries().Count());
            Assert.AreEqual(1, store.GetJournalEntries().OfType <JournalEntry <RollbackMarker> >().Count());
            Assert.IsTrue(store.GetJournalEntries().Last() is JournalEntry <RollbackMarker>);
        }
예제 #22
0
        public void JournalingIsDefault()
        {
            var config = EngineConfiguration.Create();

            Assert.AreEqual(config.PersistenceMode, PersistenceMode.Journaling);
        }
예제 #23
0
 public EngineConfiguration CreateConfig()
 {
     return(EngineConfiguration
            .Create().ForIsolatedTest());
 }
예제 #24
0
 private EngineConfiguration Config()
 {
     return(EngineConfiguration.Create().ForIsolatedTest());
 }
예제 #25
0
 public void Init()
 {
     _config       = EngineConfiguration.Create().ForIsolatedTest().ForImmutability();
     _commandStore = new InMemoryCommandStore(_config);
     _commandStore.Initialize();
 }