Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

xtool.git / Tests / XToolTests / ShutdownRegistryTests.swift

ShutdownRegistryTests.swift41 lines · 1.2 KiBRaw
1import Testing
2import XCLIUtils
3
4struct ShutdownRegistryTests {
5 @Test
6 func concurrentShutdownRunsCleanupOnceInReverseOrder() async {
7 let registry = ShutdownRegistry()
8 let events = ShutdownEvents()
9 registry.register { await events.append(1) }
10 registry.register { await events.append(2) }
11 await withTaskGroup(of: Void.self) { group in
12 for _ in 0..<3 { group.addTask { await registry.shutdown() } }
13 }
14 #expect(await events.values == [2, 1])
15 await registry.shutdown()
16 #expect(await events.values == [2, 1])
17 }
18
19 @Test
20 func concurrentRegistrationsAreAllIncludedInShutdown() async {
21 let registry = ShutdownRegistry()
22 let events = ShutdownEvents()
23 await withTaskGroup(of: Void.self) { group in
24 for value in 0..<100 {
25 group.addTask {
26 registry.register { await events.append(value) }
27 }
28 }
29 }
30 await registry.shutdown()
31 #expect(await events.values.sorted() == Array(0..<100))
32 }
33}
34
35private actor ShutdownEvents {
36 var values: [Int] = []
37
38 func append(_ value: Int) {
39 values.append(value)
40 }
41}