@@ -159,23 +159,75 @@ final class ComponentLifecycleTests: XCTestCase {
159
159
160
160
func testShutdownWhenIdle( ) {
161
161
let lifecycle = ComponentLifecycle ( label: " test " )
162
- lifecycle. register ( GoodItem ( ) )
163
162
164
- let sempahpore1 = DispatchSemaphore ( value: 0 )
163
+ let item = GoodItem ( )
164
+ lifecycle. register ( item)
165
+
166
+ let semaphore1 = DispatchSemaphore ( value: 0 )
165
167
lifecycle. shutdown { errors in
166
168
XCTAssertNil ( errors)
167
- sempahpore1 . signal ( )
169
+ semaphore1 . signal ( )
168
170
}
169
171
lifecycle. wait ( )
170
- XCTAssertEqual ( . success, sempahpore1 . wait ( timeout: . now( ) + 1 ) )
172
+ XCTAssertEqual ( . success, semaphore1 . wait ( timeout: . now( ) + 1 ) )
171
173
172
- let sempahpore2 = DispatchSemaphore ( value: 0 )
174
+ let semaphore2 = DispatchSemaphore ( value: 0 )
173
175
lifecycle. shutdown { errors in
174
176
XCTAssertNil ( errors)
175
- sempahpore2 . signal ( )
177
+ semaphore2 . signal ( )
176
178
}
177
179
lifecycle. wait ( )
178
- XCTAssertEqual ( . success, sempahpore2. wait ( timeout: . now( ) + 1 ) )
180
+ XCTAssertEqual ( . success, semaphore2. wait ( timeout: . now( ) + 1 ) )
181
+
182
+ XCTAssertEqual ( item. state, . idle, " expected item to be idle " )
183
+ }
184
+
185
+ func testShutdownWhenIdleAndNoItems( ) {
186
+ let lifecycle = ComponentLifecycle ( label: " test " )
187
+
188
+ let semaphore1 = DispatchSemaphore ( value: 0 )
189
+ lifecycle. shutdown { errors in
190
+ XCTAssertNil ( errors)
191
+ semaphore1. signal ( )
192
+ }
193
+ lifecycle. wait ( )
194
+ XCTAssertEqual ( . success, semaphore1. wait ( timeout: . now( ) + 1 ) )
195
+
196
+ let semaphore2 = DispatchSemaphore ( value: 0 )
197
+ lifecycle. shutdown { errors in
198
+ XCTAssertNil ( errors)
199
+ semaphore2. signal ( )
200
+ }
201
+ lifecycle. wait ( )
202
+ XCTAssertEqual ( . success, semaphore2. wait ( timeout: . now( ) + 1 ) )
203
+ }
204
+
205
+ func testIfNotStartedWhenIdle( ) {
206
+ var shutdown1Called = false
207
+ var shutdown2Called = false
208
+ var shutdown3Called = false
209
+
210
+ let lifecycle = ComponentLifecycle ( label: " test " )
211
+
212
+ lifecycle. register ( label: " shutdown1 " ,
213
+ start: . sync { } ,
214
+ shutdown: . sync { shutdown1Called = true } ,
215
+ shutdownIfNotStarted: true )
216
+
217
+ lifecycle. register ( label: " shutdown2 " , start: . none, shutdown: . sync {
218
+ shutdown2Called = true
219
+ } )
220
+
221
+ lifecycle. registerShutdown ( label: " shutdown3 " , . sync {
222
+ shutdown3Called = true
223
+ } )
224
+
225
+ lifecycle. shutdown ( )
226
+ lifecycle. wait ( )
227
+
228
+ XCTAssertTrue ( shutdown1Called, " expected shutdown to be called " )
229
+ XCTAssertTrue ( shutdown2Called, " expected shutdown to be called " )
230
+ XCTAssertTrue ( shutdown3Called, " expected shutdown to be called " )
179
231
}
180
232
181
233
func testShutdownWhenShutdown( ) {
0 commit comments