1
1
using System ;
2
+ using System . Threading . Tasks ;
2
3
using HandyIpc ;
3
4
using HandyIpcTests . Fixtures ;
4
5
using HandyIpcTests . Interfaces ;
@@ -18,76 +19,46 @@ public EventTypeTest(NamedPipeFixture namedPipeFixture, SocketFixture socketFixt
18
19
_socketFixture = socketFixture ;
19
20
}
20
21
21
- // [Fact]
22
- public void TestEventHandlerWithSocket ( )
22
+ [ Fact ]
23
+ public async Task TestEventHandlerWithSocket ( )
23
24
{
24
25
var instance = _socketFixture . Client . Resolve < IEventType > ( ) ;
25
- TestEventHandlerSubscribeAndUnsubscribe ( instance ) ;
26
+ await TestEventHandlerSubscribeAndUnsubscribe ( instance ) ;
26
27
}
27
28
28
- // [Fact]
29
- public void TestEventHandlerWithNamedPipe ( )
29
+ [ Fact ]
30
+ public async Task TestEventHandlerWithNamedPipe ( )
30
31
{
31
32
var instance = _namedPipeFixture . Client . Resolve < IEventType > ( ) ;
32
- TestEventHandlerSubscribeAndUnsubscribe ( instance ) ;
33
+ await TestEventHandlerSubscribeAndUnsubscribe ( instance ) ;
33
34
}
34
35
35
- private static void TestEventHandlerSubscribeAndUnsubscribe ( IEventType instance )
36
+ private static async Task TestEventHandlerSubscribeAndUnsubscribe ( IEventType instance )
36
37
{
37
38
// Some issues will occur only when the number of tests is high.
38
39
// In particular, it tests whether the event calls are synchronized.
39
40
const int testCount = 10000 ;
40
41
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 )
56
44
{
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 ;
62
49
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
+ }
77
56
}
78
57
79
- instance . Changed += Handler1 ;
80
- instance . Changed += Handler1 ;
81
- instance . Changed += handler2 ;
82
- instance . Changed += handler2 ;
83
- instance . Changed += handler3 ;
84
-
85
58
for ( int i = 0 ; i < testCount ; i ++ )
86
59
{
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 ) ;
91
62
}
92
63
}
93
64
}
0 commit comments