diff options
author | Stephen Halter <halter73@gmail.com> | 2022-01-22 07:09:02 +0300 |
---|---|---|
committer | Stephen Halter <halter73@gmail.com> | 2022-01-22 07:51:47 +0300 |
commit | 08c90d9e8b68d248aab348c29e81b1f871fbec44 (patch) | |
tree | e4e7fe2ac734770aaa55f1df203f847eb0779442 | |
parent | 149f6d94f64ae61928e74eb59307237bcc149271 (diff) |
fix testhalter73/configureawait-false
-rw-r--r-- | src/SignalR/clients/csharp/Client/test/FunctionalTests/HubConnectionTests.cs | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/SignalR/clients/csharp/Client/test/FunctionalTests/HubConnectionTests.cs b/src/SignalR/clients/csharp/Client/test/FunctionalTests/HubConnectionTests.cs index 3d7a5b7652..9b4ca06859 100644 --- a/src/SignalR/clients/csharp/Client/test/FunctionalTests/HubConnectionTests.cs +++ b/src/SignalR/clients/csharp/Client/test/FunctionalTests/HubConnectionTests.cs @@ -67,7 +67,7 @@ public class HubConnectionTests : FunctionalTestBase return hubConnectionBuilder.Build(); } - private Func<EndPoint, ValueTask<ConnectionContext>> GetHttpConnectionFactory(string url, ILoggerFactory loggerFactory, string path, HttpTransportType transportType, TransferFormat transferFormat) + private static Func<EndPoint, ValueTask<ConnectionContext>> GetHttpConnectionFactory(string url, ILoggerFactory loggerFactory, string path, HttpTransportType transportType, TransferFormat transferFormat) { return async endPoint => { @@ -75,7 +75,8 @@ public class HubConnectionTests : FunctionalTestBase var options = new HttpConnectionOptions { Url = httpEndpoint.Uri, Transports = transportType, DefaultTransferFormat = transferFormat }; var connection = new HttpConnection(options, loggerFactory); - await connection.StartAsync(); + // This is used by CanBlockOnAsyncOperationsWithOneAtATimeSynchronizationContext, so the ConfigureAwait(false) is important. + await connection.StartAsync().ConfigureAwait(false); return connection; }; @@ -2105,6 +2106,9 @@ public class HubConnectionTests : FunctionalTestBase try { + // Yield first so the rest of the test runs in the OneAtATimeSynchronizationContext.Run loop + await Task.Yield(); + Assert.True(connection.StartAsync().Wait(DefaultTimeout)); var invokeTask = connection.InvokeAsync<string>(nameof(TestHub.HelloWorld)); @@ -2126,20 +2130,31 @@ public class HubConnectionTests : FunctionalTestBase private class OneAtATimeSynchronizationContext : SynchronizationContext, IDisposable { - private Channel<(SendOrPostCallback, object)> _taskQueue = Channel.CreateUnbounded<(SendOrPostCallback, object)>(); + private readonly Channel<(SendOrPostCallback, object)> _taskQueue = Channel.CreateUnbounded<(SendOrPostCallback, object)>(); + private bool _disposed; public OneAtATimeSynchronizationContext() { - _ = Run(); + // Task.Run to avoid running with xUnit's AsyncTestSyncContext as well + _ = Task.Run(Run); } public override void Post(SendOrPostCallback d, object state) { + if (_disposed) + { + // There should be no other calls to Post() after dispose. If there are calls, + // the test has most likely failed with a timeout. Let the callbacks run so the + // timeout exception gets reported accurately instead of as a long-running test. + d(state); + } + _taskQueue.Writer.TryWrite((d, state)); } public void Dispose() { + _disposed = true; _taskQueue.Writer.Complete(); } @@ -2158,7 +2173,6 @@ public class HubConnectionTests : FunctionalTestBase } } - private class PollTrackingMessageHandler : DelegatingHandler { public Task<HttpResponseMessage> ActivePoll { get; private set; } |