public async Task GetAvailablePaged()
        {
            var domainObjectStore = new Mock <IDomainObjectStore>(MockBehavior.Strict);

            domainObjectStore.Setup(dos => dos.ReplayObject <ConfigEnvironmentList>(It.IsAny <long>()))
            .ReturnsAsync((long v) =>
            {
                var list = new ConfigEnvironmentList();
                list.ApplyEvent(new ReplayedEvent
                {
                    DomainEvent = new EnvironmentCreated(new EnvironmentIdentifier("Foo", "Foo")),
                    UtcTime     = DateTime.UtcNow,
                    Version     = 4710
                });
                list.ApplyEvent(new ReplayedEvent
                {
                    DomainEvent = new EnvironmentCreated(new EnvironmentIdentifier("Foo", "Bar")),
                    UtcTime     = DateTime.UtcNow,
                    Version     = 4711
                });
                list.ApplyEvent(new ReplayedEvent
                {
                    DomainEvent = new EnvironmentCreated(new EnvironmentIdentifier("Foo", "Baz")),
                    UtcTime     = DateTime.UtcNow,
                    Version     = 4712
                });
                return(Result.Success(list));
            })
            .Verifiable();

            var eventStore = new Mock <IEventStore>(MockBehavior.Strict);

            eventStore.Setup(es => es.ReplayEventsAsStream(
                                 It.IsAny <Func <(StoredEvent StoredEvent, DomainEventMetadata Metadata), bool> >(),
                                 It.IsAny <Func <(StoredEvent StoredEvent, DomainEvent DomainEvent), bool> >(),
                                 It.IsAny <int>(),
                                 It.IsAny <StreamDirection>(),
                                 It.IsAny <long>()))
            .Returns(Task.CompletedTask);

            var store = new EnvironmentProjectionStore(eventStore.Object,
                                                       domainObjectStore.Object,
                                                       _logger,
                                                       new ICommandValidator[0]);

            var result = await store.GetAvailable(QueryRange.Make(1, 1));

            Assert.Empty(result.Message);
            Assert.False(result.IsError, "result.IsError");
            Assert.Single(result.Data);

            domainObjectStore.Verify();
            eventStore.Verify();
        }
Example #2
0
        public async Task CachedItemBeyondMaxVersionReplayed()
        {
            var provider = new ServiceCollection()
                           .AddLogging()
                           .AddMemoryCache()
                           .BuildServiceProvider();

            var config = new ConfigurationBuilder()
                         .AddInMemoryCollection(new[] { new KeyValuePair <string, string>("MemoryCache:Local:Duration", "00:00:10") })
                         .Build();

            var logger   = provider.GetRequiredService <ILogger <DomainObjectStore> >();
            var memCache = provider.GetRequiredService <IMemoryCache>();

            var eventStore = new Mock <IEventStore>(MockBehavior.Strict);

            eventStore.Setup(es => es.ReplayEventsAsStream(
                                 It.IsAny <Func <(StoredEvent, DomainEvent), bool> >(),
                                 It.IsAny <int>(),
                                 It.IsAny <StreamDirection>(),
                                 It.IsAny <long>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            var snapshotStore = new Mock <ISnapshotStore>(MockBehavior.Strict);

            snapshotStore.Setup(s => s.GetSnapshot <ConfigEnvironmentList>(It.IsAny <string>(), It.IsAny <long>()))
            .ReturnsAsync(() => Result.Error <DomainObjectSnapshot>("no snapshot found", ErrorCode.DbQueryError))
            .Verifiable("snapshot not retrieved");

            var item = new ConfigEnvironmentList();

            item.ApplyEvent(new ReplayedEvent
            {
                DomainEvent = new EnvironmentCreated(new EnvironmentIdentifier("Foo", "Bar")),
                UtcTime     = DateTime.UtcNow,
                Version     = 4711
            });

            memCache.Set(nameof(ConfigEnvironmentList), item);

            var store = new DomainObjectStore(eventStore.Object,
                                              snapshotStore.Object,
                                              memCache,
                                              config,
                                              logger);

            var result = await store.ReplayObject <ConfigEnvironmentList>(42);

            Assert.False(result.IsError, "result.IsError");
            Assert.NotNull(result.Data);

            eventStore.Verify();
            snapshotStore.Verify();
        }
        public void ReplayHandlesEnvironmentDeleted()
        {
            var item = new ConfigEnvironmentList();

            item.ApplyEvent(new ReplayedEvent
            {
                DomainEvent = new EnvironmentCreated(new EnvironmentIdentifier("Foo", "Bar")),
                UtcTime     = DateTime.UtcNow,
                Version     = 1
            });

            Assert.NotEmpty(item.Identifiers);

            item.ApplyEvent(new ReplayedEvent
            {
                DomainEvent = new EnvironmentDeleted(new EnvironmentIdentifier("Foo", "Bar")),
                UtcTime     = DateTime.UtcNow,
                Version     = 2
            });

            Assert.Empty(item.Identifiers);
        }
        public void CalculateCacheSizeFilled()
        {
            var item = new ConfigEnvironmentList();

            item.ApplyEvent(new ReplayedEvent
            {
                DomainEvent = new EnvironmentCreated(new EnvironmentIdentifier("Foo", "Bar")),
                UtcTime     = DateTime.UtcNow,
                Version     = 4711
            });

            Assert.InRange(item.CalculateCacheSize(), 0, long.MaxValue);
        }
        public void CreateValidSnapshot()
        {
            var item = new ConfigEnvironmentList();

            item.ApplyEvent(new ReplayedEvent
            {
                DomainEvent = new EnvironmentCreated(new EnvironmentIdentifier("Foo", "Bar")),
                UtcTime     = DateTime.UtcNow,
                Version     = 4711
            });

            var snapshot = item.CreateSnapshot();

            Assert.Equal(item.CurrentVersion, snapshot.Version);
            Assert.Equal(item.MetaVersion, snapshot.MetaVersion);
            Assert.False(string.IsNullOrWhiteSpace(snapshot.Identifier));
            Assert.False(string.IsNullOrWhiteSpace(snapshot.JsonData));
            Assert.False(string.IsNullOrWhiteSpace(snapshot.DataType));
        }
        public void SnapshotAppliesAllProperties()
        {
            var snapshotSource = new ConfigEnvironmentList();

            snapshotSource.ApplyEvent(new ReplayedEvent
            {
                DomainEvent = new EnvironmentCreated(new EnvironmentIdentifier("Foo", "Bar")),
                UtcTime     = DateTime.UtcNow,
                Version     = 4711
            });
            var snapshot = snapshotSource.CreateSnapshot();

            var target = new ConfigEnvironmentList();

            target.ApplySnapshot(snapshot);

            Assert.Equal(snapshotSource.Identifiers, target.Identifiers);
            Assert.Equal(snapshotSource.CurrentVersion, target.CurrentVersion);
            Assert.Equal(snapshotSource.MetaVersion, target.MetaVersion);
        }