Beispiel #1
0
        public void Replaying_a_subset()
        {
            const string PATH = nameof(FolderEventstore_tests) + "_" + nameof(Replaying_a_subset);

            if (Directory.Exists(PATH))
            {
                Directory.Delete(PATH, true);
            }
            var sut = new Eventstore <FilesInFolderEventRepository>(PATH);

            IEvent e0 = new TestEvent {
                Foo = "a"
            };
            IEvent e1 = new AnotherTestEvent {
                Bar = 1
            };
            IEvent e2 = new AnotherTestEvent {
                Bar = 2
            };
            IEvent e3 = new TestEvent {
                Foo = "b"
            };
            IEvent e4 = new AnotherTestEvent {
                Bar = 3
            };

            sut.Record(null, new IEvent[] { e0, e1, e2, e3, e4 });

            var result = sut.Replay();

            Assert.Equal(e4.Id, result.Last().Id);
            Assert.Equal(5, result.Count());
        }
Beispiel #2
0
        public void Recording_succeeds_with_right_version()
        {
            const string PATH = nameof(FolderEventstore_tests) + "_" + nameof(Recording_succeeds_with_right_version);

            if (Directory.Exists(PATH))
            {
                Directory.Delete(PATH, true);
            }
            var sut = new Eventstore <FilesInFolderEventRepository>(PATH);

            var    state0 = sut.LastEventId;
            IEvent e0     = new TestEvent {
                Foo = "a"
            };

            sut.Record(null, e0);
            var result = sut.LastEventId;

            Assert.NotEqual(state0, result);
            IEvent e1 = new AnotherTestEvent {
                Bar = 1
            };

            sut.Record(e0.Id, e1);
            var result2 = sut.LastEventId;

            Assert.NotEqual(result, result2);
        }
Beispiel #3
0
        public void TestSender_WhenConfiguredMultipleEvents_ThenChooseWisely()
        {
            const string exchangeName        = "exchange";
            const string anotherExchangeName = "another-exchange";

            var @event       = new TestEvent();
            var anotherEvent = new AnotherTestEvent();
            var senderMock   = new Mock <IEventSender>(MockBehavior.Strict);

            senderMock.Setup(m => m.SendEvent(@event));
            var anotherSenderMock = new Mock <IEventSender>(MockBehavior.Strict);

            anotherSenderMock.Setup(m => m.SendEvent(anotherEvent));
            var factoryMock = new Mock <IEventProcessorFactory>(MockBehavior.Strict);

            factoryMock.Setup(m => m.CreateSender(exchangeName)).Returns(senderMock.Object);
            factoryMock.Setup(m => m.CreateSender(anotherExchangeName)).Returns(anotherSenderMock.Object);
            var exchangeConfiguration = new ExchangeConfiguration();

            exchangeConfiguration.ConfigureEventSender(typeof(TestEvent), exchangeName);
            exchangeConfiguration.ConfigureEventSender(typeof(AnotherTestEvent), anotherExchangeName);

            var bus = new Bus(factoryMock.Object, exchangeConfiguration) as IEventSender;

            bus.SendEvent(@event);
            bus.SendEvent(anotherEvent);

            senderMock.Verify(m => m.SendEvent(@event), Times.Once);
            anotherSenderMock.Verify(m => m.SendEvent(@event), Times.Never);
            senderMock.Verify(m => m.SendEvent(anotherEvent), Times.Never);
            anotherSenderMock.Verify(m => m.SendEvent(anotherEvent), Times.Once);
        }
Beispiel #4
0
        public async Task AddEvents()
        {
            var eventStore = new EventStore();
            var testEvent1 = new TestEvent {
                StreamId = "1", Data = "Initial"
            };
            var testEvent2 = new AnotherTestEvent {
                StreamId = "1", Data = "Updated"
            };
            await eventStore.AddEvents("1", new EventStoreEvent[] { testEvent1, testEvent2 });

            var eventStream = await eventStore.GetEvents("1");

            eventStream.Count().Should().Be(2, "should retrieve all events in the stream");
        }
Beispiel #5
0
        public async Task GetEventsBySequence()
        {
            var eventStore = new EventStore();
            var testEvent1 = new TestEvent {
                StreamId = "1", Data = "Initial"
            };
            var testEvent2 = new AnotherTestEvent {
                StreamId = "1", Data = "Updated"
            };
            var testEvent3 = new AnotherTestEvent {
                StreamId = "1", Data = "Updated 2"
            };
            var testEvent4 = new AnotherTestEvent {
                StreamId = "1", Data = "Updated 3"
            };
            await eventStore.AddEvents("1", new EventStoreEvent[] { testEvent1, testEvent2, testEvent3, testEvent4 });

            var eventStream = (await eventStore.GetEvents("1", 2)).ToArray();

            eventStream.Count().Should().Be(2, "should retrieve only events >= sequence");
            eventStream.First().Sequence.Should().Be(2, "should return the event with sequence == 2");
            eventStream.Skip(1).First().Sequence.Should().Be(3, "should return the event with sequence == 3");
        }
Beispiel #6
0
        public void TestSource_WhenConfiguredMultipleEvents_ThenChooseWisely()
        {
            const string exchangeName        = "exchange";
            const string exchangeType        = "type";
            const string routingKey          = "route";
            const string anotherExchangeName = "another-exchange";
            const string anotherExchangeType = "another-type";
            const string anotherRoutingKey   = "another-route";

            var @event       = new TestEvent();
            var anotherEvent = new AnotherTestEvent();
            var sourceMock   = new Mock <IEventSource>(MockBehavior.Strict);

            sourceMock.Setup(m => m.ReceiveEvents <TestEvent>()).Returns(Observable.Return(@event));
            var anotherSourceMock = new Mock <IEventSource>(MockBehavior.Strict);

            anotherSourceMock.Setup(m => m.ReceiveEvents <AnotherTestEvent>()).Returns(Observable.Return(anotherEvent));
            var factoryMock = new Mock <IEventProcessorFactory>(MockBehavior.Strict);

            factoryMock.Setup(m => m.CreateSource(exchangeName, exchangeType, routingKey)).Returns(sourceMock.Object);
            factoryMock.Setup(m => m.CreateSource(anotherExchangeName, anotherExchangeType, anotherRoutingKey)).Returns(anotherSourceMock.Object);
            var exchangeConfiguration = new ExchangeConfiguration();

            exchangeConfiguration.ConfigureEventSource(typeof(TestEvent), exchangeName, exchangeType, routingKey);
            exchangeConfiguration.ConfigureEventSource(typeof(AnotherTestEvent), anotherExchangeName, anotherExchangeType, anotherRoutingKey);

            var bus                  = new Bus(factoryMock.Object, exchangeConfiguration) as IEventSource;
            var receivedEvent        = bus.ReceiveEvents <TestEvent>().Wait();
            var receivedAnotherEvent = bus.ReceiveEvents <AnotherTestEvent>().Wait();

            Assert.Equal(receivedEvent, @event);
            Assert.Equal(receivedAnotherEvent, anotherEvent);
            sourceMock.Verify(m => m.ReceiveEvents <TestEvent>(), Times.Once);
            sourceMock.Verify(m => m.ReceiveEvents <AnotherTestEvent>(), Times.Never);
            anotherSourceMock.Verify(m => m.ReceiveEvents <TestEvent>(), Times.Never);
            anotherSourceMock.Verify(m => m.ReceiveEvents <AnotherTestEvent>(), Times.Once);
        }
Beispiel #7
0
        public void Replaying_from_event_id()
        {
            const string PATH = nameof(FolderEventstore_tests) + "_" + nameof(Replaying_from_event_id);

            if (Directory.Exists(PATH))
            {
                Directory.Delete(PATH, true);
            }
            var sut = new Eventstore <FilesInFolderEventRepository>(PATH);

            IEvent e0 = new TestEvent {
                Foo = "a"
            };
            IEvent e1 = new AnotherTestEvent {
                Bar = 1
            };
            IEvent e2 = new AnotherTestEvent {
                Bar = 2
            };
            IEvent e3 = new TestEvent {
                Foo = "b"
            };
            IEvent e4 = new AnotherTestEvent {
                Bar = 3
            };

            sut.Record(null, new IEvent[] { e0, e1, e2, e3, e4 });

            Replay_from_somewhere_in_the_middle();
            Replay_from_before_the_beginning();
            Replay_from_after_the_end();

            void Replay_from_somewhere_in_the_middle()
            {
                var result = sut.Replay(e2.Id);

                Assert.Equal(3, result.Count());
                Assert.Equal(2, ((AnotherTestEvent)result.ElementAt(0)).Bar);
                Assert.Equal("b", ((TestEvent)result.ElementAt(1)).Foo);
                Assert.Equal(3, ((AnotherTestEvent)result.ElementAt(2)).Bar);

                result = sut.Replay(e4.Id);
                Assert.Single(result);

                result = sut.Replay(e0.Id);
                Assert.Equal(5, result.Count());
            }

            void Replay_from_before_the_beginning()
            {
                var result = sut.Replay();

                Assert.Equal(5, result.Count());
            }

            void Replay_from_after_the_end()
            {
                var result = sut.Replay(e4.Id);

                Assert.Single(result);
            }
        }