Add test for canceling start in transport (#12846)
This commit is contained in:
parent
a00bafd70f
commit
e4b4476b37
|
|
@ -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)
|
private static async Task AssertDisposedAsync(HttpConnection connection)
|
||||||
{
|
{
|
||||||
var exception =
|
var exception =
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
|
||||||
var firstPoll = true;
|
var firstPoll = true;
|
||||||
OnRequest(async (request, next, cancellationToken) =>
|
OnRequest(async (request, next, cancellationToken) =>
|
||||||
{
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
if (ResponseUtils.IsLongPollRequest(request) && firstPoll)
|
if (ResponseUtils.IsLongPollRequest(request) && firstPoll)
|
||||||
{
|
{
|
||||||
firstPoll = false;
|
firstPoll = false;
|
||||||
|
|
@ -156,6 +157,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
|
||||||
{
|
{
|
||||||
OnRequest((request, next, cancellationToken) =>
|
OnRequest((request, next, cancellationToken) =>
|
||||||
{
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
if (request.Method.Equals(method) && string.Equals(request.RequestUri.PathAndQuery, pathAndQuery))
|
if (request.Method.Equals(method) && string.Equals(request.RequestUri.PathAndQuery, pathAndQuery))
|
||||||
{
|
{
|
||||||
return handler(request, cancellationToken);
|
return handler(request, cancellationToken);
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,9 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
|
||||||
Application = pair.Application;
|
Application = pair.Application;
|
||||||
await _startHandler();
|
await _startHandler();
|
||||||
|
|
||||||
|
// To test canceling the token from the onTransportStart Func.
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
// Start a loop to read from the pipe
|
// Start a loop to read from the pipe
|
||||||
Receiving = ReceiveLoop();
|
Receiving = ReceiveLoop();
|
||||||
async Task ReceiveLoop()
|
async Task ReceiveLoop()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue