public void multiple_idempotent_writes() { const string stream = "multiple_idempotent_writes"; using (var store = BuildConnection(_node)) { 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 async Task 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(_node)) { await store.ConnectAsync(); var result = await store .ConditionalAppendToStreamAsync(stream, ExpectedVersion.Any, new[] { TestEvent.NewTestEvent() }); Assert.AreEqual(ConditionalWriteStatus.Succeeded, result.Status); Assert.IsNotNull(result.LogPosition); Assert.IsNotNull(result.NextExpectedVersion); } }
public async Task 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(_node)) { await store.ConnectAsync(); Assert.AreEqual(0, (await store.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent())).NextExpectedVersion); var read = await store.ReadStreamEventsForwardAsync(stream, 0, 2, resolveLinkTos : false); Assert.That(read.Events.Length, Is.EqualTo(1)); } }
public async Task 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(_node)) { await store.ConnectAsync(); for (var i = 0; i < 5; i++) { await store.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent()); } await store.AppendToStreamAsync(stream, ExpectedVersion.StreamExists, TestEvent.NewTestEvent()); } }
public async Task 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(_node)) { await store.ConnectAsync(); var wev = await AssertEx.ThrowsAsync <WrongExpectedVersionException>( () => store.AppendToStreamAsync(stream, ExpectedVersion.StreamExists, TestEvent.NewTestEvent())); Assert.AreEqual(ExpectedVersion.StreamExists, wev.ExpectedVersion); Assert.AreEqual(ExpectedVersion.NoStream, wev.ActualVersion); } }
public async Task should_return_log_position_when_writing() { const string stream = "should_return_log_position_when_writing"; using (var store = BuildConnection(_node)) { await store.ConnectAsync(); var result = await store.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent()); Assert.IsTrue(0 < result.LogPosition.PreparePosition); Assert.IsTrue(0 < result.LogPosition.CommitPosition); } }
public async Task 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 = BuildConnection(_node)) { await store.ConnectAsync(); var wev = await AssertEx.ThrowsAsync <WrongExpectedVersionException>(() => store.AppendToStreamAsync(stream, 1, new[] { TestEvent.NewTestEvent() })); Assert.AreEqual(1, wev.ExpectedVersion); Assert.AreEqual(ExpectedVersion.NoStream, wev.ActualVersion); } }
public void return_partial_slice_if_not_enough_events_in_stream() { const string stream = "read_event_stream_forward_should_return_partial_slice_if_no_enough_events_in_stream"; using (var store = TestConnection.Create(_node.TcpEndPoint)) { store.Connect(); 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, 9, 5, resolveLinkTos: false); Assert.DoesNotThrow(read.Wait); Assert.That(read.Result.Events.Length, Is.EqualTo(1)); } }
public void return_partial_slice_when_got_int_max_value_as_maxcount() { const string stream = "read_event_stream_forward_should_return_partial_slice_when_got_int_max_value_as_maxcount"; using (var store = TestConnection.Create(_node.TcpEndPoint)) { store.Connect(); var write10 = store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, Enumerable.Range(0, 10).Select(x => TestEvent.NewTestEvent(x.ToString()))); Assert.DoesNotThrow(write10.Wait); var read = store.ReadStreamEventsForwardAsync(stream, StreamPosition.Start, int.MaxValue, resolveLinkTos: false); Assert.DoesNotThrow(read.Wait); Assert.That(read.Result.Events.Length, Is.EqualTo(10)); } }
public void should_create_stream_with_no_stream_exp_ver_on_first_write_if_does_not_exist() { const string stream = "should_create_stream_with_no_stream_exp_ver_on_first_write_if_does_not_exist"; using (var store = BuildConnection(_node)) { store.ConnectAsync().Wait(); Assert.AreEqual(0, store.AppendToStreamAsync(stream, ExpectedVersion.NoStream, 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)); } }
protected override void When() { _testEvents = Enumerable.Range(0, 5).Select(x => TestEvent.NewTestEvent(data: x.ToString())).ToArray(); }
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 = BuildConnection(_node)) { store.ConnectAsync().Wait(); Assert.AreEqual(0, store.AppendToStreamAsync(stream, ExpectedVersion.EmptyStream, TestEvent.NewTestEvent()).Result.NextExpectedVersion); var append = store.AppendToStreamAsync(stream, 1, new[] { TestEvent.NewTestEvent() }); Assert.That(() => append.Wait(), Throws.Exception.TypeOf <AggregateException>().With.InnerException.TypeOf <WrongExpectedVersionException>()); } }
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(_node)) { 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(_node)) { store.ConnectAsync().Wait(); var delete = store.DeleteStreamAsync(stream, ExpectedVersion.EmptyStream, hardDelete: true); Assert.DoesNotThrow(delete.Wait); var append = store.AppendToStreamAsync(stream, ExpectedVersion.Any, new[] { TestEvent.NewTestEvent() }); Assert.That(() => append.Wait(), Throws.Exception.TypeOf <AggregateException>().With.InnerException.TypeOf <StreamDeletedException>()); } }
protected override Task When() { _testEvents = Enumerable.Range(0, 5).Select(x => TestEvent.NewTestEvent(data: x.ToString())).ToArray(); return(Task.CompletedTask); }
public async Task be_able_to_subscribe_to_non_existing_stream_and_then_catch_new_event() { const string stream = "subscribe_should_be_able_to_subscribe_to_non_existing_stream_and_then_catch_created_event"; using (var store = BuildConnection(_node)) { await store.ConnectAsync(); var appeared = new CountdownEvent(1); var dropped = new CountdownEvent(1); using (await store.SubscribeToStreamAsync(stream, false, (s, x) => { appeared.Signal(); return; }, (s, r, e) => dropped.Signal())) { await store.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent()); Assert.IsTrue(appeared.Wait(Timeout), "Appeared countdown event timed out."); } } }
public void should_close_connection_after_configured_amount_of_failed_reconnections() { var closed = new ManualResetEventSlim(); var settings = ConnectionSettings.Create() .EnableVerboseLogging() .UseCustomLogger(ClientApiLoggerBridge.Default) .LimitReconnectionsTo(1) .SetReconnectionDelayTo(TimeSpan.FromMilliseconds(0)) .OnClosed((x, r) => closed.Set()) .OnConnected((x, ep) => Console.WriteLine("Connected to [{0}]...", ep)) .OnReconnecting(x => Console.WriteLine("Reconnecting...")) .OnDisconnected((x, ep) => Console.WriteLine("Disconnected from [{0}]...", ep)) .OnErrorOccurred((x, exc) => Console.WriteLine("Error: {0}", exc)) .FailOnNoServerResponse(); if (_tcpType == TcpType.Ssl) { settings.UseSslConnection("ES", false); } var ip = IPAddress.Loopback; int port = PortsHelper.GetAvailablePort(ip); try { using (var connection = EventStoreConnection.Create(settings, new IPEndPoint(ip, port))) { connection.Connect(); if (!closed.Wait(TimeSpan.FromSeconds(120))) // TCP connection timeout might be even 60 seconds { Assert.Fail("Connection timeout took too long."); } Assert.That(() => connection.AppendToStream("stream", ExpectedVersion.EmptyStream, TestEvent.NewTestEvent()), Throws.Exception.InstanceOf <AggregateException>() .With.InnerException.InstanceOf <InvalidOperationException>()); } } finally { PortsHelper.ReturnPort(port); } }
public async Task allow_multiple_subscriptions_to_same_stream() { const string stream = "subscribe_should_allow_multiple_subscriptions_to_same_stream"; using (var store = BuildConnection(_node)) { await store.ConnectAsync(); var appeared = new CountdownEvent(2); var dropped = new CountdownEvent(2); using (await store.SubscribeToStreamAsync(stream, false, (s, x) => { appeared.Signal(); return; }, (s, r, e) => dropped.Signal())) using (await store.SubscribeToStreamAsync(stream, false, (s, x) => { appeared.Signal(); return; }, (s, r, e) => dropped.Signal())) { await store.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent()); Assert.IsTrue(appeared.Wait(Timeout), "Appeared countdown event timed out."); } } }
public async Task 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(_node)) { await store.ConnectAsync(); Assert.AreEqual(0, (await store.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent())).NextExpectedVersion); Assert.AreEqual(1, (await store.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent())).NextExpectedVersion); } }
public void allow_multiple_subscriptions() { const string stream = "subscribe_to_all_should_allow_multiple_subscriptions"; using (var store = BuildConnection(_node)) { store.ConnectAsync().Wait(); var appeared = new CountdownEvent(2); var dropped = new CountdownEvent(2); using (store.SubscribeToAllAsync(false, (s, x) => { appeared.Signal(); return(Task.CompletedTask); }, (s, r, e) => dropped.Signal()).Result) using (store.SubscribeToAllAsync(false, (s, x) => { appeared.Signal(); return(Task.CompletedTask); }, (s, r, e) => dropped.Signal()).Result) { var create = store.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent()); Assert.IsTrue(create.Wait(Timeout), "StreamCreateAsync timed out."); Assert.IsTrue(appeared.Wait(Timeout), "Appeared countdown event timed out."); } } }
public async Task 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(_node)) { await store.ConnectAsync(); await store.AppendToStreamAsync(stream, ExpectedVersion.NoStream, TestEvent.NewTestEvent()); await store.AppendToStreamAsync(stream, ExpectedVersion.StreamExists, TestEvent.NewTestEvent()); } }
public async Task should_close_connection_after_configured_amount_of_failed_reconnections() { var closed = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); var settings = ConnectionSettings.Create() .EnableVerboseLogging() .UseCustomLogger(ClientApiLoggerBridge.Default) .LimitReconnectionsTo(1) .WithConnectionTimeoutOf(TimeSpan.FromSeconds(10)) .SetReconnectionDelayTo(TimeSpan.FromMilliseconds(0)) .FailOnNoServerResponse(); if (_tcpType == TcpType.Ssl) { settings.UseSslConnection(false); } var ip = IPAddress.Loopback; int port = PortsHelper.GetAvailablePort(ip); using var connection = EventStoreConnection.Create(settings, new IPEndPoint(ip, port).ToESTcpUri()); connection.Closed += (s, e) => closed.TrySetResult(true); 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); await connection.ConnectAsync(); await closed.Task.WithTimeout( TimeSpan.FromSeconds(120)); // TCP connection timeout might be even 60 seconds await AssertEx.ThrowsAsync <ObjectDisposedException>(() => connection .AppendToStreamAsync("stream", ExpectedVersion.NoStream, TestEvent.NewTestEvent()) .WithTimeout()); }
public async Task 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(_node)) { await store.ConnectAsync(); await store.SetStreamMetadataAsync(stream, ExpectedVersion.Any, new StreamMetadata(10, null, null, null, null)); await store.AppendToStreamAsync(stream, ExpectedVersion.StreamExists, TestEvent.NewTestEvent()); } }
public void should_return_log_position_when_writing() { const string stream = "delete_should_return_log_position_when_writing"; using (var connection = BuildConnection(_node)) { 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 async Task returns_failure_status_when_conditionally_appending_with_version_mismatch() { const string stream = "returns_failure_status_when_conditionally_appending_with_version_mismatch"; using (var store = BuildConnection(_node)) { await store.ConnectAsync(); var result = await store.ConditionalAppendToStreamAsync(stream, 7, new[] { TestEvent.NewTestEvent() }); Assert.AreEqual(ConditionalWriteStatus.VersionMismatch, result.Status); } }
public void following_append_with_expected_version_any_are_commited_correctly() { Assert.AreEqual(4, _connection.AppendToStreamAsync("test-stream", ExpectedVersion.Any, TestEvent.NewTestEvent(), TestEvent.NewTestEvent()).Result.NextExpectedVersion); var res = _connection.ReadStreamEventsForwardAsync("test-stream", 0, 100, false).Result; Assert.AreEqual(SliceReadStatus.Success, res.Status); Assert.AreEqual(5, res.Events.Length); for (int i = 0; i < 5; ++i) { Assert.AreEqual(i, res.Events[i].OriginalEventNumber); } }
public async Task 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(_node)) { await store.ConnectAsync(); await store.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent()); await store.DeleteStreamAsync(stream, ExpectedVersion.Any, true); var result = await store .ConditionalAppendToStreamAsync(stream, ExpectedVersion.Any, new[] { TestEvent.NewTestEvent() }); Assert.AreEqual(ConditionalWriteStatus.StreamDeleted, result.Status); } }
public void trying_to_append_new_events_with_expected_version_no_stream_fails() { Assert.That(() => _connection.AppendToStreamAsync("test-stream", ExpectedVersion.NoStream, TestEvent.NewTestEvent()).Result, Throws.Exception.InstanceOf <AggregateException>() .With.InnerException.InstanceOf <WrongExpectedVersionException>()); }
public async Task 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(_node)) { await store.ConnectAsync(); await store.DeleteStreamAsync(stream, ExpectedVersion.NoStream, hardDelete : true); await AssertEx.ThrowsAsync <StreamDeletedException>(() => store.AppendToStreamAsync(stream, ExpectedVersion.Any, TestEvent.NewTestEvent())); } }
public async Task trying_to_append_new_events_with_expected_version_no_stream_fails() { await AssertEx.ThrowsAsync <WrongExpectedVersionException>(() => _connection.AppendToStreamAsync("test-stream", ExpectedVersion.NoStream, TestEvent.NewTestEvent())); }