@@ -29,6 +29,11 @@ class EventDispatcherTests: XCTestCase {
2929 XCTAssertEqual ( MockUrlSession . validSessions, 0 , " all MockUrlSession must be invalidated " )
3030 }
3131
32+ func waitForFlush( ) {
33+ eventDispatcher? . queueLock. sync { }
34+ _ = eventDispatcher? . notify. wait ( timeout: . now( ) + 10.0 )
35+ }
36+
3237 func testDefaultDispatcher( ) {
3338 eventDispatcher = DefaultEventDispatcher ( timerInterval: 10 )
3439 let pEventD : OPTEventDispatcher = eventDispatcher!
@@ -43,8 +48,7 @@ class EventDispatcherTests: XCTestCase {
4348
4449 XCTAssert ( eventDispatcher? . eventQueue. count == 1 )
4550 eventDispatcher? . flushEvents ( )
46-
47- eventDispatcher? . queueLock. sync { }
51+ waitForFlush ( )
4852
4953 XCTAssert ( eventDispatcher? . eventQueue. count == 0 )
5054
@@ -86,15 +90,15 @@ class EventDispatcherTests: XCTestCase {
8690 }
8791
8892 pEventD. flushEvents ( )
89- wait ( )
93+ waitForFlush ( )
9094
9195 pEventD. dispatchEvent ( event: EventForDispatch ( body: Data ( ) ) , completionHandler: nil )
92- wait ( )
96+ waitForFlush ( )
9397
9498 XCTAssert ( eventDispatcher? . eventQueue. count == 1 )
9599
96100 eventDispatcher? . flushEvents ( )
97- wait ( )
101+ waitForFlush ( )
98102
99103 XCTAssert ( eventDispatcher? . eventQueue. count == 0 )
100104
@@ -111,15 +115,15 @@ class EventDispatcherTests: XCTestCase {
111115 }
112116
113117 pEventD. flushEvents ( )
114- wait ( )
118+ waitForFlush ( )
115119
116120 pEventD. dispatchEvent ( event: EventForDispatch ( body: Data ( ) ) , completionHandler: nil )
117- wait ( )
121+ waitForFlush ( )
118122
119123 XCTAssert ( eventDispatcher? . eventQueue. count == 1 )
120124
121125 eventDispatcher? . flushEvents ( )
122- wait ( )
126+ waitForFlush ( )
123127
124128 XCTAssert ( eventDispatcher? . eventQueue. count == 0 )
125129
@@ -136,15 +140,15 @@ class EventDispatcherTests: XCTestCase {
136140 }
137141
138142 pEventD. flushEvents ( )
139- wait ( )
143+ waitForFlush ( )
140144
141145 pEventD. dispatchEvent ( event: EventForDispatch ( body: Data ( ) ) , completionHandler: nil )
142- wait ( )
146+ waitForFlush ( )
143147
144148 XCTAssert ( eventDispatcher? . eventQueue. count == 1 )
145149
146150 eventDispatcher? . flushEvents ( )
147- wait ( )
151+ waitForFlush ( )
148152
149153 XCTAssert ( eventDispatcher? . eventQueue. count == 0 )
150154
@@ -198,11 +202,11 @@ class EventDispatcherTests: XCTestCase {
198202 eventDispatcher = DefaultEventDispatcher ( timerInterval: 1 )
199203
200204 eventDispatcher? . flushEvents ( )
201- eventDispatcher ? . queueLock . sync { }
205+ waitForFlush ( )
202206
203207 eventDispatcher? . dispatchEvent ( event: EventForDispatch ( body: Data ( ) ) , completionHandler: nil )
204208
205- eventDispatcher ? . queueLock . sync { }
209+ waitForFlush ( )
206210
207211 eventDispatcher? . applicationDidBecomeActive ( )
208212 eventDispatcher? . applicationDidEnterBackground ( )
@@ -298,6 +302,7 @@ class EventDispatcherTests: XCTestCase {
298302 XCTAssert ( dispatcher. eventQueue. count == 2 )
299303 dispatcher. flushEvents ( )
300304 dispatcher. queueLock. sync { }
305+ _ = dispatcher. notify. wait ( timeout: . now( ) + 10.0 )
301306 XCTAssert ( dispatcher. eventQueue. count == 0 )
302307 }
303308
0 commit comments