From 50fe4b65dea6edf2c836095fff26d57489c80397 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Wed, 6 Nov 2019 11:55:48 -0700 Subject: [PATCH] Stabilize CanCancelOnRemoteTarget test --- src/StreamJsonRpc.Tests/JsonRpcRemoteTargetTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/StreamJsonRpc.Tests/JsonRpcRemoteTargetTests.cs b/src/StreamJsonRpc.Tests/JsonRpcRemoteTargetTests.cs index 6b4c97e2..a95844a9 100644 --- a/src/StreamJsonRpc.Tests/JsonRpcRemoteTargetTests.cs +++ b/src/StreamJsonRpc.Tests/JsonRpcRemoteTargetTests.cs @@ -142,8 +142,14 @@ public async Task CanInvokeOnOriginServerFromAdditionalRemoteTarget() public async Task CanCancelOnRemoteTarget() { var tokenSource = new CancellationTokenSource(); + RemoteTargetOne.CancellableRemoteOperationEntered.Reset(); var task = this.originRpc.InvokeWithCancellationAsync(nameof(RemoteTargetOne.CancellableRemoteOperation), cancellationToken: tokenSource.Token); + + // Don't cancel the token until we've entered the method, since the point is the RPC method receives the notice, + // not that we cancel before it's even transmitted. + await RemoteTargetOne.CancellableRemoteOperationEntered.WaitAsync(this.TimeoutToken); tokenSource.Cancel(); + var result = await task; Assert.True(result); } @@ -280,6 +286,8 @@ public class RemoteTargetOne { private static TaskCompletionSource notificationTcs = new TaskCompletionSource(); + internal static readonly AsyncManualResetEvent CancellableRemoteOperationEntered = new AsyncManualResetEvent(); + public static Task NotificationReceived => notificationTcs.Task; public static void GetOne() @@ -305,6 +313,7 @@ public static async Task AddOneLongRunningAsync(int value) public static async Task CancellableRemoteOperation(CancellationToken token) { + CancellableRemoteOperationEntered.Set(); var retryIndex = 0; while (retryIndex < 100) {