1
+ using System ;
1
2
using System . Collections . Generic ;
3
+ using System . Runtime . CompilerServices ;
2
4
using System . Threading ;
3
5
using System . Threading . Tasks ;
4
6
using Xunit ;
@@ -41,15 +43,16 @@ private void CaptureSyncContext()
41
43
42
44
private async Task LoseSyncContext ( )
43
45
{
44
- await Task . Run ( ( ) => { } ) . ConfigureAwait ( false ) ; // Switch synchronization context and continue
46
+ await new CompletesOnDifferentThreadAwaitable ( ) ; // Switch synchronization context and continue
45
47
Assert . NotEqual ( _customSynchronizationContext , SynchronizationContext . Current ) ;
46
48
}
47
49
48
50
/// <summary>
49
- /// Tests capture the SynchronizationContext at various points through out their execution.
50
- /// This asserts that every capture is the expected SynchronizationContext instance and that is hasn't been lost.
51
+ /// Tests capture the SynchronizationContext at various points throughout their execution.
52
+ /// This asserts that every capture is the expected SynchronizationContext instance and that it hasn't been lost.
51
53
/// </summary>
52
54
/// <param name="numberOfExpectedCalls">Ensure that we have the expected number of captures</param>
55
+ // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
53
56
private void AssertSyncContextAlwaysRetained ( int numberOfExpectedCalls )
54
57
{
55
58
Assert . Equal ( numberOfExpectedCalls , _capturedSyncContext . Count ) ;
@@ -154,7 +157,7 @@ public async Task Multiple_Deactivations_should_retain_SyncContext()
154
157
155
158
// ASSERT
156
159
AssertSyncContextAlwaysRetained ( 3 ) ;
157
- }
160
+ }
158
161
159
162
[ Fact ]
160
163
public async Task Multiple_OnEntry_should_retain_SyncContext ( )
@@ -338,4 +341,21 @@ public async Task InternalTransition_firing_a_sync_action_should_retain_SyncCont
338
341
// ASSERT
339
342
AssertSyncContextAlwaysRetained ( 1 ) ;
340
343
}
344
+
345
+ private class CompletesOnDifferentThreadAwaitable
346
+ {
347
+ public CompletesOnDifferentThreadAwaiter GetAwaiter ( ) => new ( ) ;
348
+
349
+ internal class CompletesOnDifferentThreadAwaiter : INotifyCompletion
350
+ {
351
+ public void GetResult ( ) { }
352
+
353
+ public bool IsCompleted => false ;
354
+
355
+ public void OnCompleted ( Action continuation )
356
+ {
357
+ ThreadPool . QueueUserWorkItem ( _ => continuation ( ) ) ;
358
+ }
359
+ }
360
+ }
341
361
}
0 commit comments