Removing the connected event (#989)
This commit is contained in:
parent
ba1c210f03
commit
72514f3943
|
|
@ -39,12 +39,6 @@ namespace Microsoft.AspNetCore.SignalR.Client
|
||||||
|
|
||||||
private int _nextId = 0;
|
private int _nextId = 0;
|
||||||
|
|
||||||
public event Func<Task> Connected
|
|
||||||
{
|
|
||||||
add { _connection.Connected += value; }
|
|
||||||
remove { _connection.Connected -= value; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public event Func<Exception, Task> Closed
|
public event Func<Exception, Task> Closed
|
||||||
{
|
{
|
||||||
add { _connection.Closed += value; }
|
add { _connection.Closed += value; }
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ namespace Microsoft.AspNetCore.Sockets.Client
|
||||||
Task SendAsync(byte[] data, CancellationToken cancellationToken);
|
Task SendAsync(byte[] data, CancellationToken cancellationToken);
|
||||||
Task DisposeAsync();
|
Task DisposeAsync();
|
||||||
|
|
||||||
event Func<Task> Connected;
|
|
||||||
event Func<byte[], Task> Received;
|
event Func<byte[], Task> Received;
|
||||||
event Func<Exception, Task> Closed;
|
event Func<Exception, Task> Closed;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,6 @@ namespace Microsoft.AspNetCore.Sockets.Client
|
||||||
|
|
||||||
public IFeatureCollection Features { get; } = new FeatureCollection();
|
public IFeatureCollection Features { get; } = new FeatureCollection();
|
||||||
|
|
||||||
public event Func<Task> Connected;
|
|
||||||
public event Func<byte[], Task> Received;
|
public event Func<byte[], Task> Received;
|
||||||
public event Func<Exception, Task> Closed;
|
public event Func<Exception, Task> Closed;
|
||||||
|
|
||||||
|
|
@ -156,24 +155,6 @@ namespace Microsoft.AspNetCore.Sockets.Client
|
||||||
if (Interlocked.CompareExchange(ref _connectionState, ConnectionState.Connected, ConnectionState.Connecting)
|
if (Interlocked.CompareExchange(ref _connectionState, ConnectionState.Connected, ConnectionState.Connecting)
|
||||||
== ConnectionState.Connecting)
|
== ConnectionState.Connecting)
|
||||||
{
|
{
|
||||||
_ = _eventQueue.Enqueue(async () =>
|
|
||||||
{
|
|
||||||
_logger.RaiseConnected(_connectionId);
|
|
||||||
|
|
||||||
var connectedEventHandler = Connected;
|
|
||||||
if (connectedEventHandler != null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await connectedEventHandler.Invoke();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.ExceptionThrownFromHandler(_connectionId, nameof(Connected), ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
_ = Input.Completion.ContinueWith(async t =>
|
_ = Input.Completion.ContinueWith(async t =>
|
||||||
{
|
{
|
||||||
Interlocked.Exchange(ref _connectionState, ConnectionState.Disconnected);
|
Interlocked.Exchange(ref _connectionState, ConnectionState.Disconnected);
|
||||||
|
|
|
||||||
|
|
@ -188,81 +188,6 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
|
||||||
Assert.Equal("Cannot send messages when the connection is not in the Connected state.", exception.Message);
|
Assert.Equal("Cannot send messages when the connection is not in the Connected state.", exception.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task ConnectedEventRaisedWhenTheClientIsConnected()
|
|
||||||
{
|
|
||||||
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 request.Method == HttpMethod.Options
|
|
||||||
? ResponseUtils.CreateResponse(HttpStatusCode.OK, ResponseUtils.CreateNegotiationResponse())
|
|
||||||
: ResponseUtils.CreateResponse(HttpStatusCode.OK);
|
|
||||||
});
|
|
||||||
|
|
||||||
var connection = new HttpConnection(new Uri("http://fakeuri.org/"), TransportType.LongPolling, loggerFactory: null, httpMessageHandler: mockHttpHandler.Object);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var connectedEventRaisedTcs = new TaskCompletionSource<object>();
|
|
||||||
connection.Connected += () =>
|
|
||||||
{
|
|
||||||
connectedEventRaisedTcs.SetResult(null);
|
|
||||||
return Task.CompletedTask;
|
|
||||||
};
|
|
||||||
|
|
||||||
await connection.StartAsync();
|
|
||||||
|
|
||||||
await connectedEventRaisedTcs.Task.OrTimeout();
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
await connection.DisposeAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task ConnectedEventNotRaisedWhenTransportFailsToStart()
|
|
||||||
{
|
|
||||||
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 request.Method == HttpMethod.Options
|
|
||||||
? ResponseUtils.CreateResponse(HttpStatusCode.OK, ResponseUtils.CreateNegotiationResponse())
|
|
||||||
: ResponseUtils.CreateResponse(HttpStatusCode.OK);
|
|
||||||
});
|
|
||||||
|
|
||||||
var mockTransport = new Mock<ITransport>();
|
|
||||||
mockTransport.Setup(t => t.StartAsync(It.IsAny<Uri>(), It.IsAny<Channel<byte[], SendMessage>>(), It.IsAny<TransferMode>(), It.IsAny<string>()))
|
|
||||||
.Returns(Task.FromException(new InvalidOperationException("Transport failed to start")));
|
|
||||||
|
|
||||||
var connection = new HttpConnection(new Uri("http://fakeuri.org/"), new TestTransportFactory(mockTransport.Object), loggerFactory: null, httpMessageHandler: mockHttpHandler.Object);
|
|
||||||
var connectedEventRaised = false;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
connection.Connected += () =>
|
|
||||||
{
|
|
||||||
connectedEventRaised = true;
|
|
||||||
return Task.CompletedTask;
|
|
||||||
};
|
|
||||||
|
|
||||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
|
||||||
async () => await connection.StartAsync());
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
await connection.DisposeAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
Assert.False(connectedEventRaised);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ClosedEventRaisedWhenTheClientIsBeingStopped()
|
public async Task ClosedEventRaisedWhenTheClientIsBeingStopped()
|
||||||
{
|
{
|
||||||
|
|
@ -746,123 +671,6 @@ namespace Microsoft.AspNetCore.Sockets.Client.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task CanReceiveDataEvenIfUserThrowsInConnectedEvent()
|
|
||||||
{
|
|
||||||
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();
|
|
||||||
|
|
||||||
var content = string.Empty;
|
|
||||||
|
|
||||||
if (request.Method == HttpMethod.Get)
|
|
||||||
{
|
|
||||||
content = "42";
|
|
||||||
}
|
|
||||||
|
|
||||||
return request.Method == HttpMethod.Options
|
|
||||||
? ResponseUtils.CreateResponse(HttpStatusCode.OK, ResponseUtils.CreateNegotiationResponse())
|
|
||||||
: ResponseUtils.CreateResponse(HttpStatusCode.OK, content);
|
|
||||||
});
|
|
||||||
|
|
||||||
var connection = new HttpConnection(new Uri("http://fakeuri.org/"), TransportType.LongPolling, loggerFactory: null, httpMessageHandler: mockHttpHandler.Object);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
connection.Connected += () => Task.FromException(new InvalidOperationException());
|
|
||||||
|
|
||||||
var receiveTcs = new TaskCompletionSource<string>();
|
|
||||||
connection.Received += data =>
|
|
||||||
{
|
|
||||||
receiveTcs.TrySetResult(Encoding.UTF8.GetString(data));
|
|
||||||
return Task.CompletedTask;
|
|
||||||
};
|
|
||||||
|
|
||||||
connection.Closed += e =>
|
|
||||||
{
|
|
||||||
if (e != null)
|
|
||||||
{
|
|
||||||
receiveTcs.TrySetException(e);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
receiveTcs.TrySetCanceled();
|
|
||||||
}
|
|
||||||
return Task.CompletedTask;
|
|
||||||
};
|
|
||||||
|
|
||||||
await connection.StartAsync();
|
|
||||||
|
|
||||||
Assert.Equal("42", await receiveTcs.Task.OrTimeout());
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
await connection.DisposeAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task CanReceiveDataEvenIfUserThrowsSynchronouslyInConnectedEvent()
|
|
||||||
{
|
|
||||||
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();
|
|
||||||
|
|
||||||
var content = string.Empty;
|
|
||||||
|
|
||||||
if (request.Method == HttpMethod.Get)
|
|
||||||
{
|
|
||||||
content = "42";
|
|
||||||
}
|
|
||||||
|
|
||||||
return request.Method == HttpMethod.Options
|
|
||||||
? ResponseUtils.CreateResponse(HttpStatusCode.OK, ResponseUtils.CreateNegotiationResponse())
|
|
||||||
: ResponseUtils.CreateResponse(HttpStatusCode.OK, content);
|
|
||||||
});
|
|
||||||
|
|
||||||
var connection = new HttpConnection(new Uri("http://fakeuri.org/"), TransportType.LongPolling, loggerFactory: null, httpMessageHandler: mockHttpHandler.Object);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
connection.Connected += () =>
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException();
|
|
||||||
};
|
|
||||||
|
|
||||||
var receiveTcs = new TaskCompletionSource<string>();
|
|
||||||
connection.Received += data =>
|
|
||||||
{
|
|
||||||
receiveTcs.TrySetResult(Encoding.UTF8.GetString(data));
|
|
||||||
return Task.CompletedTask;
|
|
||||||
};
|
|
||||||
|
|
||||||
connection.Closed += e =>
|
|
||||||
{
|
|
||||||
if (e != null)
|
|
||||||
{
|
|
||||||
receiveTcs.TrySetException(e);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
receiveTcs.TrySetCanceled();
|
|
||||||
}
|
|
||||||
return Task.CompletedTask;
|
|
||||||
};
|
|
||||||
|
|
||||||
await connection.StartAsync();
|
|
||||||
|
|
||||||
Assert.Equal("42", await receiveTcs.Task.OrTimeout());
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
await connection.DisposeAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task CanReceiveDataEvenIfExceptionThrownFromPreviousReceivedEvent()
|
public async Task CanReceiveDataEvenIfExceptionThrownFromPreviousReceivedEvent()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -55,30 +55,6 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
|
||||||
Assert.Same(exception, actualException);
|
Assert.Same(exception, actualException);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task HubConnectionConnectedEventRaisedWhenTheClientIsConnected()
|
|
||||||
{
|
|
||||||
var connection = new TestConnection();
|
|
||||||
var hubConnection = new HubConnection(connection, Mock.Of<IHubProtocol>(), null);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var connectedEventRaisedTcs = new TaskCompletionSource<object>();
|
|
||||||
hubConnection.Connected += () =>
|
|
||||||
{
|
|
||||||
connectedEventRaisedTcs.SetResult(null);
|
|
||||||
return Task.CompletedTask;
|
|
||||||
};
|
|
||||||
|
|
||||||
await hubConnection.StartAsync();
|
|
||||||
|
|
||||||
await connectedEventRaisedTcs.Task.OrTimeout();
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
await hubConnection.DisposeAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ClosedEventRaisedWhenTheClientIsStopped()
|
public async Task ClosedEventRaisedWhenTheClientIsStopped()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue