Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Allow CancellationToken in streaming hub methods #2818

Merged
merged 6 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,21 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.SignalR.Tests
{
public static class CancellationTokenExtensions
{
public static Task WaitForCancellationAsync(this CancellationToken token)
{
var tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
token.Register((t) =>
{
((TaskCompletionSource<object>)t).SetResult(null);
}, tcs);
return tcs.Task;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,10 @@ public ChannelReader<int> CancelableStream(CancellationToken token)
{
var channel = Channel.CreateBounded<int>(10);

Task.Run(() =>
Task.Run(async () =>
{
_tcsService.StartedMethod.SetResult(null);
token.WaitHandle.WaitOne();
await token.WaitForCancellationAsync();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

channel.Writer.TryComplete();
_tcsService.EndMethod.SetResult(null);
});
Expand All @@ -571,10 +571,10 @@ public ChannelReader<int> CancelableStream2(int ignore, int ignore2, Cancellatio
{
var channel = Channel.CreateBounded<int>(10);

Task.Run(() =>
Task.Run(async () =>
{
_tcsService.StartedMethod.SetResult(null);
token.WaitHandle.WaitOne();
await token.WaitForCancellationAsync();
channel.Writer.TryComplete();
_tcsService.EndMethod.SetResult(null);
});
Expand All @@ -586,10 +586,10 @@ public ChannelReader<int> CancelableStreamMiddle(int ignore, CancellationToken t
{
var channel = Channel.CreateBounded<int>(10);

Task.Run(() =>
Task.Run(async () =>
{
_tcsService.StartedMethod.SetResult(null);
token.WaitHandle.WaitOne();
await token.WaitForCancellationAsync();
channel.Writer.TryComplete();
_tcsService.EndMethod.SetResult(null);
});
Expand Down