|
| 1 | +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using NUnit.Framework; |
| 9 | +using NUnit.Framework.Internal; |
| 10 | +using NUnit.Framework.Tests.TestUtilities; |
| 11 | +using NUnit.TestData; |
| 12 | + |
| 13 | +using AsyncExecutionApiAdapter = NUnit.Framework.Tests.AsyncExecutionApiAdapter; |
| 14 | + |
| 15 | +namespace NUnit.Windows.Tests |
| 16 | +{ |
| 17 | + public static class SynchronizationContextTests |
| 18 | + { |
| 19 | + [Test] |
| 20 | + public static void SynchronizationContextIsRestoredBetweenTestCaseSources() |
| 21 | + { |
| 22 | + using (TestUtils.RestoreSynchronizationContext()) // Restore the synchronization context so as not to affect other tests if this test fails |
| 23 | + { |
| 24 | + var fixture = TestBuilder.MakeFixture(typeof(SynchronizationContextFixture)); |
| 25 | + |
| 26 | + foreach (var name in new[] |
| 27 | + { |
| 28 | + nameof(SynchronizationContextFixture.TestMethodWithSourceThatSetsSynchronizationContext1), |
| 29 | + nameof(SynchronizationContextFixture.TestMethodWithSourceThatSetsSynchronizationContext2) |
| 30 | + }) |
| 31 | + { |
| 32 | + var parameterizedSuite = fixture.Tests.Single(t => t.Method?.Name == name); |
| 33 | + Assert.That(parameterizedSuite.Tests.Single().Arguments.Single(), Is.True); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static IEnumerable<AsyncExecutionApiAdapter> ApiAdapters => AsyncExecutionApiAdapter.All; |
| 39 | + |
| 40 | +#if NETCOREAPP |
| 41 | + [Platform(Include = "Win, Mono")] |
| 42 | +#endif |
| 43 | + [Apartment(ApartmentState.STA)] |
| 44 | + [TestCaseSource(nameof(ApiAdapters))] |
| 45 | + public static void ContinuationStaysOnStaThread(AsyncExecutionApiAdapter apiAdapter) |
| 46 | + { |
| 47 | + var thread = Thread.CurrentThread; |
| 48 | + |
| 49 | + apiAdapter.Execute(async () => |
| 50 | + { |
| 51 | + await Task.Yield(); |
| 52 | + Assert.That(Thread.CurrentThread, Is.SameAs(thread)); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | +#if NETCOREAPP |
| 57 | + [Platform(Include = "Win, Mono")] |
| 58 | +#endif |
| 59 | + [Apartment(ApartmentState.STA)] |
| 60 | + [TestCaseSource(nameof(ApiAdapters))] |
| 61 | + public static void AsyncDelegatesAreExecutedOnStaThread(AsyncExecutionApiAdapter apiAdapter) |
| 62 | + { |
| 63 | + var thread = Thread.CurrentThread; |
| 64 | + |
| 65 | + apiAdapter.Execute(() => |
| 66 | + { |
| 67 | + Assert.That(Thread.CurrentThread, Is.SameAs(thread)); |
| 68 | + return Task.FromResult<object?>(null); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + // TODO: test a custom awaitable type whose awaiter executes continuations on a brand new thread |
| 73 | + // to ensure that the message pump is shut down on the correct thread. |
| 74 | + public static readonly IEnumerable<Type> KnownSynchronizationContextTypes = new[] |
| 75 | + { |
| 76 | + typeof(System.Windows.Forms.WindowsFormsSynchronizationContext), |
| 77 | + typeof(System.Windows.Threading.DispatcherSynchronizationContext) |
| 78 | + }; |
| 79 | + |
| 80 | + private static SynchronizationContext CreateSynchronizationContext(Type knownSynchronizationContextType) |
| 81 | + { |
| 82 | + if (new PlatformHelper().IsPlatformSupported("Mono")) |
| 83 | + { |
| 84 | + if (knownSynchronizationContextType == typeof(System.Windows.Threading.DispatcherSynchronizationContext)) |
| 85 | + { |
| 86 | + Assert.Ignore("DispatcherSynchronizationContext throws NotImplementedException on Mono."); |
| 87 | + } |
| 88 | + else if (knownSynchronizationContextType == typeof(System.Windows.Forms.WindowsFormsSynchronizationContext)) |
| 89 | + { |
| 90 | + if (!Environment.UserInteractive) |
| 91 | + { |
| 92 | + Assert.Inconclusive("WindowsFormsSynchronizationContext throws ArgumentNullException on Mono when not running interactively."); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return (SynchronizationContext)Activator.CreateInstance(knownSynchronizationContextType)!; |
| 98 | + } |
| 99 | + |
| 100 | + [Test, CancelAfter(10_000)] |
| 101 | + public static void ContinuationDoesNotDeadlockOnKnownSynchronizationContext( |
| 102 | + [ValueSource(nameof(KnownSynchronizationContextTypes))] Type knownSynchronizationContextType, |
| 103 | + [ValueSource(nameof(ApiAdapters))] AsyncExecutionApiAdapter apiAdapter, |
| 104 | + CancellationToken cancellationToken) |
| 105 | + { |
| 106 | + var createdOnThisThread = CreateSynchronizationContext(knownSynchronizationContextType); |
| 107 | + |
| 108 | + using (TestUtils.TemporarySynchronizationContext(createdOnThisThread)) |
| 109 | + { |
| 110 | + apiAdapter.Execute(async () => |
| 111 | + { |
| 112 | + cancellationToken.ThrowIfCancellationRequested(); |
| 113 | + await Task.Yield(); |
| 114 | + }); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + [Test] |
| 119 | + public static void AsyncDelegatesAreExecutedUnderTheCurrentSynchronizationContext( |
| 120 | + [ValueSource(nameof(KnownSynchronizationContextTypes))] Type knownSynchronizationContextType, |
| 121 | + [ValueSource(nameof(ApiAdapters))] AsyncExecutionApiAdapter apiAdapter) |
| 122 | + { |
| 123 | + var createdOnThisThread = CreateSynchronizationContext(knownSynchronizationContextType); |
| 124 | + |
| 125 | + using (TestUtils.TemporarySynchronizationContext(createdOnThisThread)) |
| 126 | + { |
| 127 | + apiAdapter.Execute(() => |
| 128 | + { |
| 129 | + Assert.That(SynchronizationContext.Current, Is.TypeOf(knownSynchronizationContextType)); |
| 130 | + return Task.CompletedTask; |
| 131 | + }); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + [Test, CancelAfter(10_000)] |
| 136 | + public static void AwaitingContinuationDoesNotAlterSynchronizationContext( |
| 137 | + [ValueSource(nameof(KnownSynchronizationContextTypes))] Type knownSynchronizationContextType, |
| 138 | + [ValueSource(nameof(ApiAdapters))] AsyncExecutionApiAdapter apiAdapter, |
| 139 | + CancellationToken cancellationToken) |
| 140 | + { |
| 141 | + var createdOnThisThread = CreateSynchronizationContext(knownSynchronizationContextType); |
| 142 | + |
| 143 | + using (TestUtils.TemporarySynchronizationContext(createdOnThisThread)) |
| 144 | + { |
| 145 | + apiAdapter.Execute(async () => |
| 146 | + { |
| 147 | + cancellationToken.ThrowIfCancellationRequested(); |
| 148 | + Assert.That(SynchronizationContext.Current, Is.TypeOf(knownSynchronizationContextType)); |
| 149 | + await Task.Yield(); |
| 150 | + Assert.That(SynchronizationContext.Current, Is.TypeOf(knownSynchronizationContextType)); |
| 151 | + }); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments