Removing Dispose from Transport, Connection and HubConnection

Converting StopAsync to DisposeAsync
This commit is contained in:
moozzyk 2017-02-16 14:15:47 -08:00
parent 76bd114a2f
commit 7a4746868a
12 changed files with 372 additions and 335 deletions

View File

@ -30,7 +30,8 @@ namespace ClientSample
{ {
logger.LogInformation("Connecting to {0}", baseUrl); logger.LogInformation("Connecting to {0}", baseUrl);
var transport = new LongPollingTransport(httpClient, loggerFactory); var transport = new LongPollingTransport(httpClient, loggerFactory);
using (var connection = new HubConnection(new Uri(baseUrl), new JsonNetInvocationAdapter(), loggerFactory)) var connection = new HubConnection(new Uri(baseUrl), new JsonNetInvocationAdapter(), loggerFactory);
try
{ {
await connection.StartAsync(transport, httpClient); await connection.StartAsync(transport, httpClient);
logger.LogInformation("Connected to {0}", baseUrl); logger.LogInformation("Connected to {0}", baseUrl);
@ -58,6 +59,10 @@ namespace ClientSample
await connection.Invoke<object>("Send", line); await connection.Invoke<object>("Send", line);
} }
} }
finally
{
await connection.DisposeAsync();
}
} }
} }
} }

View File

@ -30,7 +30,8 @@ namespace ClientSample
{ {
logger.LogInformation("Connecting to {0}", baseUrl); logger.LogInformation("Connecting to {0}", baseUrl);
var transport = new LongPollingTransport(httpClient, loggerFactory); var transport = new LongPollingTransport(httpClient, loggerFactory);
using (var connection = new Connection(new Uri(baseUrl), loggerFactory)) var connection = new Connection(new Uri(baseUrl), loggerFactory);
try
{ {
var cts = new CancellationTokenSource(); var cts = new CancellationTokenSource();
connection.Received += (data, format) => logger.LogInformation($"Received: {Encoding.UTF8.GetString(data)}"); connection.Received += (data, format) => logger.LogInformation($"Received: {Encoding.UTF8.GetString(data)}");
@ -48,8 +49,10 @@ namespace ClientSample
}; };
await StartSending(loggerFactory.CreateLogger("SendLoop"), connection, cts.Token).ContinueWith(_ => cts.Cancel()); await StartSending(loggerFactory.CreateLogger("SendLoop"), connection, cts.Token).ContinueWith(_ => cts.Cancel());
}
await connection.StopAsync(); finally
{
await connection.DisposeAsync();
} }
} }
} }

View File

@ -16,7 +16,7 @@ using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.SignalR.Client namespace Microsoft.AspNetCore.SignalR.Client
{ {
public class HubConnection : IDisposable public class HubConnection
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly Connection _connection; private readonly Connection _connection;
@ -71,14 +71,9 @@ namespace Microsoft.AspNetCore.SignalR.Client
await _connection.StartAsync(transport, httpClient); await _connection.StartAsync(transport, httpClient);
} }
public async Task StopAsync() public async Task DisposeAsync()
{ {
await _connection.StopAsync(); await _connection.DisposeAsync();
}
public void Dispose()
{
_connection.Dispose();
} }
// TODO: Client return values/tasks? // TODO: Client return values/tasks?

View File

@ -12,7 +12,7 @@ using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Sockets.Client namespace Microsoft.AspNetCore.Sockets.Client
{ {
public class Connection : IDisposable public class Connection
{ {
private readonly ILoggerFactory _loggerFactory; private readonly ILoggerFactory _loggerFactory;
private readonly ILogger _logger; private readonly ILogger _logger;
@ -212,7 +212,7 @@ namespace Microsoft.AspNetCore.Sockets.Client
return false; return false;
} }
public async Task StopAsync() public async Task DisposeAsync()
{ {
Interlocked.Exchange(ref _connectionState, ConnectionState.Disconnected); Interlocked.Exchange(ref _connectionState, ConnectionState.Disconnected);
@ -232,21 +232,6 @@ namespace Microsoft.AspNetCore.Sockets.Client
} }
} }
public void Dispose()
{
Interlocked.Exchange(ref _connectionState, ConnectionState.Disconnected);
if (_transportChannel != null)
{
Output.TryComplete();
}
if (_transport != null)
{
_transport.Dispose();
}
}
private class ConnectionState private class ConnectionState
{ {
public const int Initial = 0; public const int Initial = 0;

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Sockets.Client namespace Microsoft.AspNetCore.Sockets.Client
{ {
public interface ITransport : IDisposable public interface ITransport
{ {
Task StartAsync(Uri url, IChannelConnection<Message> application); Task StartAsync(Uri url, IChannelConnection<Message> application);
Task StopAsync(); Task StopAsync();

View File

@ -42,7 +42,8 @@ namespace Microsoft.AspNetCore.Sockets.Client
_poller = Poll(Utils.AppendPath(url, "poll"), _transportCts.Token); _poller = Poll(Utils.AppendPath(url, "poll"), _transportCts.Token);
_sender = SendMessages(Utils.AppendPath(url, "send"), _transportCts.Token); _sender = SendMessages(Utils.AppendPath(url, "send"), _transportCts.Token);
Running = Task.WhenAll(_sender, _poller).ContinueWith(t => { Running = Task.WhenAll(_sender, _poller).ContinueWith(t =>
{
_application.Output.TryComplete(t.IsFaulted ? t.Exception.InnerException : null); _application.Output.TryComplete(t.IsFaulted ? t.Exception.InnerException : null);
return t; return t;
}).Unwrap(); }).Unwrap();
@ -53,12 +54,14 @@ namespace Microsoft.AspNetCore.Sockets.Client
public async Task StopAsync() public async Task StopAsync()
{ {
_transportCts.Cancel(); _transportCts.Cancel();
try
{
await Running; await Running;
} }
catch
public void Dispose()
{ {
_transportCts.Cancel(); // exceptions have been handled in the Running task continuation by closing the channel with the exception
}
} }
private async Task Poll(Uri pollUrl, CancellationToken cancellationToken) private async Task Poll(Uri pollUrl, CancellationToken cancellationToken)

View File

@ -44,6 +44,7 @@ namespace Microsoft.AspNetCore.Sockets.Client
} }
_application = application; _application = application;
await Connect(url); await Connect(url);
var sendTask = SendMessages(url, _cancellationToken); var sendTask = SendMessages(url, _cancellationToken);
var receiveTask = ReceiveMessages(url, _cancellationToken); var receiveTask = ReceiveMessages(url, _cancellationToken);
@ -154,14 +155,19 @@ namespace Microsoft.AspNetCore.Sockets.Client
await _webSocket.ConnectAsync(uriBuilder.Uri, _cancellationToken); await _webSocket.ConnectAsync(uriBuilder.Uri, _cancellationToken);
} }
public void Dispose()
{
_webSocket.Dispose();
}
public async Task StopAsync() public async Task StopAsync()
{ {
await _webSocket.CloseAsync(WebSocketCloseStatus.Empty, null, _cancellationToken); await _webSocket.CloseAsync(WebSocketCloseStatus.Empty, null, _cancellationToken);
_webSocket.Dispose();
try
{
await Running;
}
catch
{
// exceptions have been handled in the Running task continuation by closing the channel with the exception
}
} }
} }
} }

View File

@ -51,7 +51,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
using (var httpClient = _testServer.CreateClient()) using (var httpClient = _testServer.CreateClient())
{ {
var transport = new LongPollingTransport(httpClient, loggerFactory); var transport = new LongPollingTransport(httpClient, loggerFactory);
using (var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory)) var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory);
try
{ {
await connection.StartAsync(transport, httpClient); await connection.StartAsync(transport, httpClient);
@ -59,6 +60,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
Assert.Equal("Hello World!", result); Assert.Equal("Hello World!", result);
} }
finally
{
await connection.DisposeAsync();
}
} }
} }
@ -71,7 +76,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
using (var httpClient = _testServer.CreateClient()) using (var httpClient = _testServer.CreateClient())
{ {
var transport = new LongPollingTransport(httpClient, loggerFactory); var transport = new LongPollingTransport(httpClient, loggerFactory);
using (var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory)) var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory);
try
{ {
await connection.StartAsync(transport, httpClient); await connection.StartAsync(transport, httpClient);
@ -79,6 +85,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
Assert.Equal(originalMessage, result); Assert.Equal(originalMessage, result);
} }
finally
{
await connection.DisposeAsync();
}
} }
} }
@ -91,7 +101,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
using (var httpClient = _testServer.CreateClient()) using (var httpClient = _testServer.CreateClient())
{ {
var transport = new LongPollingTransport(httpClient, loggerFactory); var transport = new LongPollingTransport(httpClient, loggerFactory);
using (var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory)) var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory);
try
{ {
await connection.StartAsync(transport, httpClient); await connection.StartAsync(transport, httpClient);
@ -99,6 +110,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
Assert.Equal(originalMessage, result); Assert.Equal(originalMessage, result);
} }
finally
{
await connection.DisposeAsync();
}
} }
} }
@ -111,7 +126,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
using (var httpClient = _testServer.CreateClient()) using (var httpClient = _testServer.CreateClient())
{ {
var transport = new LongPollingTransport(httpClient, loggerFactory); var transport = new LongPollingTransport(httpClient, loggerFactory);
using (var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory)) var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory);
try
{ {
await connection.StartAsync(transport, httpClient); await connection.StartAsync(transport, httpClient);
@ -125,6 +141,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
Assert.Equal(originalMessage, await tcs.Task.OrTimeout()); Assert.Equal(originalMessage, await tcs.Task.OrTimeout());
} }
finally
{
await connection.DisposeAsync();
}
} }
} }
@ -136,7 +156,8 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
using (var httpClient = _testServer.CreateClient()) using (var httpClient = _testServer.CreateClient())
{ {
var transport = new LongPollingTransport(httpClient, loggerFactory); var transport = new LongPollingTransport(httpClient, loggerFactory);
using (var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory)) var connection = new HubConnection(new Uri("http://test/hubs"), new JsonNetInvocationAdapter(), loggerFactory);
try
{ {
await connection.StartAsync(transport, httpClient); await connection.StartAsync(transport, httpClient);
@ -145,6 +166,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.FunctionalTests
Assert.Equal(ex.Message, "Unknown hub method '!@#$%'"); Assert.Equal(ex.Message, "Unknown hub method '!@#$%'");
} }
finally
{
await connection.DisposeAsync();
}
} }
} }

View File

@ -64,7 +64,8 @@ namespace Microsoft.AspNetCore.SignalR.Tests
using (var httpClient = new HttpClient()) using (var httpClient = new HttpClient())
{ {
var transport = new LongPollingTransport(httpClient, loggerFactory); var transport = new LongPollingTransport(httpClient, loggerFactory);
using (var connection = new ClientConnection(new Uri(baseUrl + "/echo"), loggerFactory)) var connection = new ClientConnection(new Uri(baseUrl + "/echo"), loggerFactory);
try
{ {
var receiveTcs = new TaskCompletionSource<string>(); var receiveTcs = new TaskCompletionSource<string>();
connection.Received += (data, format) => receiveTcs.TrySetResult(Encoding.UTF8.GetString(data)); connection.Received += (data, format) => receiveTcs.TrySetResult(Encoding.UTF8.GetString(data));
@ -87,8 +88,10 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var receiveData = new ReceiveData(); var receiveData = new ReceiveData();
Assert.Equal(message, await receiveTcs.Task.OrTimeout()); Assert.Equal(message, await receiveTcs.Task.OrTimeout());
}
await connection.StopAsync(); finally
{
await connection.DisposeAsync();
} }
} }
} }
@ -111,7 +114,8 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var loggerFactory = new LoggerFactory(); var loggerFactory = new LoggerFactory();
var transport = new WebSocketsTransport(); var transport = new WebSocketsTransport();
using (var connection = new ClientConnection(new Uri(baseUrl + "/echo/ws"), loggerFactory)) var connection = new ClientConnection(new Uri(baseUrl + "/echo/ws"), loggerFactory);
try
{ {
var receiveTcs = new TaskCompletionSource<byte[]>(); var receiveTcs = new TaskCompletionSource<byte[]>();
connection.Received += (data, messageType) => receiveTcs.SetResult(data); connection.Received += (data, messageType) => receiveTcs.SetResult(data);
@ -124,8 +128,10 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var receivedData = await receiveTcs.Task.OrTimeout(); var receivedData = await receiveTcs.Task.OrTimeout();
Assert.Equal(message, Encoding.UTF8.GetString(receivedData)); Assert.Equal(message, Encoding.UTF8.GetString(receivedData));
}
await connection.StopAsync(); finally
{
await connection.DisposeAsync();
} }
} }
} }

View File

@ -31,12 +31,6 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
Assert.Equal(connectionUrl, new Connection(connectionUrl).Url); Assert.Equal(connectionUrl, new Connection(connectionUrl).Url);
} }
[Fact]
public void CanDisposeNotStartedConnection()
{
using (new Connection(new Uri("http://fakeuri.org"))) { }
}
[Fact] [Fact]
public async Task CannotStartRunningConnection() public async Task CannotStartRunningConnection()
{ {
@ -50,16 +44,21 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
using (var connection = new Connection(new Uri("http://fakeuri.org/"))) var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
try
{ {
await connection.StartAsync(longPollingTransport, httpClient); await connection.StartAsync(longPollingTransport, httpClient);
var exception = var exception =
await Assert.ThrowsAsync<InvalidOperationException>( await Assert.ThrowsAsync<InvalidOperationException>(
async () => await connection.StartAsync(longPollingTransport)); async () => await connection.StartAsync(longPollingTransport));
Assert.Equal("Cannot start a connection that is not in the Initial state.", exception.Message); Assert.Equal("Cannot start a connection that is not in the Initial state.", exception.Message);
}
await connection.StopAsync(); finally
{
await connection.DisposeAsync();
}
} }
} }
@ -76,11 +75,12 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
using (var connection = new Connection(new Uri("http://fakeuri.org/")))
{ {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
await connection.StartAsync(longPollingTransport, httpClient); await connection.StartAsync(longPollingTransport, httpClient);
await connection.StopAsync(); await connection.DisposeAsync();
var exception = var exception =
await Assert.ThrowsAsync<InvalidOperationException>( await Assert.ThrowsAsync<InvalidOperationException>(
async () => await connection.StartAsync(longPollingTransport)); async () => await connection.StartAsync(longPollingTransport));
@ -93,10 +93,10 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
public async Task CannotStartDisposedConnection() public async Task CannotStartDisposedConnection()
{ {
using (var httpClient = new HttpClient()) using (var httpClient = new HttpClient())
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
{ {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/")); var connection = new Connection(new Uri("http://fakeuri.org/"));
connection.Dispose(); await connection.DisposeAsync();
var exception = var exception =
await Assert.ThrowsAsync<InvalidOperationException>( await Assert.ThrowsAsync<InvalidOperationException>(
async () => await connection.StartAsync(longPollingTransport)); async () => await connection.StartAsync(longPollingTransport));
@ -108,14 +108,12 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
[Fact] [Fact]
public async Task SendReturnsFalseIfConnectionIsNotStarted() public async Task SendReturnsFalseIfConnectionIsNotStarted()
{ {
using (var connection = new Connection(new Uri("http://fakeuri.org/"))) var connection = new Connection(new Uri("http://fakeuri.org/"));
{
Assert.False(await connection.SendAsync(new byte[0], MessageType.Binary)); Assert.False(await connection.SendAsync(new byte[0], MessageType.Binary));
} }
}
[Fact] [Fact]
public async Task SendReturnsFalseIfConnectionIsStopped() public async Task SendReturnsFalseIfConnectionIsDisposed()
{ {
var mockHttpHandler = new Mock<HttpMessageHandler>(); var mockHttpHandler = new Mock<HttpMessageHandler>();
mockHttpHandler.Protected() mockHttpHandler.Protected()
@ -127,23 +125,16 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
using (var connection = new Connection(new Uri("http://fakeuri.org/")))
{
await connection.StartAsync(longPollingTransport, httpClient);
await connection.StopAsync();
Assert.False(await connection.SendAsync(new byte[0], MessageType.Binary));
}
}
[Fact]
public async Task SendReturnsFalseIfConnectionIsDisposed()
{ {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/")); var connection = new Connection(new Uri("http://fakeuri.org/"));
connection.Dispose();
await connection.StartAsync(longPollingTransport, httpClient);
await connection.DisposeAsync();
Assert.False(await connection.SendAsync(new byte[0], MessageType.Binary)); Assert.False(await connection.SendAsync(new byte[0], MessageType.Binary));
} }
}
[Fact] [Fact]
public async Task ConnectedEventRaisedWhenTheClientIsConnected() public async Task ConnectedEventRaisedWhenTheClientIsConnected()
@ -158,8 +149,10 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
using (var connection = new Connection(new Uri("http://fakeuri.org/"))) var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
try
{ {
var connectedEventRaised = false; var connectedEventRaised = false;
connection.Connected += () => connectedEventRaised = true; connection.Connected += () => connectedEventRaised = true;
@ -168,6 +161,12 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
Assert.True(connectedEventRaised); Assert.True(connectedEventRaised);
} }
finally
{
await connection.DisposeAsync();
}
}
} }
[Fact] [Fact]
@ -187,13 +186,21 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
.Returns(Task.FromException(new InvalidOperationException("Transport failed to start"))); .Returns(Task.FromException(new InvalidOperationException("Transport failed to start")));
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var connection = new Connection(new Uri("http://fakeuri.org/")))
{ {
var connection = new Connection(new Uri("http://fakeuri.org/"));
var connectedEventRaised = false; var connectedEventRaised = false;
try
{
connection.Connected += () => connectedEventRaised = true; connection.Connected += () => connectedEventRaised = true;
await Assert.ThrowsAsync<InvalidOperationException>( await Assert.ThrowsAsync<InvalidOperationException>(
async () => await connection.StartAsync(mockTransport.Object, httpClient)); async () => await connection.StartAsync(mockTransport.Object, httpClient));
}
finally
{
await connection.DisposeAsync();
}
Assert.False(connectedEventRaised); Assert.False(connectedEventRaised);
} }
@ -212,47 +219,18 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
using (var connection = new Connection(new Uri("http://fakeuri.org/")))
{ {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
var closedEventTcs = new TaskCompletionSource<Exception>(); var closedEventTcs = new TaskCompletionSource<Exception>();
connection.Closed += e => closedEventTcs.SetResult(e); connection.Closed += e => closedEventTcs.SetResult(e);
await connection.StartAsync(longPollingTransport, httpClient); await connection.StartAsync(longPollingTransport, httpClient);
await connection.StopAsync(); await connection.DisposeAsync();
Assert.Equal(closedEventTcs.Task, await Task.WhenAny(Task.Delay(1000), closedEventTcs.Task));
// in case of clean disconnect error should be null // in case of clean disconnect error should be null
Assert.Null(await closedEventTcs.Task); Assert.Null(await closedEventTcs.Task.OrTimeout());
}
}
[Fact]
public async Task ClosedEventRaisedWhenTheClientIsDisposed()
{
var mockHttpHandler = new Mock<HttpMessageHandler>();
mockHttpHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.Returns<HttpRequestMessage, CancellationToken>(async (request, cancellationToken) =>
{
await Task.Yield();
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(string.Empty) };
});
using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
{
var connection = new Connection(new Uri("http://fakeuri.org/"));
var closedEventTcs = new TaskCompletionSource<Exception>();
connection.Closed += e => closedEventTcs.TrySetResult(e);
using (connection)
{
await connection.StartAsync(longPollingTransport, httpClient);
}
Assert.Equal(closedEventTcs.Task, await Task.WhenAny(Task.Delay(1000), closedEventTcs.Task));
Assert.Null(await closedEventTcs.Task);
} }
} }
@ -273,30 +251,35 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
using (var connection = new Connection(new Uri("http://fakeuri.org/")))
{ {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
var closedEventTcs = new TaskCompletionSource<Exception>(); var closedEventTcs = new TaskCompletionSource<Exception>();
connection.Closed += e => closedEventTcs.TrySetResult(e); connection.Closed += e => closedEventTcs.TrySetResult(e);
await connection.StartAsync(longPollingTransport, httpClient);
Assert.Equal(closedEventTcs.Task, await Task.WhenAny(Task.Delay(1000), closedEventTcs.Task)); try
Assert.IsType<HttpRequestException>(await closedEventTcs.Task); {
await connection.StartAsync(longPollingTransport, httpClient);
Assert.IsType<HttpRequestException>(await closedEventTcs.Task.OrTimeout());
}
finally
{
await connection.DisposeAsync();
}
} }
} }
[Fact] [Fact]
public async Task ClosedEventNotRaisedWhenTheClientIsStoppedButWasNeverStarted() public async Task ClosedEventNotRaisedWhenTheClientIsStoppedButWasNeverStarted()
{ {
using (var connection = new Connection(new Uri("http://fakeuri.org/"))) var connection = new Connection(new Uri("http://fakeuri.org/"));
{
bool closedEventRaised = false; bool closedEventRaised = false;
connection.Closed += e => closedEventRaised = true; connection.Closed += e => closedEventRaised = true;
await connection.StopAsync(); await connection.DisposeAsync();
Assert.False(closedEventRaised); Assert.False(closedEventRaised);
} }
}
[Fact] [Fact]
public async Task TransportIsStoppedWhenConnectionIsStopped() public async Task TransportIsStoppedWhenConnectionIsStopped()
@ -311,39 +294,19 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
using (var connection = new Connection(new Uri("http://fakeuri.org/"))) var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
try
{ {
await connection.StartAsync(longPollingTransport, httpClient); await connection.StartAsync(longPollingTransport, httpClient);
Assert.False(longPollingTransport.Running.IsCompleted); Assert.False(longPollingTransport.Running.IsCompleted);
await connection.StopAsync();
await longPollingTransport.Running.OrTimeout();
} }
} finally
[Fact]
public async Task TransportIsClosedWhenConnectionIsDisposed()
{ {
var mockHttpHandler = new Mock<HttpMessageHandler>(); await connection.DisposeAsync();
mockHttpHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.Returns<HttpRequestMessage, CancellationToken>(async (request, cancellationToken) =>
{
await Task.Yield();
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(string.Empty) };
});
using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
{
using (var connection = new Connection(new Uri("http://fakeuri.org/")))
{
await connection.StartAsync(longPollingTransport, httpClient);
Assert.False(longPollingTransport.Running.IsCompleted);
} }
await longPollingTransport.Running.OrTimeout(); await longPollingTransport.Running.OrTimeout();
@ -368,8 +331,11 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
using (var connection = new Connection(new Uri("http://fakeuri.org/")))
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
try
{ {
await connection.StartAsync(longPollingTransport, httpClient); await connection.StartAsync(longPollingTransport, httpClient);
@ -377,8 +343,11 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
await connection.SendAsync(data, MessageType.Binary); await connection.SendAsync(data, MessageType.Binary);
Assert.Equal(data, await sendTcs.Task.OrTimeout()); Assert.Equal(data, await sendTcs.Task.OrTimeout());
}
await connection.StopAsync(); finally
{
await connection.DisposeAsync();
}
} }
} }
@ -401,8 +370,11 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
using (var connection = new Connection(new Uri("http://fakeuri.org/")))
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
try
{ {
var receiveTcs = new TaskCompletionSource<string>(); var receiveTcs = new TaskCompletionSource<string>();
connection.Received += (data, format) => receiveTcs.TrySetResult(Encoding.UTF8.GetString(data)); connection.Received += (data, format) => receiveTcs.TrySetResult(Encoding.UTF8.GetString(data));
@ -420,10 +392,12 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
await connection.StartAsync(longPollingTransport, httpClient); await connection.StartAsync(longPollingTransport, httpClient);
// TODO: timeout Assert.Equal("42", await receiveTcs.Task.OrTimeout());
Assert.Equal("42", await receiveTcs.Task); }
finally
await connection.StopAsync(); {
await connection.DisposeAsync();
}
} }
} }
@ -440,11 +414,12 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
using (var connection = new Connection(new Uri("http://fakeuri.org/")))
{ {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
await connection.StartAsync(longPollingTransport, httpClient); await connection.StartAsync(longPollingTransport, httpClient);
await connection.StopAsync(); await connection.DisposeAsync();
Assert.False(await connection.SendAsync(new byte[] { 1, 1, 3, 5, 8 }, MessageType.Binary)); Assert.False(await connection.SendAsync(new byte[] { 1, 1, 3, 5, 8 }, MessageType.Binary));
} }
} }
@ -466,8 +441,10 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
using (var connection = new Connection(new Uri("http://fakeuri.org/"))) var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
try
{ {
var closeTcs = new TaskCompletionSource<Exception>(); var closeTcs = new TaskCompletionSource<Exception>();
connection.Closed += e => closeTcs.TrySetResult(e); connection.Closed += e => closeTcs.TrySetResult(e);
@ -479,6 +456,11 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
Assert.False(await connection.SendAsync(new byte[] { 1, 1, 3, 5, 8 }, MessageType.Binary)); Assert.False(await connection.SendAsync(new byte[] { 1, 1, 3, 5, 8 }, MessageType.Binary));
} }
finally
{
await connection.DisposeAsync();
}
}
} }
[Fact] [Fact]
@ -498,8 +480,10 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
using (var connection = new Connection(new Uri("http://fakeuri.org/"))) var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var connection = new Connection(new Uri("http://fakeuri.org/"));
try
{ {
var closeTcs = new TaskCompletionSource<Exception>(); var closeTcs = new TaskCompletionSource<Exception>();
connection.Closed += e => closeTcs.TrySetResult(e); connection.Closed += e => closeTcs.TrySetResult(e);
@ -511,6 +495,11 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
Assert.False(await connection.SendAsync(new byte[] { 1, 1, 3, 5, 8 }, MessageType.Binary)); Assert.False(await connection.SendAsync(new byte[] { 1, 1, 3, 5, 8 }, MessageType.Binary));
} }
finally
{
await connection.DisposeAsync();
}
}
} }
} }
} }

View File

@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Sockets; using Microsoft.AspNetCore.SignalR.Tests.Common;
using Microsoft.AspNetCore.Sockets.Client; using Microsoft.AspNetCore.Sockets.Client;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Moq; using Moq;
@ -23,19 +23,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
} }
[Fact] [Fact]
public void CanDisposeNotStartedHubConnection() public async Task CanDisposeNotStartedHubConnection()
{ {
using (new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), Mock.Of<ILoggerFactory>())) await new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), Mock.Of<ILoggerFactory>())
{ } .DisposeAsync();
}
[Fact]
public async Task CanStopNotStartedHubConnection()
{
using (var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), Mock.Of<ILoggerFactory>()))
{
await hubConnection.StopAsync();
}
} }
[Fact] [Fact]
@ -51,16 +42,22 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
using (var hubConnection = new HubConnection(new Uri("http://fakeuri.org/"), Mock.Of<IInvocationAdapter>(), new LoggerFactory())) var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var hubConnection = new HubConnection(new Uri("http://fakeuri.org/"), Mock.Of<IInvocationAdapter>(), new LoggerFactory());
try
{ {
await hubConnection.StartAsync(longPollingTransport, httpClient); await hubConnection.StartAsync(longPollingTransport, httpClient);
var exception = var exception =
await Assert.ThrowsAsync<InvalidOperationException>( await Assert.ThrowsAsync<InvalidOperationException>(
async () => await hubConnection.StartAsync(longPollingTransport)); async () => await hubConnection.StartAsync(longPollingTransport));
Assert.Equal("Cannot start a connection that is not in the Initial state.", exception.Message); Assert.Equal("Cannot start a connection that is not in the Initial state.", exception.Message);
}
await hubConnection.StopAsync(); finally
{
await hubConnection.DisposeAsync();
}
} }
} }
@ -77,27 +74,12 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
using (var hubConnection = new HubConnection(new Uri("http://fakeuri.org/"), Mock.Of<IInvocationAdapter>(), new LoggerFactory()))
{
await hubConnection.StartAsync(longPollingTransport, httpClient);
await hubConnection.StopAsync();
var exception =
await Assert.ThrowsAsync<InvalidOperationException>(
async () => await hubConnection.StartAsync(longPollingTransport));
Assert.Equal("Cannot start a connection that is not in the Initial state.", exception.Message);
}
}
[Fact]
public async Task CannotStartDisposedHubConnection()
{
using (var httpClient = new HttpClient())
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
{ {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var hubConnection = new HubConnection(new Uri("http://fakeuri.org/"), Mock.Of<IInvocationAdapter>(), new LoggerFactory()); var hubConnection = new HubConnection(new Uri("http://fakeuri.org/"), Mock.Of<IInvocationAdapter>(), new LoggerFactory());
hubConnection.Dispose();
await hubConnection.StartAsync(longPollingTransport, httpClient);
await hubConnection.DisposeAsync();
var exception = var exception =
await Assert.ThrowsAsync<InvalidOperationException>( await Assert.ThrowsAsync<InvalidOperationException>(
async () => await hubConnection.StartAsync(longPollingTransport)); async () => await hubConnection.StartAsync(longPollingTransport));
@ -109,25 +91,22 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
[Fact(Skip = "Not implemented")] [Fact(Skip = "Not implemented")]
public async Task InvokeThrowsIfHubConnectionNotStarted() public async Task InvokeThrowsIfHubConnectionNotStarted()
{ {
using (var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), Mock.Of<ILoggerFactory>())) var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), Mock.Of<ILoggerFactory>());
{
var exception = var exception =
await Assert.ThrowsAsync<InvalidOperationException>(async () => await hubConnection.Invoke<int>("test")); await Assert.ThrowsAsync<InvalidOperationException>(async () => await hubConnection.Invoke<int>("test"));
Assert.Equal("Cannot invoke methods on non-started connections.", exception.Message); Assert.Equal("Cannot invoke methods on non-started connections.", exception.Message);
} }
}
[Fact(Skip = "Not implemented")] [Fact(Skip = "Not implemented")]
public async Task InvokeThrowsIfHubConnectionDisposed() public async Task InvokeThrowsIfHubConnectionDisposed()
{ {
using (var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), Mock.Of<ILoggerFactory>())) var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), Mock.Of<ILoggerFactory>());
{ await hubConnection.DisposeAsync();
hubConnection.Dispose();
var exception = var exception =
await Assert.ThrowsAsync<InvalidOperationException>(async () => await hubConnection.Invoke<int>("test")); await Assert.ThrowsAsync<InvalidOperationException>(async () => await hubConnection.Invoke<int>("test"));
Assert.Equal("Cannot invoke methods on disposed connections.", exception.Message); Assert.Equal("Cannot invoke methods on disposed connections.", exception.Message);
} }
}
// TODO: If HubConnection takes (I)Connection we could just tests if events are wired up // TODO: If HubConnection takes (I)Connection we could just tests if events are wired up
@ -144,8 +123,10 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
using (var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), new LoggerFactory())) var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), new LoggerFactory());
try
{ {
var connectedEventRaised = false; var connectedEventRaised = false;
hubConnection.Connected += () => connectedEventRaised = true; hubConnection.Connected += () => connectedEventRaised = true;
@ -154,6 +135,11 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
Assert.True(connectedEventRaised); Assert.True(connectedEventRaised);
} }
finally
{
await hubConnection.DisposeAsync();
}
}
} }
[Fact] [Fact]
@ -169,19 +155,16 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory()))
using (var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), new LoggerFactory()))
{ {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
var hubConnection = new HubConnection(new Uri("http://fakeuri.org"), Mock.Of<IInvocationAdapter>(), new LoggerFactory());
var closedEventTcs = new TaskCompletionSource<Exception>(); var closedEventTcs = new TaskCompletionSource<Exception>();
hubConnection.Closed += e => closedEventTcs.SetResult(e); hubConnection.Closed += e => closedEventTcs.SetResult(e);
await hubConnection.StartAsync(longPollingTransport, httpClient); await hubConnection.StartAsync(longPollingTransport, httpClient);
await hubConnection.StopAsync(); await hubConnection.DisposeAsync();
Assert.Null(await closedEventTcs.Task.OrTimeout());
Assert.Equal(closedEventTcs.Task, await Task.WhenAny(Task.Delay(1000), closedEventTcs.Task));
// in case of clean disconnect error should be null
Assert.Null(await closedEventTcs.Task);
} }
} }
} }

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
public class LongPollingTransportTests public class LongPollingTransportTests
{ {
[Fact] [Fact]
public async Task LongPollingTransportStopsPollAndSendLoopsWhenTransportDisposed() public async Task LongPollingTransportStopsPollAndSendLoopsWhenTransportStopped()
{ {
var mockHttpHandler = new Mock<HttpMessageHandler>(); var mockHttpHandler = new Mock<HttpMessageHandler>();
mockHttpHandler.Protected() mockHttpHandler.Protected()
@ -33,7 +33,10 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
Task transportActiveTask; Task transportActiveTask;
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
try
{ {
var connectionToTransport = Channel.CreateUnbounded<Message>(); var connectionToTransport = Channel.CreateUnbounded<Message>();
var transportToConnection = Channel.CreateUnbounded<Message>(); var transportToConnection = Channel.CreateUnbounded<Message>();
@ -44,9 +47,14 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
Assert.False(transportActiveTask.IsCompleted); Assert.False(transportActiveTask.IsCompleted);
} }
finally
{
await longPollingTransport.StopAsync();
}
await transportActiveTask.OrTimeout(); await transportActiveTask.OrTimeout();
} }
}
[Fact] [Fact]
public async Task LongPollingTransportStopsWhenPollReceives204() public async Task LongPollingTransportStopsWhenPollReceives204()
@ -61,7 +69,10 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
try
{ {
var connectionToTransport = Channel.CreateUnbounded<Message>(); var connectionToTransport = Channel.CreateUnbounded<Message>();
var transportToConnection = Channel.CreateUnbounded<Message>(); var transportToConnection = Channel.CreateUnbounded<Message>();
@ -71,6 +82,11 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
await longPollingTransport.Running.OrTimeout(); await longPollingTransport.Running.OrTimeout();
Assert.True(transportToConnection.In.Completion.IsCompleted); Assert.True(transportToConnection.In.Completion.IsCompleted);
} }
finally
{
await longPollingTransport.StopAsync();
}
}
} }
[Fact] [Fact]
@ -86,7 +102,9 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
try
{ {
var connectionToTransport = Channel.CreateUnbounded<Message>(); var connectionToTransport = Channel.CreateUnbounded<Message>();
var transportToConnection = Channel.CreateUnbounded<Message>(); var transportToConnection = Channel.CreateUnbounded<Message>();
@ -97,6 +115,11 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
await Assert.ThrowsAsync<HttpRequestException>(async () => await transportToConnection.In.Completion.OrTimeout()); await Assert.ThrowsAsync<HttpRequestException>(async () => await transportToConnection.In.Completion.OrTimeout());
Assert.Contains(" 500 ", exception.Message); Assert.Contains(" 500 ", exception.Message);
} }
finally
{
await longPollingTransport.StopAsync();
}
}
} }
[Fact] [Fact]
@ -115,7 +138,9 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
try
{ {
var connectionToTransport = Channel.CreateUnbounded<Message>(); var connectionToTransport = Channel.CreateUnbounded<Message>();
var transportToConnection = Channel.CreateUnbounded<Message>(); var transportToConnection = Channel.CreateUnbounded<Message>();
@ -135,6 +160,11 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
var exception = await Assert.ThrowsAsync<HttpRequestException>(async () => await transportToConnection.In.Completion); var exception = await Assert.ThrowsAsync<HttpRequestException>(async () => await transportToConnection.In.Completion);
Assert.Contains(" 500 ", exception.Message); Assert.Contains(" 500 ", exception.Message);
} }
finally
{
await longPollingTransport.StopAsync();
}
}
} }
[Fact] [Fact]
@ -150,7 +180,9 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
}); });
using (var httpClient = new HttpClient(mockHttpHandler.Object)) using (var httpClient = new HttpClient(mockHttpHandler.Object))
using (var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory())) {
var longPollingTransport = new LongPollingTransport(httpClient, new LoggerFactory());
try
{ {
var connectionToTransport = Channel.CreateUnbounded<Message>(); var connectionToTransport = Channel.CreateUnbounded<Message>();
var transportToConnection = Channel.CreateUnbounded<Message>(); var transportToConnection = Channel.CreateUnbounded<Message>();
@ -164,6 +196,11 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
await longPollingTransport.Running.OrTimeout(); await longPollingTransport.Running.OrTimeout();
await connectionToTransport.In.Completion.OrTimeout(); await connectionToTransport.In.Completion.OrTimeout();
} }
finally
{
await longPollingTransport.StopAsync();
}
}
} }
} }
} }