public override void SetUp()
        {
            base.SetUp();
            _node = new MiniNode(PathName);
            _node.Start();

            _firstEvent = TestEvent.NewTestEvent();

            _connection = EventStoreConnection.Create();
            _connection.Connect(_node.TcpEndPoint);

            _connection.AppendToStream("test-stream",
                                       ExpectedVersion.NoStream,
                                       _firstEvent,
                                       TestEvent.NewTestEvent(),
                                       TestEvent.NewTestEvent());

            using (var transaction = _connection.StartTransaction("test-stream", 3))
            {
                transaction.Commit();
            }
        }
Example #2
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));
            }
        }