Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ async Task OnStreamCanceled(InvocationRequest irq)
{
Log.SendingCancellation(_logger, irq.InvocationId);

await SendHubMessage(_state.CurrentConnectionStateUnsynchronized, new CancelInvocationMessage(irq.InvocationId), irq.CancellationToken).ConfigureAwait(false);
// Don't pass irq.CancellationToken, that would result in canceling the Flush and a delayed CancelInvocationMessage being sent.
await SendHubMessage(_state.CurrentConnectionStateUnsynchronized, new CancelInvocationMessage(irq.InvocationId), cancellationToken: default).ConfigureAwait(false);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,40 @@ await connection.ReceiveJsonMessage(
}
}

[Fact]
public async Task CanCancelTokenDuringStream_SendsCancelInvocation()
{
using (StartVerifiableLog())
{
var connection = new TestConnection();
var hubConnection = CreateHubConnection(connection, loggerFactory: LoggerFactory);

await hubConnection.StartAsync().DefaultTimeout();

using var cts = new CancellationTokenSource();
var asyncEnumerable = hubConnection.StreamAsync<int>("Stream", 1, cts.Token);

await using var e = asyncEnumerable.GetAsyncEnumerator(cts.Token);
var task = e.MoveNextAsync();

var item = await connection.ReadSentJsonAsync().DefaultTimeout();
var invocationId = item["invocationId"];
await connection.ReceiveJsonMessage(
new { type = HubProtocolConstants.StreamItemMessageType, invocationId, item = 1 }
).DefaultTimeout();

await task.DefaultTimeout();
cts.Cancel();

item = await connection.ReadSentJsonAsync().DefaultTimeout();
Assert.Equal(HubProtocolConstants.CancelInvocationMessageType, item["type"]);
Assert.Equal(invocationId, item["invocationId"]);

// Stream on client-side completes on cancellation
await Assert.ThrowsAsync<TaskCanceledException>(async () => await e.MoveNextAsync()).DefaultTimeout();
}
}

[Fact]
public async Task ConnectionTerminatedIfServerTimeoutIntervalElapsesWithNoMessages()
{
Expand Down