Skip to content

Commit 5ad9992

Browse files
authored
Allow zero-task startup (#55)
* Allow zero-task startup * Add test for zero-task * Fix Linux build and improve consistency * Fix sanity
1 parent 945d681 commit 5ad9992

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

Sources/Lifecycle/Lifecycle.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public struct ServiceLifecycle {
110110
let signalSource = ServiceLifecycle.trap(signal: signal, handler: { signal in
111111
self.lifecycle.log("intercepted signal: \(signal)")
112112
self.shutdown()
113-
})
113+
})
114114
self.lifecycle.shutdownGroup.notify(queue: .global()) {
115115
signalSource.cancel()
116116
}
@@ -357,14 +357,16 @@ public class ComponentLifecycle: LifecycleTask {
357357
// MARK: - private
358358

359359
private func _start(on queue: DispatchQueue, tasks: [LifecycleTask], callback: @escaping (Error?) -> Void) {
360-
precondition(tasks.count > 0, "invalid number of tasks, must be > 0")
361360
self.stateLock.withLock {
362361
guard case .idle = self.state else {
363362
preconditionFailure("invalid state, \(self.state)")
364363
}
365-
log("starting")
364+
self.log("starting")
366365
self.state = .starting(queue)
367366
}
367+
if tasks.count == 0 {
368+
self.log(level: .notice, "no tasks provided")
369+
}
368370
self.startTask(on: queue, tasks: tasks, index: 0) { started, error in
369371
self.stateLock.lock()
370372
if error != nil {

Tests/LifecycleTests/ComponentLifecycleTests+XCTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extension ComponentLifecycleTests {
4040
("testSync", testSync),
4141
("testAyncBarrier", testAyncBarrier),
4242
("testConcurrency", testConcurrency),
43+
("testZeroTask", testZeroTask),
4344
("testRegisterSync", testRegisterSync),
4445
("testRegisterShutdownSync", testRegisterShutdownSync),
4546
("testRegisterAsync", testRegisterAsync),

Tests/LifecycleTests/ComponentLifecycleTests.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class ComponentLifecycleTests: XCTestCase {
4848
dispatchPrecondition(condition: .onQueue(.global()))
4949
XCTAssertTrue(startCalls.contains(id))
5050
stopCalls.append(id)
51-
})
51+
})
5252
}
5353
lifecycle.register(items)
5454

@@ -82,7 +82,7 @@ final class ComponentLifecycleTests: XCTestCase {
8282
dispatchPrecondition(condition: .onQueue(testQueue))
8383
XCTAssertTrue(startCalls.contains(id))
8484
stopCalls.append(id)
85-
})
85+
})
8686
}
8787
lifecycle.register(items)
8888

@@ -206,7 +206,7 @@ final class ComponentLifecycleTests: XCTestCase {
206206
shutdown: .sync {
207207
XCTAssertTrue(startCalls.contains(id))
208208
stopCalls.append(id)
209-
})
209+
})
210210
}
211211
do {
212212
let id = UUID().uuidString
@@ -217,7 +217,7 @@ final class ComponentLifecycleTests: XCTestCase {
217217
shutdown: .sync {
218218
XCTAssertTrue(startCalls.contains(id))
219219
stopCalls.append(id)
220-
})
220+
})
221221
}
222222
lifecycle.start { error in
223223
XCTAssertNil(error)
@@ -470,6 +470,16 @@ final class ComponentLifecycleTests: XCTestCase {
470470
items.forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") }
471471
}
472472

473+
func testZeroTask() {
474+
let lifecycle = ComponentLifecycle(label: "test")
475+
476+
lifecycle.start { error in
477+
XCTAssertNil(error, "not expecting error")
478+
lifecycle.shutdown()
479+
}
480+
lifecycle.wait()
481+
}
482+
473483
func testRegisterSync() {
474484
class Sync {
475485
var state = State.idle
@@ -811,7 +821,7 @@ final class ComponentLifecycleTests: XCTestCase {
811821
}
812822
callback(nil)
813823
}
814-
})
824+
})
815825

816826
lifecycle.start { error in
817827
XCTAssertNil(error, "not expecting error")

0 commit comments

Comments
 (0)