コード例 #1
0
ファイル: Server.cs プロジェクト: rafaelmalheiros/lime-csharp
        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();
            }
        }
コード例 #2
0
        public async Task CleanupAsync()
        {
            await _transportListener.StopAsync(_cancellationToken);

            _serverTransport.CloseAsync(_cancellationToken);
            _clientTransport.CloseAsync(_cancellationToken);
        }
コード例 #3
0
        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();
            }
        }
コード例 #4
0
        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;
        }
コード例 #5
0
 public void Dispose()
 {
     _clientTransport.CloseAsync(CancellationToken.None);
     _serverTransport.CloseAsync(CancellationToken.None);
     _transportListener.StopAsync(_cancellationToken).Wait();
 }
コード例 #6
0
 public async Task StopServerAsync()
 {
     await _listener.StopAsync();
 }