|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.Threading; |
| 6 | +using System.Threading.Tasks; |
6 | 7 |
|
7 | 8 | /// <summary> |
8 | 9 | /// Tests for <see cref="NoMessagePumpSyncContext"/>. |
@@ -36,6 +37,109 @@ public void Default_IsNoMessagePumpSyncContext() |
36 | 37 | Assert.IsType<NoMessagePumpSyncContext>(NoMessagePumpSyncContext.Default); |
37 | 38 | } |
38 | 39 |
|
| 40 | + /// <summary> |
| 41 | + /// Verifies that <see cref="NoMessagePumpSyncContext.Post"/> schedules work on the thread pool |
| 42 | + /// when no underlying sync context is provided. |
| 43 | + /// </summary> |
| 44 | + [Fact] |
| 45 | + public async Task Post_DefaultConstructor_ExecutesOnThreadPool() |
| 46 | + { |
| 47 | + NoMessagePumpSyncContext sc = new(); |
| 48 | + TaskCompletionSource<bool> tcs = new(); |
| 49 | + sc.Post(_ => tcs.SetResult(Thread.CurrentThread.IsThreadPoolThread), null); |
| 50 | + Assert.True(await tcs.Task.WithCancellation(this.TimeoutToken)); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Verifies that <see cref="NoMessagePumpSyncContext.Send"/> executes work synchronously |
| 55 | + /// on the calling thread when no underlying sync context is provided. |
| 56 | + /// </summary> |
| 57 | + [Fact] |
| 58 | + public void Send_DefaultConstructor_ExecutesInlineOnCallingThread() |
| 59 | + { |
| 60 | + NoMessagePumpSyncContext sc = new(); |
| 61 | + int callingThreadId = Thread.CurrentThread.ManagedThreadId; |
| 62 | + int? callbackThreadId = null; |
| 63 | + bool callbackInvoked = false; |
| 64 | + |
| 65 | + sc.Send( |
| 66 | + _ => |
| 67 | + { |
| 68 | + callbackInvoked = true; |
| 69 | + callbackThreadId = Thread.CurrentThread.ManagedThreadId; |
| 70 | + }, |
| 71 | + null); |
| 72 | + |
| 73 | + Assert.True(callbackInvoked); |
| 74 | + Assert.Equal(callingThreadId, callbackThreadId); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Verifies that <see cref="NoMessagePumpSyncContext(SynchronizationContext)"/> throws |
| 79 | + /// <see cref="ArgumentNullException"/> when a null underlying context is passed. |
| 80 | + /// </summary> |
| 81 | + [Fact] |
| 82 | + public void Constructor_WithNullUnderlyingContext_Throws() |
| 83 | + { |
| 84 | + Assert.Throws<ArgumentNullException>(() => new NoMessagePumpSyncContext(null!)); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Verifies that <see cref="NoMessagePumpSyncContext.Post"/> rejects a null callback before |
| 89 | + /// delegating to the underlying sync context. |
| 90 | + /// </summary> |
| 91 | + [Fact] |
| 92 | + public void Post_WithNullCallback_Throws() |
| 93 | + { |
| 94 | + ThrowingSyncContext underlying = new(); |
| 95 | + NoMessagePumpSyncContext sc = new(underlying); |
| 96 | + |
| 97 | + Assert.Throws<ArgumentNullException>(() => sc.Post(null!, null)); |
| 98 | + Assert.False(underlying.PostInvoked); |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Verifies that <see cref="NoMessagePumpSyncContext.Send"/> rejects a null callback before |
| 103 | + /// delegating to the underlying sync context. |
| 104 | + /// </summary> |
| 105 | + [Fact] |
| 106 | + public void Send_WithNullCallback_Throws() |
| 107 | + { |
| 108 | + ThrowingSyncContext underlying = new(); |
| 109 | + NoMessagePumpSyncContext sc = new(underlying); |
| 110 | + |
| 111 | + Assert.Throws<ArgumentNullException>(() => sc.Send(null!, null)); |
| 112 | + Assert.False(underlying.SendInvoked); |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Verifies that <see cref="NoMessagePumpSyncContext.Post"/> delegates to the underlying |
| 117 | + /// sync context when one is provided. |
| 118 | + /// </summary> |
| 119 | + [Fact] |
| 120 | + public async Task Post_WithUnderlyingContext_DelegatesToUnderlying() |
| 121 | + { |
| 122 | + TaskCompletionSource<bool> tcs = new(); |
| 123 | + RecordingPostSyncContext underlying = new(posted: _ => tcs.SetResult(true)); |
| 124 | + NoMessagePumpSyncContext sc = new(underlying); |
| 125 | + sc.Post(_ => { }, null); |
| 126 | + Assert.True(await tcs.Task.WithCancellation(this.TimeoutToken)); |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Verifies that <see cref="NoMessagePumpSyncContext.Send"/> delegates to the underlying |
| 131 | + /// sync context when one is provided. |
| 132 | + /// </summary> |
| 133 | + [Fact] |
| 134 | + public void Send_WithUnderlyingContext_DelegatesToUnderlying() |
| 135 | + { |
| 136 | + bool sendInvoked = false; |
| 137 | + RecordingSendSyncContext underlying = new(sent: _ => sendInvoked = true); |
| 138 | + NoMessagePumpSyncContext sc = new(underlying); |
| 139 | + sc.Send(_ => { }, null); |
| 140 | + Assert.True(sendInvoked); |
| 141 | + } |
| 142 | + |
39 | 143 | #if NETFRAMEWORK |
40 | 144 | /// <summary> |
41 | 145 | /// Establishes the baseline: on a plain STA thread without a special synchronization context, |
@@ -77,4 +181,50 @@ public void Wait_BlocksComRpcCalls() |
77 | 181 | } |
78 | 182 | } |
79 | 183 | #endif |
| 184 | + |
| 185 | + /// <summary> |
| 186 | + /// A <see cref="SynchronizationContext"/> that invokes a callback when <see cref="Post"/> is called. |
| 187 | + /// </summary> |
| 188 | + private class RecordingPostSyncContext(Action<SendOrPostCallback> posted) : SynchronizationContext |
| 189 | + { |
| 190 | + public override void Post(SendOrPostCallback d, object? state) |
| 191 | + { |
| 192 | + posted(d); |
| 193 | + base.Post(d, state); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /// <summary> |
| 198 | + /// A <see cref="SynchronizationContext"/> that invokes a callback when <see cref="Send"/> is called. |
| 199 | + /// </summary> |
| 200 | + private class RecordingSendSyncContext(Action<SendOrPostCallback> sent) : SynchronizationContext |
| 201 | + { |
| 202 | + public override void Send(SendOrPostCallback d, object? state) |
| 203 | + { |
| 204 | + sent(d); |
| 205 | + base.Send(d, state); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /// <summary> |
| 210 | + /// A <see cref="SynchronizationContext"/> that records whether <see cref="Post"/> or <see cref="Send"/> were invoked. |
| 211 | + /// </summary> |
| 212 | + private class ThrowingSyncContext : SynchronizationContext |
| 213 | + { |
| 214 | + public bool PostInvoked { get; private set; } |
| 215 | + |
| 216 | + public bool SendInvoked { get; private set; } |
| 217 | + |
| 218 | + public override void Post(SendOrPostCallback d, object? state) |
| 219 | + { |
| 220 | + this.PostInvoked = true; |
| 221 | + throw new InvalidOperationException(); |
| 222 | + } |
| 223 | + |
| 224 | + public override void Send(SendOrPostCallback d, object? state) |
| 225 | + { |
| 226 | + this.SendInvoked = true; |
| 227 | + throw new InvalidOperationException(); |
| 228 | + } |
| 229 | + } |
80 | 230 | } |
0 commit comments