12
12
//
13
13
//===----------------------------------------------------------------------===//
14
14
15
- @testable import Lifecycle
16
- import LifecycleNIOCompat
17
15
import NIO
18
16
import NIOConcurrencyHelpers
17
+ @testable import ServiceLauncher
18
+ import ServiceLauncherNIOCompat
19
19
import XCTest
20
20
21
21
final class Tests : XCTestCase {
@@ -28,7 +28,7 @@ final class Tests: XCTestCase {
28
28
lifecycle. shutdown ( )
29
29
}
30
30
lifecycle. wait ( )
31
- items. forEach { XCTAssert ( $0. state == . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
31
+ items. forEach { XCTAssertEqual ( $0. state, . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
32
32
}
33
33
34
34
// FIXME: this test does not work in rio
@@ -43,7 +43,7 @@ final class Tests: XCTestCase {
43
43
kill ( getpid ( ) , signal. rawValue)
44
44
}
45
45
lifecycle. wait ( )
46
- items. forEach { XCTAssert ( $0. state == . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
46
+ items. forEach { XCTAssertEqual ( $0. state, . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
47
47
}
48
48
49
49
func testImmediateShutdown( ) {
@@ -53,7 +53,7 @@ final class Tests: XCTestCase {
53
53
lifecycle. start ( configuration: . init( shutdownSignal: nil ) ) { _ in }
54
54
lifecycle. shutdown ( )
55
55
lifecycle. wait ( )
56
- items. forEach { XCTAssert ( $0. state == . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
56
+ items. forEach { XCTAssertEqual ( $0. state, . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
57
57
}
58
58
59
59
func testBadStartup( ) {
@@ -79,7 +79,7 @@ final class Tests: XCTestCase {
79
79
}
80
80
lifecycle. wait ( )
81
81
let goodItems = items. compactMap { $0 as? GoodItem }
82
- goodItems. forEach { XCTAssert ( $0. state == . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
82
+ goodItems. forEach { XCTAssertEqual ( $0. state, . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
83
83
}
84
84
85
85
func testBadShutdown( ) {
@@ -105,7 +105,7 @@ final class Tests: XCTestCase {
105
105
lifecycle. shutdown ( )
106
106
}
107
107
lifecycle. wait ( )
108
- items. compactMap { $0 as? GoodItem } . forEach { XCTAssert ( $0. state == . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
108
+ items. compactMap { $0 as? GoodItem } . forEach { XCTAssertEqual ( $0. state, . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
109
109
}
110
110
111
111
func testStartAndWait( ) {
@@ -247,7 +247,7 @@ final class Tests: XCTestCase {
247
247
lifecycle. shutdown ( )
248
248
}
249
249
lifecycle. wait ( )
250
- items. forEach { XCTAssert ( $0. state == . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
250
+ items. forEach { XCTAssertEqual ( $0. state, . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
251
251
}
252
252
253
253
func testAyncBarrier( ) {
@@ -269,10 +269,10 @@ final class Tests: XCTestCase {
269
269
lifecycle. shutdown ( )
270
270
}
271
271
lifecycle. wait ( )
272
- [ item1, item2] . forEach { XCTAssert ( $0. state == . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
272
+ [ item1, item2] . forEach { XCTAssertEqual ( $0. state, . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
273
273
}
274
274
275
- func testConcurrncy ( ) {
275
+ func testConcurrency ( ) {
276
276
let lifecycle = Lifecycle ( )
277
277
let items = ( 0 ... 50000 ) . map { _ in GoodItem ( startDelay: 0 , shutdownDelay: 0 ) }
278
278
let group = DispatchGroup ( )
@@ -290,7 +290,7 @@ final class Tests: XCTestCase {
290
290
lifecycle. shutdown ( )
291
291
}
292
292
lifecycle. wait ( )
293
- items. forEach { XCTAssert ( $0. state == . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
293
+ items. forEach { XCTAssertEqual ( $0. state, . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
294
294
}
295
295
296
296
func testRegisterSync( ) {
@@ -324,7 +324,7 @@ final class Tests: XCTestCase {
324
324
lifecycle. shutdown ( )
325
325
}
326
326
lifecycle. wait ( )
327
- XCTAssert ( item. state == . shutdown, " expected item to be shutdown, but \( item. state) " )
327
+ XCTAssertEqual ( item. state, . shutdown, " expected item to be shutdown, but \( item. state) " )
328
328
}
329
329
330
330
func testRegisterShutdownSync( ) {
@@ -356,7 +356,7 @@ final class Tests: XCTestCase {
356
356
lifecycle. shutdown ( )
357
357
}
358
358
lifecycle. wait ( )
359
- XCTAssert ( item. state == . shutdown, " expected item to be shutdown, but \( item. state) " )
359
+ XCTAssertEqual ( item. state, . shutdown, " expected item to be shutdown, but \( item. state) " )
360
360
}
361
361
362
362
func testRegisterAsync( ) {
@@ -372,7 +372,7 @@ final class Tests: XCTestCase {
372
372
lifecycle. shutdown ( )
373
373
}
374
374
lifecycle. wait ( )
375
- XCTAssert ( item. state == . shutdown, " expected item to be shutdown, but \( item. state) " )
375
+ XCTAssertEqual ( item. state, . shutdown, " expected item to be shutdown, but \( item. state) " )
376
376
}
377
377
378
378
func testRegisterShutdownAsync( ) {
@@ -386,7 +386,7 @@ final class Tests: XCTestCase {
386
386
lifecycle. shutdown ( )
387
387
}
388
388
lifecycle. wait ( )
389
- XCTAssert ( item. state == . shutdown, " expected item to be shutdown, but \( item. state) " )
389
+ XCTAssertEqual ( item. state, . shutdown, " expected item to be shutdown, but \( item. state) " )
390
390
}
391
391
392
392
func testRegisterAsyncClosure( ) {
@@ -408,7 +408,7 @@ final class Tests: XCTestCase {
408
408
lifecycle. shutdown ( )
409
409
}
410
410
lifecycle. wait ( )
411
- XCTAssert ( item. state == . shutdown, " expected item to be shutdown, but \( item. state) " )
411
+ XCTAssertEqual ( item. state, . shutdown, " expected item to be shutdown, but \( item. state) " )
412
412
}
413
413
414
414
func testRegisterShutdownAsyncClosure( ) {
@@ -425,7 +425,7 @@ final class Tests: XCTestCase {
425
425
lifecycle. shutdown ( )
426
426
}
427
427
lifecycle. wait ( )
428
- XCTAssert ( item. state == . shutdown, " expected item to be shutdown, but \( item. state) " )
428
+ XCTAssertEqual ( item. state, . shutdown, " expected item to be shutdown, but \( item. state) " )
429
429
}
430
430
431
431
func testRegisterNIO( ) {
@@ -442,7 +442,7 @@ final class Tests: XCTestCase {
442
442
lifecycle. shutdown ( )
443
443
}
444
444
lifecycle. wait ( )
445
- XCTAssert ( item. state == . shutdown, " expected item to be shutdown, but \( item. state) " )
445
+ XCTAssertEqual ( item. state, . shutdown, " expected item to be shutdown, but \( item. state) " )
446
446
}
447
447
448
448
func testRegisterShutdownNIO( ) {
@@ -457,7 +457,7 @@ final class Tests: XCTestCase {
457
457
lifecycle. shutdown ( )
458
458
}
459
459
lifecycle. wait ( )
460
- XCTAssert ( item. state == . shutdown, " expected item to be shutdown, but \( item. state) " )
460
+ XCTAssertEqual ( item. state, . shutdown, " expected item to be shutdown, but \( item. state) " )
461
461
}
462
462
463
463
func testRegisterNIOClosure( ) {
@@ -480,7 +480,7 @@ final class Tests: XCTestCase {
480
480
lifecycle. shutdown ( )
481
481
}
482
482
lifecycle. wait ( )
483
- XCTAssert ( item. state == . shutdown, " expected item to be shutdown, but \( item. state) " )
483
+ XCTAssertEqual ( item. state, . shutdown, " expected item to be shutdown, but \( item. state) " )
484
484
}
485
485
486
486
func testRegisterShutdownNIOClosure( ) {
@@ -498,7 +498,7 @@ final class Tests: XCTestCase {
498
498
lifecycle. shutdown ( )
499
499
}
500
500
lifecycle. wait ( )
501
- XCTAssert ( item. state == . shutdown, " expected item to be shutdown, but \( item. state) " )
501
+ XCTAssertEqual ( item. state, . shutdown, " expected item to be shutdown, but \( item. state) " )
502
502
}
503
503
504
504
func testNIOFailure( ) {
@@ -515,6 +515,56 @@ final class Tests: XCTestCase {
515
515
}
516
516
lifecycle. wait ( )
517
517
}
518
+
519
+ func testExternalState( ) {
520
+ enum State : Equatable {
521
+ case idle
522
+ case started( String )
523
+ case shutdown
524
+ }
525
+
526
+ class Item {
527
+ let eventLoopGroup : EventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
528
+
529
+ let data : String
530
+ init ( _ data: String ) {
531
+ self . data = data
532
+ }
533
+
534
+ func start( ) -> EventLoopFuture < String > {
535
+ return self . eventLoopGroup. next ( ) . makeSucceededFuture ( self . data)
536
+ }
537
+
538
+ func shutdown( ) -> EventLoopFuture < Void > {
539
+ return self . eventLoopGroup. next ( ) . makeSucceededFuture ( ( ) )
540
+ }
541
+ }
542
+
543
+ var state = State . idle
544
+
545
+ let expectedData = UUID ( ) . uuidString
546
+ let item = Item ( expectedData)
547
+ let lifecycle = Lifecycle ( )
548
+ lifecycle. register ( name: " test " ,
549
+ start: . async {
550
+ item. start ( ) . map { data -> Void in
551
+ state = . started( data)
552
+ }
553
+ } ,
554
+ shutdown: . async {
555
+ item. shutdown ( ) . map { _ -> Void in
556
+ state = . shutdown
557
+ }
558
+ } )
559
+
560
+ lifecycle. start ( configuration: . init( shutdownSignal: nil ) ) { error in
561
+ XCTAssertNil ( error, " not expecting error " )
562
+ XCTAssertEqual ( state, . started( expectedData) , " expected item to be shutdown, but \( state) " )
563
+ lifecycle. shutdown ( )
564
+ }
565
+ lifecycle. wait ( )
566
+ XCTAssertEqual ( state, . shutdown, " expected item to be shutdown, but \( state) " )
567
+ }
518
568
}
519
569
520
570
private class GoodItem : LifecycleItem {
0 commit comments