|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftServiceLauncher open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2019-2020 Apple Inc. and the SwiftServiceLauncher project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftServiceLauncher project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Foundation |
| 16 | +import NIO |
| 17 | +import NIOConcurrencyHelpers |
| 18 | +import ServiceLauncher |
| 19 | + |
| 20 | +class GoodItem: LifecycleItem { |
| 21 | + let id: String |
| 22 | + let startDelay: Double |
| 23 | + let shutdownDelay: Double |
| 24 | + |
| 25 | + var state = State.idle |
| 26 | + let stateLock = Lock() |
| 27 | + |
| 28 | + init(id: String = UUID().uuidString, |
| 29 | + startDelay: Double = Double.random(in: 0.01 ... 0.1), |
| 30 | + shutdownDelay: Double = Double.random(in: 0.01 ... 0.1)) { |
| 31 | + self.id = id |
| 32 | + self.startDelay = startDelay |
| 33 | + self.shutdownDelay = shutdownDelay |
| 34 | + } |
| 35 | + |
| 36 | + var label: String { |
| 37 | + return self.id |
| 38 | + } |
| 39 | + |
| 40 | + func start(on queue: DispatchQueue, callback: @escaping (Error?) -> Void) { |
| 41 | + queue.asyncAfter(deadline: .now() + self.startDelay) { |
| 42 | + self.stateLock.withLock { self.state = .started } |
| 43 | + callback(nil) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + func shutdown(on queue: DispatchQueue, callback: @escaping (Error?) -> Void) { |
| 48 | + queue.asyncAfter(deadline: .now() + self.shutdownDelay) { |
| 49 | + self.stateLock.withLock { self.state = .shutdown } |
| 50 | + callback(nil) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + enum State { |
| 55 | + case idle |
| 56 | + case started |
| 57 | + case shutdown |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class NIOItem { |
| 62 | + let id: String |
| 63 | + let eventLoopGroup: EventLoopGroup |
| 64 | + let startDelay: Int64 |
| 65 | + let shutdownDelay: Int64 |
| 66 | + |
| 67 | + var state = State.idle |
| 68 | + let stateLock = Lock() |
| 69 | + |
| 70 | + init(eventLoopGroup: EventLoopGroup, |
| 71 | + id: String = UUID().uuidString, |
| 72 | + startDelay: Int64 = Int64.random(in: 10 ... 20), |
| 73 | + shutdownDelay: Int64 = Int64.random(in: 10 ... 20)) { |
| 74 | + self.id = id |
| 75 | + self.eventLoopGroup = eventLoopGroup |
| 76 | + self.startDelay = startDelay |
| 77 | + self.shutdownDelay = shutdownDelay |
| 78 | + } |
| 79 | + |
| 80 | + func start() -> EventLoopFuture<Void> { |
| 81 | + return self.eventLoopGroup.next().scheduleTask(in: .milliseconds(self.startDelay)) { |
| 82 | + self.stateLock.withLock { self.state = .started } |
| 83 | + }.futureResult |
| 84 | + } |
| 85 | + |
| 86 | + func shutdown() -> EventLoopFuture<Void> { |
| 87 | + return self.eventLoopGroup.next().scheduleTask(in: .milliseconds(self.shutdownDelay)) { |
| 88 | + self.stateLock.withLock { self.state = .shutdown } |
| 89 | + }.futureResult |
| 90 | + } |
| 91 | + |
| 92 | + enum State { |
| 93 | + case idle |
| 94 | + case started |
| 95 | + case shutdown |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +struct TestError: Error {} |
0 commit comments