public void multiple_idempotent_writes()
        {
            const string stream = "multiple_idempotent_writes";
            using (var store = BuildConnection())
            {
                store.ConnectAsync().Wait();

                var events = new[] { TestEvent.NewTestEvent(), TestEvent.NewTestEvent(), TestEvent.NewTestEvent(), TestEvent.NewTestEvent() };
                Assert.AreEqual(3, store.AppendToStreamAsync(stream, ExpectedVersion.Any, events).Result.NextExpectedVersion);
                Assert.AreEqual(3, store.AppendToStreamAsync(stream, ExpectedVersion.Any, events).Result.NextExpectedVersion);
            }
        }
        public void setting_metadata_for_existing_stream_works()
        {
            _connection.AppendToStreamAsync(_stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Wait();

            var metadataBytes = Guid.NewGuid().ToByteArray();

            _connection.SetStreamMetadataAsync(_stream, ExpectedVersion.EmptyStream, metadataBytes).Wait();

            var meta = _connection.GetStreamMetadataAsRawBytesAsync(_stream).Result;

            Assert.AreEqual(_stream, meta.Stream);
            Assert.AreEqual(false, meta.IsStreamDeleted);
            Assert.AreEqual(0, meta.MetastreamVersion);
            Assert.AreEqual(metadataBytes, meta.StreamMetadata);
        }
        public void soft_deleted_stream_returns_no_stream_and_no_events_on_read()
        {
            const string stream = "soft_deleted_stream_returns_no_stream_and_no_events_on_read";

            Assert.AreEqual(1, _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Result.NextExpectedVersion);
            _conn.DeleteStreamAsync(stream, 1).Wait();

            var res = _conn.ReadStreamEventsForwardAsync(stream, 0, 100, false).Result;

            Assert.AreEqual(SliceReadStatus.StreamNotFound, res.Status);
            Assert.AreEqual(0, res.Events.Length);
            Assert.AreEqual(1, res.LastEventNumber);
        }
Exemple #4
0
 public async Task append_to_stream_should_fail_with_not_supported_exception()
 {
     const string stream = "append_to_stream_should_fail_with_not_supported_exception";
     await AssertEx.ThrowsAsync <OperationNotSupportedException>(
         () => _conn.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent()));
 }
        public void soft_deleted_stream_allows_recreation_only_for_first_write()
        {
            const string stream = "soft_deleted_stream_allows_recreation_only_for_first_write";

            Assert.AreEqual(1, _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Result.NextExpectedVersion);
            _conn.DeleteStreamAsync(stream, 1).Wait();

            var events = new[] { TestEvent.NewTestEvent(), TestEvent.NewTestEvent(), TestEvent.NewTestEvent() };

            Assert.AreEqual(4, _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, events).Result.NextExpectedVersion);
            Assert.That(() => _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent()).Wait(),
                        Throws.Exception.InstanceOf <AggregateException>()
                        .With.InnerException.InstanceOf <WrongExpectedVersionException>());

            var res = _conn.ReadStreamEventsForwardAsync(stream, 0, 100, false).Result;

            Assert.AreEqual(SliceReadStatus.Success, res.Status);
            Assert.AreEqual(4, res.LastEventNumber);
            Assert.AreEqual(3, res.Events.Length);
            Assert.AreEqual(events.Select(x => x.EventId), res.Events.Select(x => x.OriginalEvent.EventId));
            Assert.AreEqual(new[] { 2, 3, 4 }, res.Events.Select(x => x.OriginalEvent.EventNumber));

            var meta = _conn.GetStreamMetadataAsync(stream).Result;

            Assert.AreEqual(2, meta.StreamMetadata.TruncateBefore);
            Assert.AreEqual(1, meta.MetastreamVersion);
        }
        public void setting_json_metadata_on_nonempty_soft_deleted_stream_recreates_stream_not_overriding_metadata()
        {
            const string stream = "setting_json_metadata_on_nonempty_soft_deleted_stream_recreates_stream_not_overriding_metadata";

            Assert.AreEqual(1, _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Result.NextExpectedVersion);
            _conn.DeleteStreamAsync(stream, 1, hardDelete: false).Wait();

            Assert.AreEqual(1, _conn.SetStreamMetadataAsync(stream, 0,
                                                            StreamMetadata.Build().SetMaxCount(100)
                                                            .SetDeleteRole("some-role")
                                                            .SetCustomProperty("key1", true)
                                                            .SetCustomProperty("key2", 17)
                                                            .SetCustomProperty("key3", "some value")).Result.NextExpectedVersion);

            var res = _conn.ReadStreamEventsForwardAsync(stream, 0, 100, false).Result;

            Assert.AreEqual(SliceReadStatus.Success, res.Status);
            Assert.AreEqual(1, res.LastEventNumber);
            Assert.AreEqual(0, res.Events.Length);

            var meta = _conn.GetStreamMetadataAsync(stream).Result;

            Assert.AreEqual(2, meta.MetastreamVersion);
            Assert.AreEqual(2, meta.StreamMetadata.TruncateBefore);
            Assert.AreEqual(100, meta.StreamMetadata.MaxCount);
            Assert.AreEqual("some-role", meta.StreamMetadata.Acl.DeleteRole);
            Assert.AreEqual(true, meta.StreamMetadata.GetValue <bool>("key1"));
            Assert.AreEqual(17, meta.StreamMetadata.GetValue <int>("key2"));
            Assert.AreEqual("some value", meta.StreamMetadata.GetValue <string>("key3"));
        }
        public void should_append_with_stream_exists_exp_ver_if_metadata_stream_exists()
        {
            const string stream = "should_append_with_stream_exists_exp_ver_if_metadata_stream_exists";
            using (var store = BuildConnection())
            {
                store.ConnectAsync().Wait();
                store.SetStreamMetadataAsync(stream, ExpectedVersion.Any, new StreamMetadata(10, null, null, null, null)).Wait();
 
                var append = store.AppendToStreamAsync(stream, ExpectedVersion.StreamExists, new[] { TestEvent.NewTestEvent() });
                Assert.DoesNotThrow(append.Wait);
            }
        }
        public void soft_deleted_stream_when_recreated_preserves_metadata_except_truncatebefore()
        {
            const string stream = "soft_deleted_stream_when_recreated_preserves_metadata_except_truncatebefore";

            Assert.AreEqual(1, _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Result.NextExpectedVersion);

            Assert.AreEqual(0, _conn.SetStreamMetadataAsync(stream, ExpectedVersion.NoStream,
                                                            StreamMetadata.Build().SetTruncateBefore(long.MaxValue)
                                                            .SetMaxCount(100)
                                                            .SetDeleteRole("some-role")
                                                            .SetCustomProperty("key1", true)
                                                            .SetCustomProperty("key2", 17)
                                                            .SetCustomProperty("key3", "some value")).Result.NextExpectedVersion);

            var events = new[] { TestEvent.NewTestEvent(), TestEvent.NewTestEvent(), TestEvent.NewTestEvent() };

            Assert.AreEqual(4, _conn.AppendToStreamAsync(stream, 1, events).Result.NextExpectedVersion);

            var res = _conn.ReadStreamEventsForwardAsync(stream, 0, 100, false).Result;

            Assert.AreEqual(SliceReadStatus.Success, res.Status);
            Assert.AreEqual(4, res.LastEventNumber);
            Assert.AreEqual(3, res.Events.Length);
            Assert.AreEqual(events.Select(x => x.EventId), res.Events.Select(x => x.OriginalEvent.EventId));
            Assert.AreEqual(new[] { 2, 3, 4 }, res.Events.Select(x => x.OriginalEvent.EventNumber));

            var meta = _conn.GetStreamMetadataAsync(stream).Result;

            Assert.AreEqual(1, meta.MetastreamVersion);
            Assert.AreEqual(2, meta.StreamMetadata.TruncateBefore);
            Assert.AreEqual(100, meta.StreamMetadata.MaxCount);
            Assert.AreEqual("some-role", meta.StreamMetadata.Acl.DeleteRole);
            Assert.AreEqual(true, meta.StreamMetadata.GetValue <bool>("key1"));
            Assert.AreEqual(17, meta.StreamMetadata.GetValue <int>("key2"));
            Assert.AreEqual("some value", meta.StreamMetadata.GetValue <string>("key3"));
        }
        public void should_append_with_stream_exists_exp_ver_to_existing_stream()
        {
            const string stream = "should_append_with_stream_exists_exp_ver_to_existing_stream";
            using (var store = BuildConnection())
            {
                store.ConnectAsync().Wait();
                store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, TestEvent.NewTestEvent()).Wait();
 
                var append = store.AppendToStreamAsync(stream, ExpectedVersion.StreamExists, new[] { TestEvent.NewTestEvent() });
                Assert.DoesNotThrow(append.Wait);
            }
        }
        public void should_append_with_stream_exists_exp_ver_to_stream_with_multiple_events()
        {
            const string stream = "should_append_with_stream_exists_exp_ver_to_stream_with_multiple_events";
            using (var store = BuildConnection())
            {
                store.ConnectAsync().Wait();
                for(var i = 0; i < 5; i++) {
                    store.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent()).Wait();
                }
 
                var append = store.AppendToStreamAsync(stream, ExpectedVersion.StreamExists, new[] { TestEvent.NewTestEvent() });
                Assert.DoesNotThrow(append.Wait);
            }
        }
 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 = BuildConnection())
     {
         store.ConnectAsync().Wait();
         Assert.AreEqual(0, store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, TestEvent.NewTestEvent()).Result.NextExpectedVersion);
         Assert.AreEqual(1, store.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent()).Result.NextExpectedVersion);
     }
 }
        public void should_fail_writing_with_any_exp_ver_to_deleted_stream()
        {
            const string stream = "should_fail_writing_with_any_exp_ver_to_deleted_stream";
            using (var store = BuildConnection())
            {
                store.ConnectAsync().Wait();

                try
                {
                    store.DeleteStreamAsync(stream, ExpectedVersion.EmptyStream, hardDelete: true).Wait();
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc);
                    Assert.Fail();
                }

                var append = store.AppendToStreamAsync(stream, ExpectedVersion.Any, new[] { TestEvent.NewTestEvent() });
                Assert.That(() => append.Wait(), Throws.Exception.TypeOf<AggregateException>().With.InnerException.TypeOf<StreamDeletedException>());
            }
        }
 public void should_return_log_position_when_writing()
 {
     const string stream = "should_return_log_position_when_writing";
     using (var store = BuildConnection())
     {
         store.ConnectAsync().Wait();
         var result = store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, TestEvent.NewTestEvent()).Result;
         Assert.IsTrue(0 < result.LogPosition.PreparePosition);
         Assert.IsTrue(0 < result.LogPosition.CommitPosition);
     }
 }
Exemple #14
0
        public void should_close_connection_after_configured_amount_of_failed_reconnections()
        {
            var closed   = new ManualResetEventSlim();
            var settings =
                ConnectionSettings.Create()
                .EnableVerboseLogging()
                .LimitReconnectionsTo(1)
                .WithConnectionTimeoutOf(TimeSpan.FromSeconds(10))
                .SetReconnectionDelayTo(TimeSpan.FromMilliseconds(0))
                .FailOnNoServerResponse();

            if (_tcpType == TcpType.Ssl)
            {
                settings.UseSslConnection("ES", false);
            }

            using (var connection = EventStoreConnection.Create(settings, TestNode.BlackHole.ToESTcpUri()))
            {
                connection.Closed        += (s, e) => closed.Set();
                connection.Connected     += (s, e) => Console.WriteLine("EventStoreConnection '{0}': connected to [{1}]...", e.Connection.ConnectionName, e.RemoteEndPoint);
                connection.Reconnecting  += (s, e) => Console.WriteLine("EventStoreConnection '{0}': reconnecting...", e.Connection.ConnectionName);
                connection.Disconnected  += (s, e) => Console.WriteLine("EventStoreConnection '{0}': disconnected from [{1}]...", e.Connection.ConnectionName, e.RemoteEndPoint);
                connection.ErrorOccurred += (s, e) => Console.WriteLine("EventStoreConnection '{0}': error = {1}", e.Connection.ConnectionName, e.Exception);

                connection.ConnectAsync().Wait();

                if (!closed.Wait(TimeSpan.FromSeconds(120))) // TCP connection timeout might be even 60 seconds
                {
                    Assert.Fail("Connection timeout took too long.");
                }

                Assert.That(() => connection.AppendToStreamAsync("stream", ExpectedVersion.EmptyStream, TestEvent.NewTestEvent()).Wait(),
                            Throws.Exception.InstanceOf <AggregateException>()
                            .With.InnerException.InstanceOf <InvalidOperationException>());
            }
        }
        public void should_fail_appending_with_stream_exists_exp_ver_and_stream_does_not_exist()
        {
            const string stream = "should_fail_appending_with_stream_exists_exp_ver_and_stream_does_not_exist";
            using (var store = BuildConnection())
            {
                store.ConnectAsync().Wait();
 
                var append = store.AppendToStreamAsync(stream, ExpectedVersion.StreamExists, new[] { TestEvent.NewTestEvent() });
                Assert.That(() => append.Wait(), Throws.Exception.TypeOf<AggregateException>().With.InnerException.TypeOf<WrongExpectedVersionException>());
            }
        }
        public void should_return_log_position_when_writing()
        {
            const string stream = "delete_should_return_log_position_when_writing";

            using (var connection = BuildConnection())
            {
                connection.ConnectAsync().Wait();

                var result = connection.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, TestEvent.NewTestEvent()).Result;
                var delete = connection.DeleteStreamAsync(stream, 1, hardDelete: true).Result;

                Assert.IsTrue(0 < result.LogPosition.PreparePosition);
                Assert.IsTrue(0 < result.LogPosition.CommitPosition);
            }
        }
        public void should_fail_appending_with_stream_exists_exp_ver_to_soft_deleted_stream()
        {
            const string stream = "should_fail_appending_with_stream_exists_exp_ver_to_soft_deleted_stream";
            using (var store = BuildConnection())
            {
                store.ConnectAsync().Wait();

                var delete = store.DeleteStreamAsync(stream, ExpectedVersion.EmptyStream, hardDelete: false);
                Assert.DoesNotThrow(delete.Wait);

                var append = store.AppendToStreamAsync(stream, ExpectedVersion.StreamExists, new[] { TestEvent.NewTestEvent() });
                Assert.That(() => append.Wait(), Throws.Exception.TypeOf<AggregateException>().With.InnerException.TypeOf<StreamDeletedException>());
            }
        }
        public void soft_deleted_stream_can_be_hard_deleted()
        {
            const string stream = "soft_deleted_stream_can_be_deleted";

            Assert.AreEqual(1, _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Result.NextExpectedVersion);
            _conn.DeleteStreamAsync(stream, 1).Wait();
            _conn.DeleteStreamAsync(stream, ExpectedVersion.Any, hardDelete: true).Wait();

            var res = _conn.ReadStreamEventsForwardAsync(stream, 0, 100, false).Result;

            Assert.AreEqual(SliceReadStatus.StreamDeleted, res.Status);
            var meta = _conn.GetStreamMetadataAsync(stream).Result;

            Assert.AreEqual(true, meta.IsStreamDeleted);

            Assert.That(() => _conn.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent()).Wait(),
                        Throws.Exception.InstanceOf <AggregateException>()
                        .With.InnerException.InstanceOf <StreamDeletedException>());
        }
        public void returns_success_status_when_conditionally_appending_with_matching_version()
        {
            const string stream = "returns_success_status_when_conditionally_appending_with_matching_version";
            using (var store = BuildConnection())
            {
                store.ConnectAsync().Wait();

                var result = store.ConditionalAppendToStreamAsync(stream, ExpectedVersion.Any, new[] { TestEvent.NewTestEvent() }).Result;

                Assert.AreEqual(ConditionalWriteStatus.Succeeded, result.Status);
                Assert.IsNotNull(result.LogPosition);
                Assert.IsNotNull(result.NextExpectedVersion);
            }
        }
        public void soft_deleted_stream_appends_both_writes_when_expver_any()
        {
            const string stream = "soft_deleted_stream_appends_both_concurrent_writes_when_expver_any";

            Assert.AreEqual(1, _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Result.NextExpectedVersion);
            _conn.DeleteStreamAsync(stream, 1).Wait();

            var events1 = new[] { TestEvent.NewTestEvent(), TestEvent.NewTestEvent(), TestEvent.NewTestEvent() };
            var events2 = new[] { TestEvent.NewTestEvent(), TestEvent.NewTestEvent() };

            Assert.AreEqual(4, _conn.AppendToStreamAsync(stream, ExpectedVersion.Any, events1).Result.NextExpectedVersion);
            Assert.AreEqual(6, _conn.AppendToStreamAsync(stream, ExpectedVersion.Any, events2).Result.NextExpectedVersion);

            var res = _conn.ReadStreamEventsForwardAsync(stream, 0, 100, false).Result;

            Assert.AreEqual(SliceReadStatus.Success, res.Status);
            Assert.AreEqual(6, res.LastEventNumber);
            Assert.AreEqual(5, res.Events.Length);
            Assert.AreEqual(events1.Concat(events2).Select(x => x.EventId), res.Events.Select(x => x.OriginalEvent.EventId));
            Assert.AreEqual(new[] { 2, 3, 4, 5, 6 }, res.Events.Select(x => x.OriginalEvent.EventNumber));

            var meta = _conn.GetStreamMetadataAsync(stream).Result;

            Assert.AreEqual(2, meta.StreamMetadata.TruncateBefore);
            Assert.AreEqual(1, meta.MetastreamVersion);
        }
        public void returns_failure_status_when_conditionally_appending_to_a_deleted_stream()
        {
            const string stream = "returns_failure_status_when_conditionally_appending_to_a_deleted_stream";
            using (var store = BuildConnection())
            {
                store.ConnectAsync().Wait();

                store.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent()).Wait();
                store.DeleteStreamAsync(stream, ExpectedVersion.Any, true).Wait();

                var result = store.ConditionalAppendToStreamAsync(stream, ExpectedVersion.Any, new[] { TestEvent.NewTestEvent() }).Result;

                Assert.AreEqual(ConditionalWriteStatus.StreamDeleted, result.Status);
            }
        }
        public void setting_nonjson_metadata_on_nonempty_soft_deleted_stream_recreates_stream_keeping_original_metadata()
        {
            const string stream = "setting_nonjson_metadata_on_nonempty_soft_deleted_stream_recreates_stream_overriding_metadata";

            Assert.AreEqual(1, _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Result.NextExpectedVersion);
            _conn.DeleteStreamAsync(stream, 1, hardDelete: false).Wait();

            Assert.AreEqual(1, _conn.SetStreamMetadataAsync(stream, 0, new byte[256]).Result.NextExpectedVersion);

            var res = _conn.ReadStreamEventsForwardAsync(stream, 0, 100, false).Result;

            Assert.AreEqual(SliceReadStatus.Success, res.Status);
            Assert.AreEqual(1, res.LastEventNumber);
            Assert.AreEqual(2, res.Events.Length);
            Assert.AreEqual(new[] { 0, 1 }, res.Events.Select(x => x.OriginalEventNumber).ToArray());

            var meta = _conn.GetStreamMetadataAsRawBytesAsync(stream).Result;

            Assert.AreEqual(1, meta.MetastreamVersion);
            Assert.AreEqual(new byte[256], meta.StreamMetadata);
        }
        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 = BuildConnection())
            {
                store.ConnectAsync().Wait();
                Assert.AreEqual(0, store.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent()).Result.NextExpectedVersion);

                var read = store.ReadStreamEventsForwardAsync(stream, 0, 2, resolveLinkTos: false);
                Assert.DoesNotThrow(read.Wait);
                Assert.That(read.Result.Events.Length, Is.EqualTo(1));
            }
        }
        public void soft_deleted_stream_allows_recreation_when_expver_no_stream()
        {
            const string stream = "soft_deleted_stream_allows_recreation_when_expver_no_stream";

            Assert.AreEqual(1, _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Result.NextExpectedVersion);
            _conn.DeleteStreamAsync(stream, 1).Wait();

            var events = new[] { TestEvent.NewTestEvent(), TestEvent.NewTestEvent(), TestEvent.NewTestEvent() };

            Assert.AreEqual(4, _conn.AppendToStreamAsync(stream, ExpectedVersion.NoStream, events).Result.NextExpectedVersion);

            var res = _conn.ReadStreamEventsForwardAsync(stream, 0, 100, false).Result;

            Assert.AreEqual(SliceReadStatus.Success, res.Status);
            Assert.AreEqual(4, res.LastEventNumber);
            Assert.AreEqual(3, res.Events.Length);
            Assert.AreEqual(events.Select(x => x.EventId), res.Events.Select(x => x.OriginalEvent.EventId));
            Assert.AreEqual(new[] { 2, 3, 4 }, res.Events.Select(x => x.OriginalEvent.EventNumber));

            var meta = _conn.GetStreamMetadataAsync(stream).Result;

            Assert.AreEqual(2, meta.StreamMetadata.TruncateBefore);
            Assert.AreEqual(1, meta.MetastreamVersion);
        }
        public void return_empty_slice_when_called_on_non_existing_range()
        {
            const string stream = "read_event_stream_forward_should_return_empty_slice_when_called_on_non_existing_range";

            using (var store = BuildConnection())
            {
                store.ConnectAsync().Wait();

                var write10 = store.AppendToStreamAsync(stream,
                                                        ExpectedVersion.EmptyStream,
                                                        Enumerable.Range(0, 10).Select(x => TestEvent.NewTestEvent((x + 1).ToString(CultureInfo.InvariantCulture))));
                Assert.DoesNotThrow(write10.Wait);

                var read = store.ReadStreamEventsForwardAsync(stream, 11, 5, resolveLinkTos: false);
                Assert.DoesNotThrow(read.Wait);

                Assert.That(read.Result.Events.Length, Is.EqualTo(0));
            }
        }