Beispiel #1
0
        public void should_fail_to_create_stream_with_wrong_exp_ver_on_first_write_if_does_not_exist()
        {
            const string stream = "should_fail_to_create_stream_with_wrong_exp_ver_on_first_write_if_does_not_exist";

            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
            {
                var append = store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, new[] { new TestEvent() });
                Assert.That(() => append.Wait(), Throws.Exception.TypeOf <AggregateException>().With.InnerException.TypeOf <WrongExpectedVersionException>());
            }
        }
Beispiel #2
0
        public void should_fail_appending_with_wrong_exp_ver_to_existing_stream()
        {
            const string stream = "should_fail_appending_with_wrong_exp_ver_to_existing_stream";

            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
            {
                var create = store.CreateStreamAsync(stream, new byte[0]);
                Assert.DoesNotThrow(create.Wait);

                var append = store.AppendToStreamAsync(stream, 1, new[] { new TestEvent() });
                Assert.That(() => append.Wait(), Throws.Exception.TypeOf <AggregateException>().With.InnerException.TypeOf <WrongExpectedVersionException>());
            }
        }
Beispiel #3
0
        public void should_append_with_any_exp_ver_to_existing_stream()
        {
            const string stream = "should_append_with_any_exp_ver_to_existing_stream";

            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
            {
                var create = store.CreateStreamAsync(stream, new byte[0]);
                Assert.DoesNotThrow(create.Wait);

                var append = store.AppendToStreamAsync(stream, ExpectedVersion.Any, new[] { new TestEvent() });
                Assert.DoesNotThrow(append.Wait);
            }
        }
Beispiel #4
0
        public void should_create_stream_with_any_exp_ver_on_first_write_if_does_not_exist()
        {
            const string stream = "should_create_stream_with_any_exp_ver_on_first_write_if_does_not_exist";

            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
            {
                var append = store.AppendToStreamAsync(stream, ExpectedVersion.Any, new[] { new TestEvent() });
                Assert.DoesNotThrow(append.Wait);

                var read = store.ReadEventStreamForwardAsync(stream, 0, 2);
                Assert.DoesNotThrow(read.Wait);
                Assert.That(read.Result.Events.Length, Is.EqualTo(2));
            }
        }
Beispiel #5
0
        public void can_append_multiple_events_at_once()
        {
            const string stream = "can_append_multiple_events_at_once";

            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
            {
                var create = store.CreateStreamAsync(stream, new byte[0]);
                Assert.DoesNotThrow(create.Wait);

                var events = Enumerable.Range(0, 100).Select(i => new TestEvent(i.ToString(), i.ToString()));
                var append = store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, events);
                Assert.DoesNotThrow(append.Wait);
            }
        }
        protected async Task <IReadOnlyList <IEvent> > Save(AggregateRoot aggregate, string streamName, Action <IDictionary <string, object> > updateHeaders)
        {
            var commitHeaders = new Dictionary <string, object>
            {
                { AGGREGATE_CLR_TYPE_HEADER, aggregate.GetType().AssemblyQualifiedName }
            };

            updateHeaders?.Invoke(commitHeaders);

            var newEvents       = new List <IEvent>(aggregate.GetUncommittedChanges()).AsReadOnly();
            var originalVersion = aggregate.Version - newEvents.Count - 1; // -1 because the stream beginns at version 0
            var expectedVersion = originalVersion < 0 ? ExpectedVersion.NoStream : originalVersion;
            var eventsToSave    = newEvents.Select(e => EventSerializer.ToEventData(e, commitHeaders)).ToList();

            await EventStoreConnection.AppendToStreamAsync(streamName, expectedVersion, eventsToSave);

            aggregate.ClearUncommittedChanges();

            return(newEvents);
        }
Beispiel #7
0
        public void should_not_fail_to_commit_if_started_with_wrong_ver_but_committing_with_correct_ver()
        {
            const string stream = "should_not_fail_to_commit_if_started_with_wrong_ver_but_committing_with_correct_ver";

            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
                store.CreateStream(stream, new byte[0]);

            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
            {
                var start = store.StartTransactionAsync(stream, 1);
                Assert.DoesNotThrow(start.Wait);

                var append = store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, new[] { new TestEvent() });
                Assert.DoesNotThrow(append.Wait);

                var write = store.TransactionalWriteAsync(start.Result.TransactionId, start.Result.Stream, new[] { new TestEvent() });
                Assert.DoesNotThrow(write.Wait);

                var commit = store.CommitTransactionAsync(start.Result.TransactionId, start.Result.Stream);
                Assert.DoesNotThrow(commit.Wait);
            }
        }
Beispiel #8
0
        public void should_fail_to_commit_if_started_with_correct_ver_but_committing_with_bad()
        {
            const string stream = "should_fail_to_commit_if_started_with_correct_ver_but_committing_with_bad";

            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
                store.CreateStream(stream, new byte[0]);

            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
            {
                var start = store.StartTransactionAsync(stream, ExpectedVersion.EmptyStream);
                Assert.DoesNotThrow(start.Wait);

                var append = store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, new[] { new TestEvent() });
                Assert.DoesNotThrow(append.Wait);

                var write = store.TransactionalWriteAsync(start.Result.TransactionId, start.Result.Stream, new[] { new TestEvent() });
                Assert.DoesNotThrow(write.Wait);

                var commit = store.CommitTransactionAsync(start.Result.TransactionId, start.Result.Stream);
                Assert.That(() => commit.Wait(), Throws.Exception.TypeOf <AggregateException>().With.InnerException.TypeOf <WrongExpectedVersionException>());
            }
        }
Beispiel #9
0
        public void should_commit_when_writing_with_exp_ver_any_even_while_somene_is_writing_in_parallel()
        {
            const string stream = "should_commit_when_writing_with_exp_ver_any_even_while_somene_is_writing_in_parallel";

            var transWritesCompleted        = new AutoResetEvent(false);
            var writesToSameStreamCompleted = new AutoResetEvent(false);

            var totalTranWrites  = 500;
            var totalPlainWrites = 500;

            //excplicitly creating stream
            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
                store.CreateStream(stream, new byte[0]);

            //500 events during transaction
            ThreadPool.QueueUserWorkItem(_ =>
            {
                using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
                {
                    var transaction = store.StartTransaction(stream, ExpectedVersion.Any);
                    var writes      = new List <Task>();
                    for (int i = 0; i < totalTranWrites; i++)
                    {
                        if (i % 10 == 0)
                        {
                            writes.RemoveAll(write => write.IsCompleted);
                        }

                        writes.Add(store.TransactionalWriteAsync(transaction.TransactionId,
                                                                 transaction.Stream,
                                                                 new[] { new TestEvent((i + 1).ToString(), "trans write") }));
                    }

                    Task.WaitAll(writes.ToArray());
                    store.CommitTransaction(transaction.TransactionId, transaction.Stream);

                    transWritesCompleted.Set();
                }
            });

            //500 events to same stream in parallel
            ThreadPool.QueueUserWorkItem(_ =>
            {
                using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
                {
                    var writes = new List <Task>();
                    for (int i = 0; i < totalPlainWrites; i++)
                    {
                        if (i % 10 == 0)
                        {
                            writes.RemoveAll(write => write.IsCompleted);
                        }

                        writes.Add(store.AppendToStreamAsync(stream,
                                                             ExpectedVersion.Any,
                                                             new[] { new TestEvent((i + 1).ToString(), "plain write") }));
                    }

                    Task.WaitAll(writes.ToArray());

                    writesToSameStreamCompleted.Set();
                }
            });

            transWritesCompleted.WaitOne();
            writesToSameStreamCompleted.WaitOne();

            //check all written
            using (var store = new EventStoreConnection(MiniNode.Instance.TcpEndPoint))
            {
                var slice = store.ReadEventStreamForward(stream, 0, totalTranWrites + totalPlainWrites + 1);
                Assert.That(slice.Events.Length, Is.EqualTo(totalTranWrites + totalPlainWrites + 1));

                Assert.That(slice.Events.Count(ent => Encoding.UTF8.GetString(ent.Metadata) == "trans write"), Is.EqualTo(totalTranWrites));
                Assert.That(slice.Events.Count(ent => Encoding.UTF8.GetString(ent.Metadata) == "plain write"), Is.EqualTo(totalPlainWrites));
            }
        }