Exemple #1
0
        public void AggregateShouldBeValidWhenLoadedWithExistentSnapshotAndNewEvents()
        {
            EventBus              eventBus;
            EventStore            eventStore;
            SnapshotStore         snapshotStore;
            SnapshotProcessor     snapshotProcessor;
            InMemorySnapshotStore memorySnapshotStore;
            var repository = EnvironmentMock.CreateEnvironment <TestSnapshotAggregate>(out eventBus, out eventStore,
                                                                                       out snapshotStore, out snapshotProcessor, out memorySnapshotStore, 3);

            var guid = Guid.NewGuid();

            repository.Save(new TestSnapshotAggregate(guid, "Test Snapshot Aggregate v0"), -1);

            var aggregate = repository.GetById(guid);

            aggregate.ChangeName("Test Snapshot Aggregate v1");
            aggregate.ChangeName("Test Snapshot Aggregate v2");
            aggregate.ChangeName("Test Snapshot Aggregate v3 (Snapshot)");

            repository.Save(aggregate, 0);

            const string name        = "Test Snapshot Aggregate v4";
            var          aggregateV4 = repository.GetById(guid);

            aggregateV4.ChangeName(name);
            repository.Save(aggregateV4, 3);

            repository.GetById(guid).ReturnNameForTest().Should().Be(name);
        }
Exemple #2
0
        public void SnapshotProcesserIsSnapshotNeededShouldReturnTrueWhenThreasholdIsExceeded()
        {
            EventBus              eventBus;
            EventStore            eventStore;
            SnapshotStore         snapshotStore;
            SnapshotProcessor     snapshotProcessor;
            InMemorySnapshotStore memorySnapshotStore;
            var repository = EnvironmentMock.CreateEnvironment <TestSnapshotAggregate>(out eventBus, out eventStore,
                                                                                       out snapshotStore, out snapshotProcessor, out memorySnapshotStore, 3);

            var guid = Guid.NewGuid();

            repository.Save(new TestSnapshotAggregate(guid, "Test Snapshot Aggregate v0"), -1);

            var aggregate = repository.GetById(guid);

            aggregate.ChangeName("Test Snapshot Aggregate v1");
            aggregate.ChangeName("Test Snapshot Aggregate v2");
            aggregate.ChangeName("Test Snapshot Aggregate v3 (Snapshot)");
            repository.Save(aggregate, 0);

            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 5).Should().BeFalse();
            //Because aggregate version stays 3, every new aggregate version needs a snapshot
            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 6).Should().BeTrue();
            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 7).Should().BeTrue();
            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 8).Should().BeTrue();
            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 9).Should().BeTrue();
        }
Exemple #3
0
        public void RepositoryShouldLoadAndSave20000AggregatesWithSnapShotsEnabledIn60Seconds()
        {
            EventBus              eventBus;
            EventStore            eventStore;
            SnapshotStore         snapshotStore;
            SnapshotProcessor     snapshotProcessor;
            InMemorySnapshotStore memorySnapshotStore;
            var repository = EnvironmentMock.CreateEnvironment <TestSnapshotAggregate>(out eventBus, out eventStore,
                                                                                       out snapshotStore, out snapshotProcessor, out memorySnapshotStore, 100);

            var guid = Guid.NewGuid();

            repository.Save(new TestSnapshotAggregate(guid, "Test Snapshot Aggregate v0"), -1);

            Action act = () =>
            {
                for (var i = 1; i <= 19999; i++)
                {
                    var aggregate = repository.GetById(guid);
                    aggregate.ChangeName("Test Snapshot Aggregate v" + i);
                    repository.Save(aggregate, i - 1);
                }
            };

            act.ExecutionTime().ShouldNotExceed(new TimeSpan(0, 0, 60));
        }
Exemple #4
0
        public void VersionOfAggregatesShouldBeIndependentlyIncreased()
        {
            EventBus   eventBus;
            EventStore eventStore;
            var        repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore);

            var guid1 = Guid.NewGuid();
            var guid2 = Guid.NewGuid();

            repository.Save(new TestAggregate(guid1, "Test Aggregate 1"), -1);
            repository.Save(new TestAggregate(guid2, "Test Aggregate 2"), -1);

            var expectedVersion1 = TestDatabase.Items[guid1].OriginalVersion;
            var expectedVersion2 = TestDatabase.Items[guid2].OriginalVersion;

            var loadedAggregate1 = repository.GetById(guid1);

            loadedAggregate1.ChangeName("New test Aggregate 1");
            repository.Save(loadedAggregate1, expectedVersion1);

            eventStore.GetEventsByAggregateId(guid1)
            .OrderByDescending(e => e.Version)
            .FirstOrDefault()
            .Version.Should()
            .Be(expectedVersion1 + 1);
            eventStore.GetEventsByAggregateId(guid2)
            .OrderByDescending(e => e.Version)
            .FirstOrDefault()
            .Version.Should()
            .Be(expectedVersion2);
        }
Exemple #5
0
        public void SnapshotProcesserIsSnapshotNeededShouldReturnCorrectValuesWhenMultipleEventsAreCommittedAtOnce()
        {
            EventBus              eventBus;
            EventStore            eventStore;
            SnapshotStore         snapshotStore;
            SnapshotProcessor     snapshotProcessor;
            InMemorySnapshotStore memorySnapshotStore;
            var repository = EnvironmentMock.CreateEnvironment <TestSnapshotAggregate>(out eventBus, out eventStore,
                                                                                       out snapshotStore, out snapshotProcessor, out memorySnapshotStore, 3);

            var guid = Guid.NewGuid();

            repository.Save(new TestSnapshotAggregate(guid, "Test Snapshot Aggregate v0"), -1);

            var aggregate = repository.GetById(guid);

            aggregate.ChangeName("Test Snapshot Aggregate v1");
            aggregate.ChangeName("Test Snapshot Aggregate v2");
            aggregate.ChangeName("Test Snapshot Aggregate v3 (Snapshot)");
            aggregate.ChangeName("Test Snapshot Aggregate v4");
            aggregate.ChangeName("Test Snapshot Aggregate v5");
            aggregate.ChangeName("Test Snapshot Aggregate v6 (Snapshot)");
            aggregate.ChangeName("Test Snapshot Aggregate v7");
            aggregate.ChangeName("Test Snapshot Aggregate v8");
            aggregate.ChangeName("Test Snapshot Aggregate v9 (Snapshot)");
            repository.Save(aggregate, 0);

            aggregate.GetSnapshot().Version.Should().Be(9);
            memorySnapshotStore.All().Count().Should().Be(1);
        }
Exemple #6
0
        public void EventStoreShouldThrowConcurrencyExceptionWhenExpectedVersionIsWrong()
        {
            EventBus   eventBus;
            EventStore eventStore;
            var        repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore);

            var guid = Guid.NewGuid();

            repository.Save(new TestAggregate(guid, "Test Aggregate 1"), -1);

            var expectedVersion = TestDatabase.Items[guid].OriginalVersion;

            var loadedAggregate = repository.GetById(guid);

            loadedAggregate.ChangeName("New test Aggregate 1");

            repository.Save(loadedAggregate, expectedVersion);

            var newLoadedAggregate = repository.GetById(guid);

            newLoadedAggregate.ChangeName("Newer test Aggregate 1");

            Action act = () => repository.Save(newLoadedAggregate, expectedVersion);

            act.ShouldThrow <ConcurrencyException>();
        }
        public void RepositoryShouldReturnNewAggregateIfItWasntSavedBefore()
        {
            EventBus   eventBus;
            EventStore eventStore;
            var        repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore);

            var guid = Guid.NewGuid();

            repository.GetById(guid).Should().NotBeNull();
            repository.GetById(guid).Id.Should().Be(Guid.Empty);
        }
Exemple #8
0
        public void VersionOfAggregateShouldBePublishedByEventByStore()
        {
            EventBus   eventBus;
            EventStore eventStore;
            var        repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore);

            var guid = Guid.NewGuid();

            repository.Save(new TestAggregate(guid, "Test Aggregate 1"), -1);

            TestDatabase.Items[guid].OriginalVersion.Should().Be(0);
        }
Exemple #9
0
        public void VersionOfAggregateCreateEventShouldBeZero()
        {
            EventBus   eventBus;
            EventStore eventStore;
            var        repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore);

            var guid      = Guid.NewGuid();
            var aggregate = new TestAggregate(guid, "Test Aggregate 1");

            repository.Save(aggregate, -1);

            eventStore.GetEventsByAggregateId(guid).FirstOrDefault().Version.Should().Be(0);
        }
        public void RepositoryShouldPersistEventToEventStoreWhenAggregateWasSaved()
        {
            EventBus   eventBus;
            EventStore eventStore;
            var        repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore);

            var guid      = Guid.NewGuid();
            var aggregate = new TestAggregate(guid, String.Empty);

            repository.Save(aggregate, -1);

            eventStore.GetEventsByAggregateId(guid).Count().Should().Be(1);
        }
Exemple #11
0
        public void SnapshotProcesserIsSnapshotNeededShouldCorrectlyCalculateThreasholdIntervalsWhenAggregateVersionIsIncreased()
        {
            EventBus              eventBus;
            EventStore            eventStore;
            SnapshotStore         snapshotStore;
            SnapshotProcessor     snapshotProcessor;
            InMemorySnapshotStore memorySnapshotStore;
            var repository = EnvironmentMock.CreateEnvironment <TestSnapshotAggregate>(out eventBus, out eventStore,
                                                                                       out snapshotStore, out snapshotProcessor, out memorySnapshotStore, 3);

            var guid = Guid.NewGuid();

            repository.Save(new TestSnapshotAggregate(guid, "Test Snapshot Aggregate v0"), -1);

            var aggregate = repository.GetById(guid);

            aggregate.ChangeName("Test Snapshot Aggregate v1");
            aggregate.ChangeName("Test Snapshot Aggregate v2");
            aggregate.ChangeName("Test Snapshot Aggregate v3 (Snapshot)");
            repository.Save(aggregate, 0);

            aggregate.ChangeName("Test Snapshot Aggregate v4");
            repository.Save(aggregate, 3);

            aggregate.ChangeName("Test Snapshot Aggregate v5");
            repository.Save(aggregate, 4);

            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 5).Should().BeFalse();
            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 6).Should().BeTrue(); // Needed because last snapshot version is 3

            aggregate.ChangeName("Test Snapshot Aggregate v6 (Snapshot)");
            repository.Save(aggregate, 5);

            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 6).Should().BeFalse(); //Not needed because snapshot was taken of v5

            aggregate.ChangeName("Test Snapshot Aggregate v7");
            repository.Save(aggregate, 6);

            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 7).Should().BeFalse();

            aggregate.ChangeName("Test Snapshot Aggregate v8");
            repository.Save(aggregate, 7);

            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 8).Should().BeFalse();
            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 9).Should().BeTrue(); // Needed because last snapshot version is 6

            aggregate.ChangeName("Test Snapshot Aggregate v9 (snapshot)");
            repository.Save(aggregate, 8);

            snapshotProcessor.IsSnapshotNeeded(guid, typeof(TestSnapshotAggregate), 9).Should().BeFalse(); //Not needed because snapshot was taken of v9
        }
Exemple #12
0
        public void RepositoryShouldThrowExceptionIfNotSupported()
        {
            EventBus              eventBus;
            EventStore            eventStore;
            SnapshotStore         snapshotStore;
            SnapshotProcessor     snapshotProcessor;
            InMemorySnapshotStore memorySnapshotStore;
            var repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore,
                                                                               out snapshotStore, out snapshotProcessor, out memorySnapshotStore, 3);

            Action act = () => repository.GetOriginator(Guid.NewGuid());

            act.ShouldThrow <SnapshotNotSupportedException>();
        }
        public void RepositorySaveShouldInitiateEventHandlersToUpdateReadModel()
        {
            EventBus   eventBus;
            EventStore eventStore;
            var        repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore);

            var          guid      = Guid.NewGuid();
            const string name      = "Initial Aggregate Name";
            var          aggregate = new TestAggregate(guid, String.Empty);

            aggregate.ChangeName(name);

            repository.Save(aggregate, -1);

            TestDatabase.Items[guid].Name.Should().Be(name);
        }
        public void RepositoryShouldReturnPreviouslySavedAggregate()
        {
            EventBus   eventBus;
            EventStore eventStore;
            var        repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore);

            var          guid      = Guid.NewGuid();
            const string name      = "Initial Aggregate Name";
            var          aggregate = new TestAggregate(guid, String.Empty);

            aggregate.ChangeName(name);

            repository.Save(aggregate, -1);

            repository.GetById(guid).ReturnNameForTest().Should().Be(name);
        }
        public void RepositorySaveShouldMarkAllAggregateChangesAsCommitted()
        {
            EventBus   eventBus;
            EventStore eventStore;
            var        repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore);

            var          guid      = Guid.NewGuid();
            const string name      = "Initial Aggregate Name";
            var          aggregate = new TestAggregate(guid, String.Empty);

            aggregate.ChangeName(name);

            repository.Save(aggregate, -1);

            repository.GetById(guid).HasPendingChanges().Should().Be(false);
        }
Exemple #16
0
        public void SnapshotProcessorShouldNotCreateSnapshotIfAggregateDoesntSupportThis()
        {
            EventBus              eventBus;
            EventStore            eventStore;
            SnapshotStore         snapshotStore;
            SnapshotProcessor     snapshotProcessor;
            InMemorySnapshotStore memorySnapshotStore;

            EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore,
                                                              out snapshotStore, out snapshotProcessor, out memorySnapshotStore);

            var guid = Guid.NewGuid();

            snapshotProcessor.CreateSnapshot(guid, typeof(TestAggregate));

            memorySnapshotStore.Find(guid).Should().BeEmpty();
        }
Exemple #17
0
        public void SnapshotProcessorShouldNotCreateSnapshotBeforeThresholdIsReached()
        {
            EventBus              eventBus;
            EventStore            eventStore;
            SnapshotStore         snapshotStore;
            SnapshotProcessor     snapshotProcessor;
            InMemorySnapshotStore memorySnapshotStore;
            var repository = EnvironmentMock.CreateEnvironment <TestSnapshotAggregate>(out eventBus, out eventStore,
                                                                                       out snapshotStore, out snapshotProcessor, out memorySnapshotStore, 3);

            var guid      = Guid.NewGuid();
            var aggregate = new TestSnapshotAggregate(guid, "Test Snapshot Aggregate v0");

            aggregate.ChangeName("Test Snapshot Aggregate v1");

            repository.Save(aggregate, -1);

            memorySnapshotStore.Find(guid).Should().BeEmpty();
        }
Exemple #18
0
        public void VersionOfAggregateShouldBeIncreatedWithNewAddedEvents()
        {
            EventBus   eventBus;
            EventStore eventStore;
            var        repository = EnvironmentMock.CreateEnvironment <TestAggregate>(out eventBus, out eventStore);

            var guid = Guid.NewGuid();

            repository.Save(new TestAggregate(guid, "Test Aggregate 1"), -1);

            var expectedVersion = TestDatabase.Items[guid].OriginalVersion;

            var loadedAggregate = repository.GetById(guid);

            loadedAggregate.ChangeName("New test Aggregate 1");
            repository.Save(loadedAggregate, expectedVersion);

            eventStore.GetEventsByAggregateId(guid)
            .OrderByDescending(e => e.Version)
            .FirstOrDefault()
            .Version.Should()
            .Be(expectedVersion + 1);
        }