11using System ;
2+ using System . Threading . Tasks ;
23using HandyIpc ;
34using HandyIpcTests . Fixtures ;
45using HandyIpcTests . Interfaces ;
@@ -18,76 +19,46 @@ public EventTypeTest(NamedPipeFixture namedPipeFixture, SocketFixture socketFixt
1819 _socketFixture = socketFixture ;
1920 }
2021
21- // [Fact]
22- public void TestEventHandlerWithSocket ( )
22+ [ Fact ]
23+ public async Task TestEventHandlerWithSocket ( )
2324 {
2425 var instance = _socketFixture . Client . Resolve < IEventType > ( ) ;
25- TestEventHandlerSubscribeAndUnsubscribe ( instance ) ;
26+ await TestEventHandlerSubscribeAndUnsubscribe ( instance ) ;
2627 }
2728
28- // [Fact]
29- public void TestEventHandlerWithNamedPipe ( )
29+ [ Fact ]
30+ public async Task TestEventHandlerWithNamedPipe ( )
3031 {
3132 var instance = _namedPipeFixture . Client . Resolve < IEventType > ( ) ;
32- TestEventHandlerSubscribeAndUnsubscribe ( instance ) ;
33+ await TestEventHandlerSubscribeAndUnsubscribe ( instance ) ;
3334 }
3435
35- private static void TestEventHandlerSubscribeAndUnsubscribe ( IEventType instance )
36+ private static async Task TestEventHandlerSubscribeAndUnsubscribe ( IEventType instance )
3637 {
3738 // Some issues will occur only when the number of tests is high.
3839 // In particular, it tests whether the event calls are synchronized.
3940 const int testCount = 10000 ;
4041
41- int count1 = 0 ;
42- int count2 = 0 ;
43- int count3 = 0 ;
44-
45- // ReSharper disable AccessToModifiedClosure
46- void Handler1 ( object ? _ , EventArgs e ) => count1 ++ ;
47- EventHandler handler2 = ( _ , _ ) => count2 ++ ;
48- EventHandler handler3 = ( _ , _ ) => count3 ++ ;
49- // ReSharper restore AccessToModifiedClosure
50-
51- instance . Changed += Handler1 ;
52- instance . Changed += handler2 ;
53- instance . Changed += handler3 ;
54-
55- for ( int i = 0 ; i < testCount ; i ++ )
42+ int count = 0 ;
43+ Task WrapAsAsync ( IEventType source )
5644 {
57- instance . RaiseChanged ( EventArgs . Empty ) ;
58- Assert . Equal ( i + 1 , count1 ) ;
59- Assert . Equal ( i + 1 , count2 ) ;
60- Assert . Equal ( i + 1 , count3 ) ;
61- }
45+ TaskCompletionSource tcs = new ( ) ;
46+ source . Changed += OnChanged ;
47+ source . RaiseChanged ( EventArgs . Empty ) ;
48+ return tcs . Task ;
6249
63- count1 = 0 ;
64- count2 = 0 ;
65- count3 = 0 ;
66-
67- instance . Changed -= Handler1 ;
68- instance . Changed -= handler2 ;
69- instance . Changed -= handler3 ;
70-
71- for ( int i = 0 ; i < testCount ; i ++ )
72- {
73- instance . RaiseChanged ( EventArgs . Empty ) ;
74- Assert . Equal ( 0 , count1 ) ;
75- Assert . Equal ( 0 , count2 ) ;
76- Assert . Equal ( 0 , count3 ) ;
50+ void OnChanged ( object ? sender , EventArgs e )
51+ {
52+ source . Changed -= OnChanged ;
53+ count ++ ;
54+ tcs . SetResult ( ) ;
55+ }
7756 }
7857
79- instance . Changed += Handler1 ;
80- instance . Changed += Handler1 ;
81- instance . Changed += handler2 ;
82- instance . Changed += handler2 ;
83- instance . Changed += handler3 ;
84-
8558 for ( int i = 0 ; i < testCount ; i ++ )
8659 {
87- instance . RaiseChanged ( EventArgs . Empty ) ;
88- Assert . Equal ( 2 * ( i + 1 ) , count1 ) ;
89- Assert . Equal ( 2 * ( i + 1 ) , count2 ) ;
90- Assert . Equal ( i + 1 , count3 ) ;
60+ await WrapAsAsync ( instance ) ;
61+ Assert . Equal ( i + 1 , count ) ;
9162 }
9263 }
9364 }
0 commit comments