Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
282 changes: 196 additions & 86 deletions Sources/ServiceLauncher/Lifecycle.swift

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import XCTest
@testable import ServiceLauncherTests

XCTMain([
testCase(Tests.allTests),
testCase(LifeycleTests.allTests),
testCase(TopLevelTests.allTests),
])
#endif
101 changes: 101 additions & 0 deletions Tests/ServiceLauncherTests/Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftServiceLauncher open source project
//
// Copyright (c) 2019-2020 Apple Inc. and the SwiftServiceLauncher project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftServiceLauncher project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Foundation
import NIO
import NIOConcurrencyHelpers
import ServiceLauncher

class GoodItem: LifecycleItem {
let queue = DispatchQueue(label: "GoodItem", attributes: .concurrent)

let id: String
let startDelay: Double
let shutdownDelay: Double

var state = State.idle
let stateLock = Lock()

init(id: String = UUID().uuidString,
startDelay: Double = Double.random(in: 0.01 ... 0.1),
shutdownDelay: Double = Double.random(in: 0.01 ... 0.1)) {
self.id = id
self.startDelay = startDelay
self.shutdownDelay = shutdownDelay
}

var label: String {
return self.id
}

func start(callback: @escaping (Error?) -> Void) {
self.queue.asyncAfter(deadline: .now() + self.startDelay) {
self.stateLock.withLock { self.state = .started }
callback(nil)
}
}

func shutdown(callback: @escaping (Error?) -> Void) {
self.queue.asyncAfter(deadline: .now() + self.shutdownDelay) {
self.stateLock.withLock { self.state = .shutdown }
callback(nil)
}
}

enum State {
case idle
case started
case shutdown
}
}

class NIOItem {
let id: String
let eventLoopGroup: EventLoopGroup
let startDelay: Int64
let shutdownDelay: Int64

var state = State.idle
let stateLock = Lock()

init(eventLoopGroup: EventLoopGroup,
id: String = UUID().uuidString,
startDelay: Int64 = Int64.random(in: 10 ... 20),
shutdownDelay: Int64 = Int64.random(in: 10 ... 20)) {
self.id = id
self.eventLoopGroup = eventLoopGroup
self.startDelay = startDelay
self.shutdownDelay = shutdownDelay
}

func start() -> EventLoopFuture<Void> {
return self.eventLoopGroup.next().scheduleTask(in: .milliseconds(self.startDelay)) {
self.stateLock.withLock { self.state = .started }
}.futureResult
}

func shutdown() -> EventLoopFuture<Void> {
return self.eventLoopGroup.next().scheduleTask(in: .milliseconds(self.shutdownDelay)) {
self.stateLock.withLock { self.state = .shutdown }
}.futureResult
}

enum State {
case idle
case started
case shutdown
}
}

struct TestError: Error {}
6 changes: 2 additions & 4 deletions Tests/ServiceLauncherTests/LifecycleTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ import XCTest
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
///

extension Tests {
static var allTests: [(String, (Tests) -> () throws -> Void)] {
extension LifeycleTests {
static var allTests: [(String, (LifeycleTests) -> () throws -> Void)] {
return [
("testStartThenShutdown", testStartThenShutdown),
("testImmediateShutdown", testImmediateShutdown),
("testBadStartup", testBadStartup),
("testBadShutdown", testBadShutdown),
("testStartAndWait", testStartAndWait),
("testBadStartAndWait", testBadStartAndWait),
("testShutdownInOrder", testShutdownInOrder),
("testSync", testSync),
("testAyncBarrier", testAyncBarrier),
Expand Down
Loading