From 0052995d3087f3692d1626da2f67d4b3e7660a48 Mon Sep 17 00:00:00 2001 From: tomer doron Date: Tue, 7 Apr 2020 13:55:11 -0700 Subject: [PATCH] small refactoring (#31) motivation: prepare for OSS changes: * rename the internal _shutdown function to shutdownItem * add and improve few tests --- Sources/ServiceLauncher/Lifecycle.swift | 12 +-- .../LifecycleTests+XCTest.swift | 3 +- .../ServiceLauncherTests/LifecycleTests.swift | 90 ++++++++++++++----- 3 files changed, 74 insertions(+), 31 deletions(-) diff --git a/Sources/ServiceLauncher/Lifecycle.swift b/Sources/ServiceLauncher/Lifecycle.swift index 69f518b..d3606fc 100644 --- a/Sources/ServiceLauncher/Lifecycle.swift +++ b/Sources/ServiceLauncher/Lifecycle.swift @@ -131,7 +131,7 @@ public class Lifecycle { } self.state = .starting(configuration.callbackQueue) } - self._start(on: configuration.callbackQueue, items: items, index: 0) { started, error in + self.startItem(on: configuration.callbackQueue, items: items, index: 0) { started, error in self.stateLock.lock() if error != nil { self.state = .shuttingDown(configuration.callbackQueue) @@ -165,7 +165,7 @@ public class Lifecycle { } } - private func _start(on queue: DispatchQueue, items: [LifecycleItem], index: Int, callback: @escaping (Int, Error?) -> Void) { + private func startItem(on queue: DispatchQueue, items: [LifecycleItem], index: Int, callback: @escaping (Int, Error?) -> Void) { // async barrier let start = { (callback) -> Void in queue.async { items[index].start(callback: callback) } } let callback = { (index, error) -> Void in queue.async { callback(index, error) } } @@ -183,7 +183,7 @@ public class Lifecycle { if case .shuttingDown = self.stateLock.withLock({ self.state }) { return callback(index, nil) } - self._start(on: queue, items: items, index: index + 1, callback: callback) + self.startItem(on: queue, items: items, index: index + 1, callback: callback) } } @@ -192,7 +192,7 @@ public class Lifecycle { self.logger.info("shutting down lifecycle") self.state = .shuttingDown(queue) } - self._shutdown(on: queue, items: items.reversed(), index: 0, errors: nil) { errors in + self.shutdownItem(on: queue, items: items.reversed(), index: 0, errors: nil) { errors in self.stateLock.withLock { guard case .shuttingDown = self.state else { preconditionFailure("invalid state, \(self.state)") @@ -204,7 +204,7 @@ public class Lifecycle { } } - private func _shutdown(on queue: DispatchQueue, items: [LifecycleItem], index: Int, errors: [String: Error]?, callback: @escaping ([String: Error]?) -> Void) { + private func shutdownItem(on queue: DispatchQueue, items: [LifecycleItem], index: Int, errors: [String: Error]?, callback: @escaping ([String: Error]?) -> Void) { // async barrier let shutdown = { (callback) -> Void in queue.async { items[index].shutdown(callback: callback) } } let callback = { (errors) -> Void in queue.async { callback(errors) } } @@ -222,7 +222,7 @@ public class Lifecycle { errors![items[index].label] = error self.logger.error("failed to stop [\(items[index].label)]: \(error)") } - self._shutdown(on: queue, items: items, index: index + 1, errors: errors, callback: callback) + self.shutdownItem(on: queue, items: items, index: index + 1, errors: errors, callback: callback) } } diff --git a/Tests/ServiceLauncherTests/LifecycleTests+XCTest.swift b/Tests/ServiceLauncherTests/LifecycleTests+XCTest.swift index e3a9cdf..986c1cd 100644 --- a/Tests/ServiceLauncherTests/LifecycleTests+XCTest.swift +++ b/Tests/ServiceLauncherTests/LifecycleTests+XCTest.swift @@ -26,7 +26,8 @@ extension Tests { static var allTests: [(String, (Tests) -> () throws -> Void)] { return [ ("testStartThenShutdown", testStartThenShutdown), - ("testDispatchQueues", testDispatchQueues), + ("testDefaultCallbackQueue", testDefaultCallbackQueue), + ("testUserDefinedCallbackQueue", testUserDefinedCallbackQueue), ("testShutdownWhileStarting", testShutdownWhileStarting), ("testShutdownWhenIdle", testShutdownWhenIdle), ("testShutdownWhenShutdown", testShutdownWhenShutdown), diff --git a/Tests/ServiceLauncherTests/LifecycleTests.swift b/Tests/ServiceLauncherTests/LifecycleTests.swift index 6fe9130..e643a88 100644 --- a/Tests/ServiceLauncherTests/LifecycleTests.swift +++ b/Tests/ServiceLauncherTests/LifecycleTests.swift @@ -19,7 +19,7 @@ import XCTest final class Tests: XCTestCase { func testStartThenShutdown() { - let items = (0 ... Int.random(in: 10 ... 20)).map { _ in GoodItem() } + let items = (5 ... Int.random(in: 10 ... 20)).map { _ in GoodItem() } let lifecycle = Lifecycle() lifecycle.register(items) lifecycle.start(configuration: .init(shutdownSignal: nil)) { startError in @@ -35,7 +35,7 @@ final class Tests: XCTestCase { // FIXME: this test does not work in rio func _testShutdownWithSignal() { let signal = Lifecycle.Signal.ALRM - let items = (0 ... Int.random(in: 10 ... 20)).map { _ in GoodItem() } + let items = (5 ... Int.random(in: 10 ... 20)).map { _ in GoodItem() } let lifecycle = Lifecycle() lifecycle.register(items) let configuration = Lifecycle.Configuration(shutdownSignal: [signal]) @@ -47,29 +47,71 @@ final class Tests: XCTestCase { items.forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") } } - func testDispatchQueues() { + func testDefaultCallbackQueue() { let lifecycle = Lifecycle() - let testQueue = DispatchQueue(label: UUID().uuidString) + var startCalls = [String]() + var stopCalls = [String]() - lifecycle.register(label: UUID().uuidString, - start: { - dispatchPrecondition(condition: DispatchPredicate.onQueue(testQueue)) - }, - shutdown: { - dispatchPrecondition(condition: DispatchPredicate.onQueue(testQueue)) - }) - lifecycle.register(label: UUID().uuidString, - start: { - dispatchPrecondition(condition: DispatchPredicate.onQueue(testQueue)) - }, - shutdown: { - dispatchPrecondition(condition: DispatchPredicate.onQueue(testQueue)) - }) - lifecycle.start(configuration: .init(callbackQueue: testQueue, shutdownSignal: nil)) { error in - XCTAssertNil(error) - lifecycle.shutdown() + let items = (1 ... Int.random(in: 10 ... 20)).map { index -> LifecycleItem in + let id = "item-\(index)" + return Lifecycle.Item(label: id, + start: .sync { + dispatchPrecondition(condition: .onQueue(.global())) + startCalls.append(id) + }, + shutdown: .sync { + dispatchPrecondition(condition: .onQueue(.global())) + XCTAssertTrue(startCalls.contains(id)) + stopCalls.append(id) + }) + } + lifecycle.register(items) + + lifecycle.start(configuration: .init(shutdownSignal: nil)) { startError in + dispatchPrecondition(condition: .onQueue(.global())) + XCTAssertNil(startError, "not expecting error") + lifecycle.shutdown { shutdownErrors in + dispatchPrecondition(condition: .onQueue(.global())) + XCTAssertNil(shutdownErrors, "not expecting error") + } } lifecycle.wait() + items.forEach { item in XCTAssertTrue(startCalls.contains(item.label), "expected \(item.label) to be started") } + items.forEach { item in XCTAssertTrue(stopCalls.contains(item.label), "expected \(item.label) to be stopped") } + } + + func testUserDefinedCallbackQueue() { + let lifecycle = Lifecycle() + let testQueue = DispatchQueue(label: UUID().uuidString) + var startCalls = [String]() + var stopCalls = [String]() + + let items = (1 ... Int.random(in: 10 ... 20)).map { index -> LifecycleItem in + let id = "item-\(index)" + return Lifecycle.Item(label: id, + start: .sync { + dispatchPrecondition(condition: .onQueue(testQueue)) + startCalls.append(id) + }, + shutdown: .sync { + dispatchPrecondition(condition: .onQueue(testQueue)) + XCTAssertTrue(startCalls.contains(id)) + stopCalls.append(id) + }) + } + lifecycle.register(items) + + lifecycle.start(configuration: .init(callbackQueue: testQueue, shutdownSignal: nil)) { startError in + dispatchPrecondition(condition: .onQueue(testQueue)) + XCTAssertNil(startError, "not expecting error") + lifecycle.shutdown { shutdownErrors in + dispatchPrecondition(condition: .onQueue(testQueue)) + XCTAssertNil(shutdownErrors, "not expecting error") + } + } + lifecycle.wait() + items.forEach { item in XCTAssertTrue(startCalls.contains(item.label), "expected \(item.label) to be started") } + items.forEach { item in XCTAssertTrue(stopCalls.contains(item.label), "expected \(item.label) to be stopped") } } func testShutdownWhileStarting() { @@ -104,7 +146,7 @@ final class Tests: XCTestCase { } var started = 0 let startSempahore = DispatchSemaphore(value: 0) - let items = (0 ... Int.random(in: 10 ... 20)).map { _ in Item { + let items = (5 ... Int.random(in: 10 ... 20)).map { _ in Item { started += 1 startSempahore.signal() } } @@ -387,7 +429,7 @@ final class Tests: XCTestCase { } let lifecycle = Lifecycle() - let items = (0 ... Int.random(in: 10 ... 20)).map { _ in Sync() } + let items = (5 ... Int.random(in: 10 ... 20)).map { _ in Sync() } items.forEach { item in lifecycle.register(label: item.id, start: item.start, shutdown: item.shutdown) } @@ -424,7 +466,7 @@ final class Tests: XCTestCase { func testConcurrency() { let lifecycle = Lifecycle() - let items = (0 ... 50000).map { _ in GoodItem(startDelay: 0, shutdownDelay: 0) } + let items = (5000 ... 10000).map { _ in GoodItem(startDelay: 0, shutdownDelay: 0) } let group = DispatchGroup() items.forEach { item in group.enter()