Add test for canceling start in transport (#12846)

This commit is contained in:
Mikael Mengistu 2019-08-08 20:14:56 -07:00 committed by GitHub
parent a00bafd70f
commit e4b4476b37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 0 deletions

View File

@ -427,6 +427,89 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
}
}
[Fact]
public async Task CancellationTokenFromStartPassedToTransport()
{
using (StartVerifiableLog())
{
var cts = new CancellationTokenSource();
var httpHandler = new TestHttpMessageHandler();
await WithConnectionAsync(
CreateConnection(httpHandler,
transport: new TestTransport(onTransportStart: () => {
// Cancel the token when the transport is starting which will fail the startTask.
cts.Cancel();
return Task.CompletedTask;
})),
async (connection) =>
{
// We aggregate failures that happen when we start the transport. The operation cancelled exception will
// be an inner exception.
var ex = await Assert.ThrowsAsync<AggregateException>(async () => await connection.StartAsync(cts.Token)).OrTimeout();
Assert.Equal(3, ex.InnerExceptions.Count);
var innerEx = ex.InnerExceptions[2];
var innerInnerEx = innerEx.InnerException;
Assert.IsType<OperationCanceledException>(innerInnerEx);
});
}
}
[Fact]
public async Task SSECanBeCanceled()
{
bool ExpectedErrors(WriteContext writeContext)
{
return writeContext.LoggerName == typeof(HttpConnection).FullName &&
writeContext.EventId.Name == "ErrorStartingTransport";
}
using (StartVerifiableLog(expectedErrorsFilter: ExpectedErrors))
{
var httpHandler = new TestHttpMessageHandler();
httpHandler.OnGet("/?id=00000000-0000-0000-0000-000000000000", (_, __) =>
{
// Simulating a cancellationToken canceling this request.
throw new OperationCanceledException("Cancel SSE Start.");
});
var sse = new ServerSentEventsTransport(new HttpClient(httpHandler), LoggerFactory);
await WithConnectionAsync(
CreateConnection(httpHandler, loggerFactory: LoggerFactory, transport: sse, transportType: HttpTransportType.ServerSentEvents),
async (connection) =>
{
var ex = await Assert.ThrowsAsync<AggregateException>(async () => await connection.StartAsync()).OrTimeout();
});
}
}
[Fact]
public async Task LongPollingTransportCanBeCanceled()
{
using (StartVerifiableLog())
{
var cts = new CancellationTokenSource();
var httpHandler = new TestHttpMessageHandler(autoNegotiate: false);
httpHandler.OnNegotiate((request, cancellationToken) =>
{
// Cancel token so that the first request poll will throw
cts.Cancel();
return ResponseUtils.CreateResponse(HttpStatusCode.OK, ResponseUtils.CreateNegotiationContent());
});
var lp = new LongPollingTransport(new HttpClient(httpHandler));
await WithConnectionAsync(
CreateConnection(httpHandler, transport: lp, transportType: HttpTransportType.LongPolling),
async (connection) =>
{
var ex = await Assert.ThrowsAsync<AggregateException>(async () => await connection.StartAsync(cts.Token).OrTimeout());
});
}
}
private static async Task AssertDisposedAsync(HttpConnection connection)
{
var exception =

View File

@ -46,6 +46,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
var firstPoll = true;
OnRequest(async (request, next, cancellationToken) =>
{
cancellationToken.ThrowIfCancellationRequested();
if (ResponseUtils.IsLongPollRequest(request) && firstPoll)
{
firstPoll = false;
@ -156,6 +157,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
{
OnRequest((request, next, cancellationToken) =>
{
cancellationToken.ThrowIfCancellationRequested();
if (request.Method.Equals(method) && string.Equals(request.RequestUri.PathAndQuery, pathAndQuery))
{
return handler(request, cancellationToken);

View File

@ -44,6 +44,9 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
Application = pair.Application;
await _startHandler();
// To test canceling the token from the onTransportStart Func.
cancellationToken.ThrowIfCancellationRequested();
// Start a loop to read from the pipe
Receiving = ReceiveLoop();
async Task ReceiveLoop()