public void TrySaveEventsInMultipleCommits()
        {
            // arrange
            var eventStore = new SqlServerEventStore(this.ConnectionString);
            var streamId   = Guid.NewGuid();
            var events1    = new[] { new Event {
                                         Id = 1, Value = "One"
                                     } };
            var events2 = new[] { new Event {
                                      Id = 2, Value = "Two"
                                  } };

            string firstCommitState;
            string secondCommitState;
            string actualCommitState;

            // act
            eventStore.CommitStream(streamId, events1, Guid.NewGuid(), null, out firstCommitState);
            eventStore.CommitStream(streamId, events2, Guid.NewGuid(), firstCommitState, out secondCommitState);
            var actualEvents = eventStore.GetStream(streamId, 0, out actualCommitState);

            // assert
            actualEvents.Count().Should().Be(2);
            actualEvents.First().Should().BeOfType <Event>();
            actualEvents.Last().Should().BeOfType <Event>();
            actualEvents.Cast <Event>().First().ShouldBeEquivalentTo(events1[0]);
            actualEvents.Cast <Event>().Last().ShouldBeEquivalentTo(events2[0]);
            actualCommitState.Should().Be(secondCommitState);
        }
        public void TrySaveEventsForMultipleStreams()
        {
            // arrange
            var eventStore = new SqlServerEventStore(this.ConnectionString);
            var stream1Id  = Guid.NewGuid();
            var stream2Id  = Guid.NewGuid();
            var events1    = new[] { new Event {
                                         Id = 1, Value = "One"
                                     } };
            var events2 = new[] { new Event {
                                      Id = 2, Value = "Two"
                                  } };

            string stream1CommitState;
            string stream2CommitState;
            string actualStream1CommitState;
            string actualStream2CommitState;

            // act
            eventStore.CommitStream(stream1Id, events1, Guid.NewGuid(), null, out stream1CommitState);
            eventStore.CommitStream(stream2Id, events2, Guid.NewGuid(), null, out stream2CommitState);
            var stream1Events = eventStore.GetStream(stream1Id, 0, out actualStream1CommitState);
            var stream2Events = eventStore.GetStream(stream2Id, 0, out actualStream2CommitState);

            // assert
            stream1Events.Count().Should().Be(1);
            stream1Events.Single().Should().BeOfType <Event>();
            stream1Events.Cast <Event>().Single().ShouldBeEquivalentTo(events1[0]);
            actualStream1CommitState.Should().Be(stream1CommitState);
            stream2Events.Count().Should().Be(1);
            stream2Events.Single().Should().BeOfType <Event>();
            stream2Events.Cast <Event>().Single().ShouldBeEquivalentTo(events2[0]);
            actualStream2CommitState.Should().Be(stream2CommitState);
        }
Exemple #3
0
        public void TryGetBatchTwiceFromEventStoreWithSingleEventAndDifferentDispatchers()
        {
            // arrange
            var eventDispatcherEventStore = new SqlServerEventDispatcherEventStore(this.ConnectionString);
            var eventStore = new SqlServerEventStore(this.ConnectionString);
            var streamId   = Guid.NewGuid();
            var events     = new[]
            {
                new Event {
                    Id = 3, Value = "Three"
                },
            };

            string commitState;

            eventStore.CommitStream(streamId, events, Guid.NewGuid(), null, out commitState);

            // act
            var firstBatch  = eventDispatcherEventStore.GetNextUndispatchedEventsBatch(Guid.NewGuid(), 50);
            var secondBatch = eventDispatcherEventStore.GetNextUndispatchedEventsBatch(Guid.NewGuid(), 50);

            // assert
            firstBatch.Should().NotBeNull();
            firstBatch.Events.Should().ContainSingle(@event => @event.Payload.As <Event>().Id == 3 && @event.Payload.As <Event>().Value == "Three");
            secondBatch.Should().NotBeNull();
            secondBatch.Events.Should().ContainSingle(@event => @event.Payload.As <Event>().Id == 3 && @event.Payload.As <Event>().Value == "Three");
        }
Exemple #4
0
        public void TryGetMultipleBatchesFromEventStoreWithManyEvents()
        {
            // arrange
            var eventDispatcherEventStore = new SqlServerEventDispatcherEventStore(this.ConnectionString);
            var eventStore = new SqlServerEventStore(this.ConnectionString);
            var streamId   = Guid.NewGuid();
            var events     = new[]
            {
                new Event {
                    Id = 4, Value = "Four"
                },
                new Event {
                    Id = 5, Value = "Five"
                },
                new Event {
                    Id = 6, Value = "Six"
                },
            };

            string commitState;

            eventStore.CommitStream(streamId, events, Guid.NewGuid(), null, out commitState);

            // act
            var firstBatch  = eventDispatcherEventStore.GetNextUndispatchedEventsBatch(Guid.Empty, 2);
            var secondBatch = eventDispatcherEventStore.GetNextUndispatchedEventsBatch(Guid.Empty, 2);

            // assert
            firstBatch.Should().NotBeNull();
            firstBatch.Events.Should().Contain(@event => @event.Payload.As <Event>().Id == 4 && @event.Payload.As <Event>().Value == "Four");
            firstBatch.Events.Should().Contain(@event => @event.Payload.As <Event>().Id == 5 && @event.Payload.As <Event>().Value == "Five");
            secondBatch.Should().NotBeNull();
            secondBatch.Events.Should().ContainSingle(@event => @event.Payload.As <Event>().Id == 6 && @event.Payload.As <Event>().Value == "Six");
        }
        public void TrySaveMultipleEvents()
        {
            // arrange
            var eventStore = new SqlServerEventStore(this.ConnectionString);
            var streamId   = Guid.NewGuid();
            var events     = new[]
            {
                new Event {
                    Id = 1, Value = "One"
                },
                new Event {
                    Id = 2, Value = "Two"
                },
            };

            string commitState;
            string actualCommitState;

            // act
            eventStore.CommitStream(streamId, events, Guid.NewGuid(), null, out commitState);
            var actualEvents = eventStore.GetStream(streamId, 0, out actualCommitState);

            // assert
            actualEvents.Count().Should().Be(2);
            actualEvents.First().Should().BeOfType <Event>();
            actualEvents.Last().Should().BeOfType <Event>();
            actualEvents.Cast <Event>().First().ShouldBeEquivalentTo(events[0]);
            actualEvents.Cast <Event>().Last().ShouldBeEquivalentTo(events[1]);
            actualCommitState.Should().Be(commitState);
        }
        public void ExpectServerSideConcurrencyException()
        {
            // arrange
            var eventStore = new SqlServerEventStore(this.ConnectionString);
            var streamId   = Guid.NewGuid();
            var events     = new[] { new Event {
                                         Id = 1, Value = "One"
                                     } };

            string commitState;

            using (new TransactionScope(TransactionScopeOption.RequiresNew))
                using (var connection = new SqlConnection(this.ConnectionString))
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandType = CommandType.Text;
                        command.CommandText = @"EXEC sp_getapplock @Resource = @StreamId, @LockMode = 'Exclusive', @LockTimeout = 1000;";
                        command.Parameters.Add("@StreamId", SqlDbType.UniqueIdentifier).Value = streamId;

                        // NOTE (Cameron): This will cause SQL Server to acquire the same application lock as the commit attempt below.
                        connection.Open();
                        command.ExecuteNonQuery();

                        // act
                        Action action = () => eventStore.CommitStream(streamId, events, Guid.NewGuid(), null, out commitState);

                        // assert
                        action.ShouldThrow <ConcurrencyException>();
                    }
        }
        public void ExpectClientSideConcurrencyException()
        {
            // arrange
            var eventStore = new SqlServerEventStore(this.ConnectionString);
            var streamId   = Guid.NewGuid();
            var events1    = new[] { new Event {
                                         Id = 1, Value = "One"
                                     } };
            var events2 = new[] { new Event {
                                      Id = 1, Value = "AnotherOne"
                                  } };

            string commitState;

            eventStore.CommitStream(streamId, events1, Guid.NewGuid(), null, out commitState);

            // act
            Action action = () => eventStore.CommitStream(streamId, events2, Guid.NewGuid(), null, out commitState);

            // assert
            action.ShouldThrow <ConcurrencyException>();
        }
Exemple #8
0
        public void TryGetBatchFromEventStoreWithSingleEvent()
        {
            // arrange
            var eventDispatcherEventStore = new SqlServerEventDispatcherEventStore(this.ConnectionString);
            var eventStore = new SqlServerEventStore(this.ConnectionString);
            var streamId   = Guid.NewGuid();
            var events     = new[]
            {
                new Event {
                    Id = 1, Value = "One"
                },
            };

            string commitState;

            eventStore.CommitStream(streamId, events, Guid.NewGuid(), null, out commitState);

            // act
            var batch = eventDispatcherEventStore.GetNextUndispatchedEventsBatch(Guid.Empty, 50);

            // assert
            batch.Should().NotBeNull();
            batch.Events.Should().ContainSingle(@event => @event.Payload.As <Event>().Id == 1 && @event.Payload.As <Event>().Value == "One");
        }