@@ -19,7 +19,7 @@ import XCTest
19
19
20
20
final class Tests : XCTestCase {
21
21
func testStartThenShutdown( ) {
22
- let items = ( 0 ... Int . random ( in: 10 ... 20 ) ) . map { _ in GoodItem ( ) }
22
+ let items = ( 5 ... Int . random ( in: 10 ... 20 ) ) . map { _ in GoodItem ( ) }
23
23
let lifecycle = Lifecycle ( )
24
24
lifecycle. register ( items)
25
25
lifecycle. start ( configuration: . init( shutdownSignal: nil ) ) { startError in
@@ -35,7 +35,7 @@ final class Tests: XCTestCase {
35
35
// FIXME: this test does not work in rio
36
36
func _testShutdownWithSignal( ) {
37
37
let signal = Lifecycle . Signal. ALRM
38
- let items = ( 0 ... Int . random ( in: 10 ... 20 ) ) . map { _ in GoodItem ( ) }
38
+ let items = ( 5 ... Int . random ( in: 10 ... 20 ) ) . map { _ in GoodItem ( ) }
39
39
let lifecycle = Lifecycle ( )
40
40
lifecycle. register ( items)
41
41
let configuration = Lifecycle . Configuration ( shutdownSignal: [ signal] )
@@ -47,29 +47,71 @@ final class Tests: XCTestCase {
47
47
items. forEach { XCTAssertEqual ( $0. state, . shutdown, " expected item to be shutdown, but \( $0. state) " ) }
48
48
}
49
49
50
- func testDispatchQueues( ) {
50
+ func testDefaultCallbackQueue( ) {
51
+ let lifecycle = Lifecycle ( )
52
+ var startCalls = [ String] ( )
53
+ var stopCalls = [ String] ( )
54
+
55
+ let items = ( 1 ... Int . random ( in: 10 ... 20 ) ) . map { index -> LifecycleItem in
56
+ let id = " item- \( index) "
57
+ return Lifecycle . Item ( label: id,
58
+ start: . sync {
59
+ dispatchPrecondition ( condition: . onQueue( . global( ) ) )
60
+ startCalls. append ( id)
61
+ } ,
62
+ shutdown: . sync {
63
+ dispatchPrecondition ( condition: . onQueue( . global( ) ) )
64
+ XCTAssertTrue ( startCalls. contains ( id) )
65
+ stopCalls. append ( id)
66
+ } )
67
+ }
68
+ lifecycle. register ( items)
69
+
70
+ lifecycle. start ( configuration: . init( shutdownSignal: nil ) ) { startError in
71
+ dispatchPrecondition ( condition: . onQueue( . global( ) ) )
72
+ XCTAssertNil ( startError, " not expecting error " )
73
+ lifecycle. shutdown { shutdownErrors in
74
+ dispatchPrecondition ( condition: . onQueue( . global( ) ) )
75
+ XCTAssertNil ( shutdownErrors, " not expecting error " )
76
+ }
77
+ }
78
+ lifecycle. wait ( )
79
+ items. forEach { item in XCTAssertTrue ( startCalls. contains ( item. label) , " expected \( item. label) to be started " ) }
80
+ items. forEach { item in XCTAssertTrue ( stopCalls. contains ( item. label) , " expected \( item. label) to be stopped " ) }
81
+ }
82
+
83
+ func testUserDefinedCallbackQueue( ) {
51
84
let lifecycle = Lifecycle ( )
52
85
let testQueue = DispatchQueue ( label: UUID ( ) . uuidString)
86
+ var startCalls = [ String] ( )
87
+ var stopCalls = [ String] ( )
53
88
54
- lifecycle. register ( label: UUID ( ) . uuidString,
55
- start: {
56
- dispatchPrecondition ( condition: DispatchPredicate . onQueue ( testQueue) )
57
- } ,
58
- shutdown: {
59
- dispatchPrecondition ( condition: DispatchPredicate . onQueue ( testQueue) )
60
- } )
61
- lifecycle. register ( label: UUID ( ) . uuidString,
62
- start: {
63
- dispatchPrecondition ( condition: DispatchPredicate . onQueue ( testQueue) )
64
- } ,
65
- shutdown: {
66
- dispatchPrecondition ( condition: DispatchPredicate . onQueue ( testQueue) )
67
- } )
68
- lifecycle. start ( configuration: . init( callbackQueue: testQueue, shutdownSignal: nil ) ) { error in
69
- XCTAssertNil ( error)
70
- lifecycle. shutdown ( )
89
+ let items = ( 1 ... Int . random ( in: 10 ... 20 ) ) . map { index -> LifecycleItem in
90
+ let id = " item- \( index) "
91
+ return Lifecycle . Item ( label: id,
92
+ start: . sync {
93
+ dispatchPrecondition ( condition: . onQueue( testQueue) )
94
+ startCalls. append ( id)
95
+ } ,
96
+ shutdown: . sync {
97
+ dispatchPrecondition ( condition: . onQueue( testQueue) )
98
+ XCTAssertTrue ( startCalls. contains ( id) )
99
+ stopCalls. append ( id)
100
+ } )
101
+ }
102
+ lifecycle. register ( items)
103
+
104
+ lifecycle. start ( configuration: . init( callbackQueue: testQueue, shutdownSignal: nil ) ) { startError in
105
+ dispatchPrecondition ( condition: . onQueue( testQueue) )
106
+ XCTAssertNil ( startError, " not expecting error " )
107
+ lifecycle. shutdown { shutdownErrors in
108
+ dispatchPrecondition ( condition: . onQueue( testQueue) )
109
+ XCTAssertNil ( shutdownErrors, " not expecting error " )
110
+ }
71
111
}
72
112
lifecycle. wait ( )
113
+ items. forEach { item in XCTAssertTrue ( startCalls. contains ( item. label) , " expected \( item. label) to be started " ) }
114
+ items. forEach { item in XCTAssertTrue ( stopCalls. contains ( item. label) , " expected \( item. label) to be stopped " ) }
73
115
}
74
116
75
117
func testShutdownWhileStarting( ) {
@@ -104,7 +146,7 @@ final class Tests: XCTestCase {
104
146
}
105
147
var started = 0
106
148
let startSempahore = DispatchSemaphore ( value: 0 )
107
- let items = ( 0 ... Int . random ( in: 10 ... 20 ) ) . map { _ in Item {
149
+ let items = ( 5 ... Int . random ( in: 10 ... 20 ) ) . map { _ in Item {
108
150
started += 1
109
151
startSempahore. signal ( )
110
152
} }
@@ -387,7 +429,7 @@ final class Tests: XCTestCase {
387
429
}
388
430
389
431
let lifecycle = Lifecycle ( )
390
- let items = ( 0 ... Int . random ( in: 10 ... 20 ) ) . map { _ in Sync ( ) }
432
+ let items = ( 5 ... Int . random ( in: 10 ... 20 ) ) . map { _ in Sync ( ) }
391
433
items. forEach { item in
392
434
lifecycle. register ( label: item. id, start: item. start, shutdown: item. shutdown)
393
435
}
@@ -424,7 +466,7 @@ final class Tests: XCTestCase {
424
466
425
467
func testConcurrency( ) {
426
468
let lifecycle = Lifecycle ( )
427
- let items = ( 0 ... 50000 ) . map { _ in GoodItem ( startDelay: 0 , shutdownDelay: 0 ) }
469
+ let items = ( 5000 ... 10000 ) . map { _ in GoodItem ( startDelay: 0 , shutdownDelay: 0 ) }
428
470
let group = DispatchGroup ( )
429
471
items. forEach { item in
430
472
group. enter ( )
0 commit comments