Skip to content

Commit c62901a

Browse files
authored
rename LifecycleItem::name to LifecycleItem:label (#22)
motivation: more swifty API, along the lines of DispatchQueue, Logger, Metrics changes: * rename LifecycleItem::name to LifecycleItem:label * adjust tests
1 parent 35ea48e commit c62901a

File tree

2 files changed

+41
-51
lines changed

2 files changed

+41
-51
lines changed

Sources/ServiceLauncher/Lifecycle.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ public class Lifecycle {
150150
if index >= items.count {
151151
return callback(index, nil)
152152
}
153-
self.logger.info("starting item [\(items[index].name)]")
153+
self.logger.info("starting item [\(items[index].label)]")
154154
start { error in
155155
if let error = error {
156-
self.logger.info("failed to start [\(items[index].name)]: \(error)")
156+
self.logger.info("failed to start [\(items[index].label)]: \(error)")
157157
return callback(index, error)
158158
}
159159
// shutdown called while starting
@@ -192,10 +192,10 @@ public class Lifecycle {
192192
if index >= items.count {
193193
return callback()
194194
}
195-
self.logger.info("stopping item [\(items[index].name)]")
195+
self.logger.info("stopping item [\(items[index].label)]")
196196
shutdown { error in
197197
if let error = error {
198-
self.logger.info("failed to stop [\(items[index].name)]: \(error)")
198+
self.logger.info("failed to stop [\(items[index].label)]: \(error)")
199199
}
200200
self._shutdown(on: queue, items: items, index: index + 1, callback: callback)
201201
}
@@ -212,7 +212,7 @@ public class Lifecycle {
212212

213213
extension Lifecycle {
214214
internal struct Item: LifecycleItem {
215-
let name: String
215+
let label: String
216216
let start: Handler
217217
let shutdown: Handler
218218

@@ -274,39 +274,39 @@ public extension Lifecycle {
274274
/// Add a `LifecycleItem` to a `LifecycleItems` collection.
275275
///
276276
/// - parameters:
277-
/// - name: name of the item, useful for debugging.
277+
/// - label: label of the item, useful for debugging.
278278
/// - start: closure to perform the startup.
279279
/// - shutdown: closure to perform the shutdown.
280-
func register(name: String, start: Handler, shutdown: Handler) {
281-
self.register(Item(name: name, start: start, shutdown: shutdown))
280+
func register(label: String, start: Handler, shutdown: Handler) {
281+
self.register(Item(label: label, start: start, shutdown: shutdown))
282282
}
283283

284284
/// Adds a `LifecycleItem` to a `LifecycleItems` collection.
285285
///
286286
/// - parameters:
287-
/// - name: name of the item, useful for debugging.
287+
/// - label: label of the item, useful for debugging.
288288
/// - shutdown: closure to perform the shutdown.
289-
func registerShutdown(name: String, _ handler: Handler) {
290-
self.register(name: name, start: .none, shutdown: handler)
289+
func registerShutdown(label: String, _ handler: Handler) {
290+
self.register(label: label, start: .none, shutdown: handler)
291291
}
292292

293293
/// Adds a `LifecycleItem` to a `LifecycleItems` collection.
294294
///
295295
/// - parameters:
296-
/// - name: name of the item, useful for debugging.
296+
/// - label: label of the item, useful for debugging.
297297
/// - start: closure to perform the shutdown.
298298
/// - shutdown: closure to perform the shutdown.
299-
func register(name: String, start: @escaping () throws -> Void, shutdown: @escaping () throws -> Void) {
300-
self.register(name: name, start: .sync(start), shutdown: .sync(shutdown))
299+
func register(label: String, start: @escaping () throws -> Void, shutdown: @escaping () throws -> Void) {
300+
self.register(label: label, start: .sync(start), shutdown: .sync(shutdown))
301301
}
302302

303303
/// Adds a `LifecycleItem` to a `LifecycleItems` collection.
304304
///
305305
/// - parameters:
306-
/// - name: name of the item, useful for debugging.
306+
/// - label: label of the item, useful for debugging.
307307
/// - shutdown: closure to perform the shutdown.
308-
func registerShutdown(name: String, _ handler: @escaping () throws -> Void) {
309-
self.register(name: name, start: .none, shutdown: .sync(handler))
308+
func registerShutdown(label: String, _ handler: @escaping () throws -> Void) {
309+
self.register(label: label, start: .none, shutdown: .sync(handler))
310310
}
311311
}
312312

@@ -361,7 +361,7 @@ public extension Lifecycle {
361361

362362
/// Represents an item that can be started and shut down
363363
public protocol LifecycleItem {
364-
var name: String { get }
364+
var label: String { get }
365365
func start(callback: @escaping (Error?) -> Void)
366366
func shutdown(callback: @escaping (Error?) -> Void)
367367
}

Tests/ServiceLauncherTests/LifecycleTests.swift

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ final class Tests: XCTestCase {
5858

5959
func testBadStartup() {
6060
class BadItem: LifecycleItem {
61-
var name: String {
62-
return "\(self)"
63-
}
61+
let label: String = UUID().uuidString
6462

6563
func start(callback: (Error?) -> Void) {
6664
callback(TestError())
@@ -84,9 +82,7 @@ final class Tests: XCTestCase {
8482

8583
func testBadShutdown() {
8684
class BadItem: LifecycleItem {
87-
var name: String {
88-
return "\(self)"
89-
}
85+
let label: String = UUID().uuidString
9086

9187
func start(callback: (Error?) -> Void) {
9288
callback(nil)
@@ -117,9 +113,7 @@ final class Tests: XCTestCase {
117113
self.semaphore = semaphore
118114
}
119115

120-
var name: String {
121-
return "\(self)"
122-
}
116+
let label: String = UUID().uuidString
123117

124118
func start(callback: (Error?) -> Void) {
125119
self.state = .started
@@ -153,9 +147,7 @@ final class Tests: XCTestCase {
153147

154148
func testBadStartAndWait() {
155149
class BadItem: LifecycleItem {
156-
var name: String {
157-
return "\(self)"
158-
}
150+
let label: String = UUID().uuidString
159151

160152
func start(callback: (Error?) -> Void) {
161153
callback(TestError())
@@ -183,8 +175,8 @@ final class Tests: XCTestCase {
183175
self.result = result
184176
}
185177

186-
var name: String {
187-
return "\(self.id)"
178+
var label: String {
179+
return self.id
188180
}
189181

190182
func start(callback: (Error?) -> Void) {
@@ -239,7 +231,7 @@ final class Tests: XCTestCase {
239231
let lifecycle = Lifecycle()
240232
let items = (0 ... Int.random(in: 10 ... 20)).map { _ in Sync() }
241233
items.forEach { item in
242-
lifecycle.register(name: item.id, start: item.start, shutdown: item.shutdown)
234+
lifecycle.register(label: item.id, start: item.start, shutdown: item.shutdown)
243235
}
244236

245237
lifecycle.start(configuration: .init(shutdownSignal: nil)) { error in
@@ -255,14 +247,14 @@ final class Tests: XCTestCase {
255247
let lifecycle = Lifecycle()
256248

257249
let item1 = NIOItem(eventLoopGroup: eventLoopGroup)
258-
lifecycle.register(name: "item1", start: .eventLoopFuture(item1.start), shutdown: .eventLoopFuture(item1.shutdown))
250+
lifecycle.register(label: "item1", start: .eventLoopFuture(item1.start), shutdown: .eventLoopFuture(item1.shutdown))
259251

260-
lifecycle.register(name: "blocker",
252+
lifecycle.register(label: "blocker",
261253
start: { () -> Void in try eventLoopGroup.next().makeSucceededFuture(()).wait() },
262254
shutdown: { () -> Void in try eventLoopGroup.next().makeSucceededFuture(()).wait() })
263255

264256
let item2 = NIOItem(eventLoopGroup: eventLoopGroup)
265-
lifecycle.register(name: "item2", start: .eventLoopFuture(item2.start), shutdown: .eventLoopFuture(item2.shutdown))
257+
lifecycle.register(label: "item2", start: .eventLoopFuture(item2.start), shutdown: .eventLoopFuture(item2.shutdown))
266258

267259
lifecycle.start(configuration: .init(shutdownSignal: nil)) { error in
268260
XCTAssertNil(error, "not expecting error")
@@ -315,7 +307,7 @@ final class Tests: XCTestCase {
315307
let lifecycle = Lifecycle()
316308

317309
let item = Sync()
318-
lifecycle.register(name: "test",
310+
lifecycle.register(label: "test",
319311
start: item.start,
320312
shutdown: item.shutdown)
321313

@@ -349,7 +341,7 @@ final class Tests: XCTestCase {
349341
let lifecycle = Lifecycle()
350342

351343
let item = Sync()
352-
lifecycle.registerShutdown(name: "test", item.shutdown)
344+
lifecycle.registerShutdown(label: "test", item.shutdown)
353345

354346
lifecycle.start(configuration: .init(shutdownSignal: nil)) { error in
355347
XCTAssertNil(error, "not expecting error")
@@ -363,7 +355,7 @@ final class Tests: XCTestCase {
363355
let lifecycle = Lifecycle()
364356

365357
let item = GoodItem()
366-
lifecycle.register(name: "test",
358+
lifecycle.register(label: "test",
367359
start: .async(item.start),
368360
shutdown: .async(item.shutdown))
369361

@@ -379,7 +371,7 @@ final class Tests: XCTestCase {
379371
let lifecycle = Lifecycle()
380372

381373
let item = GoodItem()
382-
lifecycle.registerShutdown(name: "test", .async(item.shutdown))
374+
lifecycle.registerShutdown(label: "test", .async(item.shutdown))
383375

384376
lifecycle.start(configuration: .init(shutdownSignal: nil)) { error in
385377
XCTAssertNil(error, "not expecting error")
@@ -393,7 +385,7 @@ final class Tests: XCTestCase {
393385
let lifecycle = Lifecycle()
394386

395387
let item = GoodItem()
396-
lifecycle.register(name: "test",
388+
lifecycle.register(label: "test",
397389
start: .async { callback in
398390
print("start")
399391
item.start(callback: callback)
@@ -415,7 +407,7 @@ final class Tests: XCTestCase {
415407
let lifecycle = Lifecycle()
416408

417409
let item = GoodItem()
418-
lifecycle.registerShutdown(name: "test", .async { callback in
410+
lifecycle.registerShutdown(label: "test", .async { callback in
419411
print("shutdown")
420412
item.shutdown(callback: callback)
421413
})
@@ -433,7 +425,7 @@ final class Tests: XCTestCase {
433425
let lifecycle = Lifecycle()
434426

435427
let item = NIOItem(eventLoopGroup: eventLoopGroup)
436-
lifecycle.register(name: item.id,
428+
lifecycle.register(label: item.id,
437429
start: .eventLoopFuture(item.start),
438430
shutdown: .eventLoopFuture(item.shutdown))
439431

@@ -450,7 +442,7 @@ final class Tests: XCTestCase {
450442
let lifecycle = Lifecycle()
451443

452444
let item = NIOItem(eventLoopGroup: eventLoopGroup)
453-
lifecycle.registerShutdown(name: item.id, .eventLoopFuture(item.shutdown))
445+
lifecycle.registerShutdown(label: item.id, .eventLoopFuture(item.shutdown))
454446

455447
lifecycle.start(configuration: .init(shutdownSignal: nil)) { error in
456448
XCTAssertNil(error, "not expecting error")
@@ -465,7 +457,7 @@ final class Tests: XCTestCase {
465457
let lifecycle = Lifecycle()
466458

467459
let item = NIOItem(eventLoopGroup: eventLoopGroup)
468-
lifecycle.register(name: item.id,
460+
lifecycle.register(label: item.id,
469461
start: .eventLoopFuture {
470462
print("start")
471463
return item.start()
@@ -488,7 +480,7 @@ final class Tests: XCTestCase {
488480
let lifecycle = Lifecycle()
489481

490482
let item = NIOItem(eventLoopGroup: eventLoopGroup)
491-
lifecycle.registerShutdown(name: item.id, .eventLoopFuture {
483+
lifecycle.registerShutdown(label: item.id, .eventLoopFuture {
492484
print("shutdown")
493485
return item.shutdown()
494486
})
@@ -505,7 +497,7 @@ final class Tests: XCTestCase {
505497
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
506498
let lifecycle = Lifecycle()
507499

508-
lifecycle.register(name: "test",
500+
lifecycle.register(label: "test",
509501
start: .eventLoopFuture { eventLoopGroup.next().makeFailedFuture(TestError()) },
510502
shutdown: .eventLoopFuture { eventLoopGroup.next().makeSucceededFuture(()) })
511503

@@ -545,7 +537,7 @@ final class Tests: XCTestCase {
545537
let expectedData = UUID().uuidString
546538
let item = Item(expectedData)
547539
let lifecycle = Lifecycle()
548-
lifecycle.register(name: "test",
540+
lifecycle.register(label: "test",
549541
start: .eventLoopFuture {
550542
item.start().map { data -> Void in
551543
state = .started(data)
@@ -580,9 +572,7 @@ private class GoodItem: LifecycleItem {
580572
self.shutdownDelay = shutdownDelay
581573
}
582574

583-
var name: String {
584-
return "\(GoodItem.self)"
585-
}
575+
let label: String = UUID().uuidString
586576

587577
func start(callback: @escaping (Error?) -> Void) {
588578
self.queue.asyncAfter(deadline: .now() + self.startDelay) {

0 commit comments

Comments
 (0)