public async Task CatchupSubscription_ReceivesEvents_Success() { var streamId = $"TestStream-{Guid.NewGuid():N}"; var credentials = new UserCredentials("admin", "changeit"); var connectionSettings = new ConnectionSettings(credentials, "127.0.0.1", 1113, "myConnection"); var connection = new EventStoreConnection(connectionSettings); await connection.ConnectAsync().ConfigureAwait(false); // First write a few events before the subscription const int initialEventCounter = 3; for (var i = 0; i < initialEventCounter; ++i) { await WriteRandomEventToStream(streamId, connection).ConfigureAwait(false); } // Start catchup subscription var totalEventsHandled = 0; var subscription = await connection.CreateCatchupSubscription(streamId, 0, async e => { Interlocked.Increment(ref totalEventsHandled); }).ConfigureAwait(false); const int afterEventCounter = 4; for (var i = 0; i < afterEventCounter; ++i) { await WriteRandomEventToStream(streamId, connection).ConfigureAwait(false); } const int attempts = 10; for (var i = 0; i < attempts; ++i) { if (Thread.VolatileRead(ref totalEventsHandled) == initialEventCounter + afterEventCounter) { return; } await Task.Delay(500).ConfigureAwait(false); } throw new Exception($"Only {Thread.VolatileRead(ref totalEventsHandled)} events handled of expected {initialEventCounter + afterEventCounter}"); }