Skip to content

Commit 070734a

Browse files
authored
rename module (#12)
motivation: adjust module name to avoid ambiguity between module and main type changes: rename Lifecycle to ServiceLauncher
1 parent ae96513 commit 070734a

File tree

7 files changed

+81
-29
lines changed

7 files changed

+81
-29
lines changed

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ needs to be listed here.
1212
### Contributors
1313

1414
- Tomer Doron <[email protected]>
15+
- Yim Lee <[email protected]>
1516

1617
**Updating this list**
1718

Package.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import PackageDescription
55
let package = Package(
66
name: "swift-service-launcher",
77
products: [
8-
.library(name: "Lifecycle", targets: ["Lifecycle"]),
9-
.library(name: "LifecycleNIOCompat", targets: ["LifecycleNIOCompat"]),
8+
.library(name: "ServiceLauncher", targets: ["ServiceLauncher"]),
9+
.library(name: "ServiceLauncherNIOCompat", targets: ["ServiceLauncherNIOCompat"]),
1010
],
1111
dependencies: [
1212
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
@@ -15,8 +15,8 @@ let package = Package(
1515
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"), // used in tests
1616
],
1717
targets: [
18-
.target(name: "Lifecycle", dependencies: ["Logging", "Metrics", "Backtrace"]),
19-
.target(name: "LifecycleNIOCompat", dependencies: ["Lifecycle", "NIO"]),
20-
.testTarget(name: "LifecycleTests", dependencies: ["Lifecycle", "LifecycleNIOCompat"]),
18+
.target(name: "ServiceLauncher", dependencies: ["Logging", "Metrics", "Backtrace"]),
19+
.target(name: "ServiceLauncherNIOCompat", dependencies: ["ServiceLauncher", "NIO"]),
20+
.testTarget(name: "ServiceLauncherTests", dependencies: ["ServiceLauncher", "ServiceLauncherNIOCompat"]),
2121
]
2222
)

Sources/LifecycleNIOCompat/Bridge.swift renamed to Sources/ServiceLauncherNIOCompat/Bridge.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Lifecycle
1615
import NIO
16+
import ServiceLauncher
1717

1818
extension Lifecycle.Handler {
1919
/// Asynchronous `Lifecycle.Handler` based on an `EventLoopFuture`.

Tests/LinuxMain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import XCTest
2323
///
2424

2525
#if os(Linux) || os(FreeBSD)
26-
@testable import LifecycleTests
26+
@testable import ServiceLauncherTests
2727

2828
XCTMain([
2929
testCase(Tests.allTests),

Tests/LifecycleTests/LifecycleTests+XCTest.swift renamed to Tests/ServiceLauncherTests/LifecycleTests+XCTest.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension Tests {
3434
("testShutdownInOrder", testShutdownInOrder),
3535
("testSync", testSync),
3636
("testAyncBarrier", testAyncBarrier),
37-
("testConcurrncy", testConcurrncy),
37+
("testConcurrency", testConcurrency),
3838
("testRegisterSync", testRegisterSync),
3939
("testRegisterShutdownSync", testRegisterShutdownSync),
4040
("testRegisterAsync", testRegisterAsync),
@@ -46,6 +46,7 @@ extension Tests {
4646
("testRegisterNIOClosure", testRegisterNIOClosure),
4747
("testRegisterShutdownNIOClosure", testRegisterShutdownNIOClosure),
4848
("testNIOFailure", testNIOFailure),
49+
("testExternalState", testExternalState),
4950
]
5051
}
5152
}

Tests/LifecycleTests/LifecycleTests.swift renamed to Tests/ServiceLauncherTests/LifecycleTests.swift

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
@testable import Lifecycle
16-
import LifecycleNIOCompat
1715
import NIO
1816
import NIOConcurrencyHelpers
17+
@testable import ServiceLauncher
18+
import ServiceLauncherNIOCompat
1919
import XCTest
2020

2121
final class Tests: XCTestCase {
@@ -28,7 +28,7 @@ final class Tests: XCTestCase {
2828
lifecycle.shutdown()
2929
}
3030
lifecycle.wait()
31-
items.forEach { XCTAssert($0.state == .shutdown, "expected item to be shutdown, but \($0.state)") }
31+
items.forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") }
3232
}
3333

3434
// FIXME: this test does not work in rio
@@ -43,7 +43,7 @@ final class Tests: XCTestCase {
4343
kill(getpid(), signal.rawValue)
4444
}
4545
lifecycle.wait()
46-
items.forEach { XCTAssert($0.state == .shutdown, "expected item to be shutdown, but \($0.state)") }
46+
items.forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") }
4747
}
4848

4949
func testImmediateShutdown() {
@@ -53,7 +53,7 @@ final class Tests: XCTestCase {
5353
lifecycle.start(configuration: .init(shutdownSignal: nil)) { _ in }
5454
lifecycle.shutdown()
5555
lifecycle.wait()
56-
items.forEach { XCTAssert($0.state == .shutdown, "expected item to be shutdown, but \($0.state)") }
56+
items.forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") }
5757
}
5858

5959
func testBadStartup() {
@@ -79,7 +79,7 @@ final class Tests: XCTestCase {
7979
}
8080
lifecycle.wait()
8181
let goodItems = items.compactMap { $0 as? GoodItem }
82-
goodItems.forEach { XCTAssert($0.state == .shutdown, "expected item to be shutdown, but \($0.state)") }
82+
goodItems.forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") }
8383
}
8484

8585
func testBadShutdown() {
@@ -105,7 +105,7 @@ final class Tests: XCTestCase {
105105
lifecycle.shutdown()
106106
}
107107
lifecycle.wait()
108-
items.compactMap { $0 as? GoodItem }.forEach { XCTAssert($0.state == .shutdown, "expected item to be shutdown, but \($0.state)") }
108+
items.compactMap { $0 as? GoodItem }.forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") }
109109
}
110110

111111
func testStartAndWait() {
@@ -247,7 +247,7 @@ final class Tests: XCTestCase {
247247
lifecycle.shutdown()
248248
}
249249
lifecycle.wait()
250-
items.forEach { XCTAssert($0.state == .shutdown, "expected item to be shutdown, but \($0.state)") }
250+
items.forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") }
251251
}
252252

253253
func testAyncBarrier() {
@@ -269,10 +269,10 @@ final class Tests: XCTestCase {
269269
lifecycle.shutdown()
270270
}
271271
lifecycle.wait()
272-
[item1, item2].forEach { XCTAssert($0.state == .shutdown, "expected item to be shutdown, but \($0.state)") }
272+
[item1, item2].forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") }
273273
}
274274

275-
func testConcurrncy() {
275+
func testConcurrency() {
276276
let lifecycle = Lifecycle()
277277
let items = (0 ... 50000).map { _ in GoodItem(startDelay: 0, shutdownDelay: 0) }
278278
let group = DispatchGroup()
@@ -290,7 +290,7 @@ final class Tests: XCTestCase {
290290
lifecycle.shutdown()
291291
}
292292
lifecycle.wait()
293-
items.forEach { XCTAssert($0.state == .shutdown, "expected item to be shutdown, but \($0.state)") }
293+
items.forEach { XCTAssertEqual($0.state, .shutdown, "expected item to be shutdown, but \($0.state)") }
294294
}
295295

296296
func testRegisterSync() {
@@ -324,7 +324,7 @@ final class Tests: XCTestCase {
324324
lifecycle.shutdown()
325325
}
326326
lifecycle.wait()
327-
XCTAssert(item.state == .shutdown, "expected item to be shutdown, but \(item.state)")
327+
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown, but \(item.state)")
328328
}
329329

330330
func testRegisterShutdownSync() {
@@ -356,7 +356,7 @@ final class Tests: XCTestCase {
356356
lifecycle.shutdown()
357357
}
358358
lifecycle.wait()
359-
XCTAssert(item.state == .shutdown, "expected item to be shutdown, but \(item.state)")
359+
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown, but \(item.state)")
360360
}
361361

362362
func testRegisterAsync() {
@@ -372,7 +372,7 @@ final class Tests: XCTestCase {
372372
lifecycle.shutdown()
373373
}
374374
lifecycle.wait()
375-
XCTAssert(item.state == .shutdown, "expected item to be shutdown, but \(item.state)")
375+
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown, but \(item.state)")
376376
}
377377

378378
func testRegisterShutdownAsync() {
@@ -386,7 +386,7 @@ final class Tests: XCTestCase {
386386
lifecycle.shutdown()
387387
}
388388
lifecycle.wait()
389-
XCTAssert(item.state == .shutdown, "expected item to be shutdown, but \(item.state)")
389+
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown, but \(item.state)")
390390
}
391391

392392
func testRegisterAsyncClosure() {
@@ -408,7 +408,7 @@ final class Tests: XCTestCase {
408408
lifecycle.shutdown()
409409
}
410410
lifecycle.wait()
411-
XCTAssert(item.state == .shutdown, "expected item to be shutdown, but \(item.state)")
411+
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown, but \(item.state)")
412412
}
413413

414414
func testRegisterShutdownAsyncClosure() {
@@ -425,7 +425,7 @@ final class Tests: XCTestCase {
425425
lifecycle.shutdown()
426426
}
427427
lifecycle.wait()
428-
XCTAssert(item.state == .shutdown, "expected item to be shutdown, but \(item.state)")
428+
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown, but \(item.state)")
429429
}
430430

431431
func testRegisterNIO() {
@@ -442,7 +442,7 @@ final class Tests: XCTestCase {
442442
lifecycle.shutdown()
443443
}
444444
lifecycle.wait()
445-
XCTAssert(item.state == .shutdown, "expected item to be shutdown, but \(item.state)")
445+
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown, but \(item.state)")
446446
}
447447

448448
func testRegisterShutdownNIO() {
@@ -457,7 +457,7 @@ final class Tests: XCTestCase {
457457
lifecycle.shutdown()
458458
}
459459
lifecycle.wait()
460-
XCTAssert(item.state == .shutdown, "expected item to be shutdown, but \(item.state)")
460+
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown, but \(item.state)")
461461
}
462462

463463
func testRegisterNIOClosure() {
@@ -480,7 +480,7 @@ final class Tests: XCTestCase {
480480
lifecycle.shutdown()
481481
}
482482
lifecycle.wait()
483-
XCTAssert(item.state == .shutdown, "expected item to be shutdown, but \(item.state)")
483+
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown, but \(item.state)")
484484
}
485485

486486
func testRegisterShutdownNIOClosure() {
@@ -498,7 +498,7 @@ final class Tests: XCTestCase {
498498
lifecycle.shutdown()
499499
}
500500
lifecycle.wait()
501-
XCTAssert(item.state == .shutdown, "expected item to be shutdown, but \(item.state)")
501+
XCTAssertEqual(item.state, .shutdown, "expected item to be shutdown, but \(item.state)")
502502
}
503503

504504
func testNIOFailure() {
@@ -515,6 +515,56 @@ final class Tests: XCTestCase {
515515
}
516516
lifecycle.wait()
517517
}
518+
519+
func testExternalState() {
520+
enum State: Equatable {
521+
case idle
522+
case started(String)
523+
case shutdown
524+
}
525+
526+
class Item {
527+
let eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
528+
529+
let data: String
530+
init(_ data: String) {
531+
self.data = data
532+
}
533+
534+
func start() -> EventLoopFuture<String> {
535+
return self.eventLoopGroup.next().makeSucceededFuture(self.data)
536+
}
537+
538+
func shutdown() -> EventLoopFuture<Void> {
539+
return self.eventLoopGroup.next().makeSucceededFuture(())
540+
}
541+
}
542+
543+
var state = State.idle
544+
545+
let expectedData = UUID().uuidString
546+
let item = Item(expectedData)
547+
let lifecycle = Lifecycle()
548+
lifecycle.register(name: "test",
549+
start: .async {
550+
item.start().map { data -> Void in
551+
state = .started(data)
552+
}
553+
},
554+
shutdown: .async {
555+
item.shutdown().map { _ -> Void in
556+
state = .shutdown
557+
}
558+
})
559+
560+
lifecycle.start(configuration: .init(shutdownSignal: nil)) { error in
561+
XCTAssertNil(error, "not expecting error")
562+
XCTAssertEqual(state, .started(expectedData), "expected item to be shutdown, but \(state)")
563+
lifecycle.shutdown()
564+
}
565+
lifecycle.wait()
566+
XCTAssertEqual(state, .shutdown, "expected item to be shutdown, but \(state)")
567+
}
518568
}
519569

520570
private class GoodItem: LifecycleItem {

0 commit comments

Comments
 (0)