Send delete request after poll ends (#2020)
* Added test and fixed other test
This commit is contained in:
parent
0f8485cafc
commit
d6395a52bc
|
|
@ -88,16 +88,16 @@ namespace Microsoft.AspNetCore.Http.Connections.Client.Internal
|
||||||
// Set the sending error so we communicate that to the application
|
// Set the sending error so we communicate that to the application
|
||||||
_error = sending.IsFaulted ? sending.Exception.InnerException : null;
|
_error = sending.IsFaulted ? sending.Exception.InnerException : null;
|
||||||
|
|
||||||
// Send the DELETE request to clean-up the connection on the server.
|
// Cancel the poll request
|
||||||
// This will also cause the poll to return.
|
|
||||||
await SendDeleteRequest(url);
|
|
||||||
|
|
||||||
_transportCts.Cancel();
|
_transportCts.Cancel();
|
||||||
|
|
||||||
// Cancel any pending flush so that we can quit
|
// Cancel any pending flush so that we can quit
|
||||||
_application.Output.CancelPendingFlush();
|
_application.Output.CancelPendingFlush();
|
||||||
|
|
||||||
await receiving;
|
await receiving;
|
||||||
|
|
||||||
|
// Send the DELETE request to clean-up the connection on the server.
|
||||||
|
await SendDeleteRequest(url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -380,12 +380,13 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
|
||||||
}
|
}
|
||||||
else if (request.Method == HttpMethod.Get)
|
else if (request.Method == HttpMethod.Get)
|
||||||
{
|
{
|
||||||
|
cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken));
|
||||||
// This is the poll task
|
// This is the poll task
|
||||||
return await tcs.Task;
|
return await tcs.Task;
|
||||||
}
|
}
|
||||||
else if (request.Method == HttpMethod.Delete)
|
else if (request.Method == HttpMethod.Delete)
|
||||||
{
|
{
|
||||||
tcs.TrySetResult(ResponseUtils.CreateResponse(HttpStatusCode.NoContent));
|
return ResponseUtils.CreateResponse(HttpStatusCode.Accepted);
|
||||||
}
|
}
|
||||||
return ResponseUtils.CreateResponse(HttpStatusCode.OK);
|
return ResponseUtils.CreateResponse(HttpStatusCode.OK);
|
||||||
});
|
});
|
||||||
|
|
@ -419,6 +420,57 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task LongPollingTransportSendsDeleteAfterPollEnds()
|
||||||
|
{
|
||||||
|
var sentRequests = new List<byte[]>();
|
||||||
|
var pollTcs = new TaskCompletionSource<HttpResponseMessage>();
|
||||||
|
var deleteTcs = new TaskCompletionSource<object>();
|
||||||
|
|
||||||
|
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();
|
||||||
|
if (request.Method == HttpMethod.Post)
|
||||||
|
{
|
||||||
|
// Build a new request object, but convert the entire payload to string
|
||||||
|
sentRequests.Add(await request.Content.ReadAsByteArrayAsync());
|
||||||
|
}
|
||||||
|
else if (request.Method == HttpMethod.Get)
|
||||||
|
{
|
||||||
|
cancellationToken.Register(() => pollTcs.TrySetCanceled(cancellationToken));
|
||||||
|
// This is the poll task
|
||||||
|
return await pollTcs.Task;
|
||||||
|
}
|
||||||
|
else if (request.Method == HttpMethod.Delete)
|
||||||
|
{
|
||||||
|
// The poll task should have been completed
|
||||||
|
Assert.True(pollTcs.Task.IsCompleted);
|
||||||
|
|
||||||
|
deleteTcs.TrySetResult(null);
|
||||||
|
|
||||||
|
return ResponseUtils.CreateResponse(HttpStatusCode.Accepted);
|
||||||
|
}
|
||||||
|
return ResponseUtils.CreateResponse(HttpStatusCode.OK);
|
||||||
|
});
|
||||||
|
|
||||||
|
using (var httpClient = new HttpClient(mockHttpHandler.Object))
|
||||||
|
{
|
||||||
|
var longPollingTransport = new LongPollingTransport(httpClient);
|
||||||
|
|
||||||
|
// Start the transport
|
||||||
|
await longPollingTransport.StartAsync(TestUri, TransferFormat.Binary);
|
||||||
|
|
||||||
|
var task = longPollingTransport.StopAsync();
|
||||||
|
|
||||||
|
await deleteTcs.Task.OrTimeout();
|
||||||
|
|
||||||
|
await task.OrTimeout();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(TransferFormat.Binary)]
|
[InlineData(TransferFormat.Binary)]
|
||||||
[InlineData(TransferFormat.Text)]
|
[InlineData(TransferFormat.Text)]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue