public async Task StopAsync(CancellationToken cancellationToken) { await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); try { if (_listenerTask == null) { throw new InvalidOperationException("The server is not started"); } await _transportListener.StopAsync(cancellationToken); _listenerCts.Cancel(); _consumerBlock.Complete(); await Task.WhenAll(_listenerTask, _consumerBlock.Completion).ConfigureAwait(false); _listenerCts.Dispose(); _listenerCts = null; _listenerTask = null; _consumerBlock = null; } finally { _semaphore.Release(); } }
public async Task CleanupAsync() { await _transportListener.StopAsync(_cancellationToken); _serverTransport.CloseAsync(_cancellationToken); _clientTransport.CloseAsync(_cancellationToken); }
public async Task SendAsync_AfterServerDisconnect_ClientAutomaticallyReconnects() { ITransport clientTransport = null; ITransport serverTransport = null; ITransportListener transportListener = null; try { // Arrange transportListener = CreateTransportListener(ListenerUri, EnvelopeSerializer); await transportListener.StartAsync(CancellationToken); clientTransport = CreateClientTransport(EnvelopeSerializer); var serverTransportTask = transportListener.AcceptTransportAsync(CancellationToken); await clientTransport.OpenAsync(ListenerUri, CancellationToken); serverTransport = await serverTransportTask; await serverTransport.OpenAsync(ListenerUri, CancellationToken); var message = Dummy.CreateMessage(Dummy.CreateTextContent()); // Act await transportListener.StopAsync(CancellationToken); await serverTransport.CloseAsync(CancellationToken); transportListener = CreateTransportListener(ListenerUri, EnvelopeSerializer); await transportListener.StartAsync(CancellationToken); serverTransport = await transportListener.AcceptTransportAsync(CancellationToken); await serverTransport.OpenAsync(ListenerUri, CancellationToken); await clientTransport.SendAsync(message, CancellationToken); var actual = await serverTransport.ReceiveAsync(CancellationToken); // Assert var actualMessage = actual.ShouldBeOfType <Message>(); CompareMessages(message, actualMessage); } finally { (clientTransport as IDisposable)?.Dispose(); (serverTransport as IDisposable)?.Dispose(); (transportListener as IDisposable)?.Dispose(); } }
public async Task TearDown() { _cts.Dispose(); using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5))) { await Task.WhenAll( _serverTransport.CloseAsync(cts.Token), _clientTransport.CloseAsync(cts.Token)); await _transportListener.StopAsync(cts.Token); } _clientTransport.DisposeIfDisposable(); _serverTransport.DisposeIfDisposable(); _transportListener.DisposeIfDisposable(); // Set null to avoid issues with NUnit reusing the test class instance _clientTransport = null; _serverTransport = null; _transportListener = null; }
public void Dispose() { _clientTransport.CloseAsync(CancellationToken.None); _serverTransport.CloseAsync(CancellationToken.None); _transportListener.StopAsync(_cancellationToken).Wait(); }
public async Task StopServerAsync() { await _listener.StopAsync(); }