Skip to content

Commit bcf9d1b

Browse files
committed
Enable test: discarding task group making sure we deinit confidently
1 parent b6edb09 commit bcf9d1b

File tree

1 file changed

+75
-19
lines changed

1 file changed

+75
-19
lines changed
Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library) | %FileCheck %s
22
// TODO: move to target-run-simple-leaks-swift once CI is using at least Xcode 14.3
33

4-
// rdar://110025115 - Temporarily disable this test
5-
// REQUIRES: rdar110025115
6-
74
// REQUIRES: concurrency
85
// REQUIRES: executable_test
96
// REQUIRES: concurrency_runtime
@@ -19,29 +16,76 @@ import _Concurrency
1916
final class PayloadFirst {}
2017
final class PayloadSecond {}
2118

19+
actor SimpleCountDownLatch {
20+
let from: Int
21+
var count: Int
22+
23+
var continuation: CheckedContinuation<Void, Never>?
24+
25+
init(from: Int) {
26+
self.from = from
27+
self.count = from
28+
}
29+
30+
func hit() {
31+
defer { count -= 1 }
32+
print("hit @ \(count)")
33+
if count == 0 {
34+
fatalError("Counted down more times than expected! (From: \(from))")
35+
} else if count == 1 {
36+
print("hit resume")
37+
print("resume")
38+
continuation?.resume()
39+
}
40+
}
41+
42+
func wait() async {
43+
guard self.count > 0 else {
44+
return // we're done
45+
}
46+
47+
return await withCheckedContinuation { cc in
48+
self.continuation = cc
49+
}
50+
}
51+
}
52+
2253
final class ErrorFirst: Error {
2354
let first: PayloadFirst
55+
let id: String
56+
let latch: SimpleCountDownLatch
2457

25-
init(file: String = #fileID, line: UInt = #line) {
58+
init(latch: SimpleCountDownLatch, file: String = #fileID, line: UInt = #line) {
59+
self.latch = latch
60+
self.id = "\(file):\(line)"
2661
first = .init()
62+
print("init \(self) id:\(id)")
2763
}
2864
deinit {
29-
print("deinit \(self)")
65+
print("deinit \(self) id:\(id)")
66+
Task { [latch] in await latch.hit() }
3067
}
3168
}
3269

70+
// Should not really matter that different types, but want to make really sure
3371
final class ErrorSecond: Error {
34-
let second: PayloadSecond
72+
let first: PayloadFirst
73+
let id: String
74+
let latch: SimpleCountDownLatch
3575

36-
init(file: String = #fileID, line: UInt = #line) {
37-
second = .init()
76+
init(latch: SimpleCountDownLatch, file: String = #fileID, line: UInt = #line) {
77+
self.latch = latch
78+
self.id = "\(file):\(line)"
79+
first = .init()
80+
print("init \(self) id:\(id)")
3881
}
39-
4082
deinit {
41-
print("deinit \(self)")
83+
print("deinit \(self) id:\(id)")
84+
Task { [latch] in await latch.hit() }
4285
}
4386
}
4487

88+
4589
func shouldStartWith(_ lhs: Any, _ rhs: Any) {
4690
let l = "\(lhs)"
4791
let r = "\(rhs)"
@@ -50,20 +94,25 @@ func shouldStartWith(_ lhs: Any, _ rhs: Any) {
5094

5195
// NOTE: Not as StdlibUnittest/TestSuite since these types of tests are unreasonably slow to load/debug.
5296

97+
@discardableResult
98+
func one() -> Int {
99+
1
100+
}
101+
53102
@main struct Main {
54103
static func main() async {
104+
let latch = SimpleCountDownLatch(from: 6)
55105
do {
106+
56107
let got = try await withThrowingDiscardingTaskGroup() { group in
57-
group.addTask {
58-
1
59-
}
60-
group.addTask {
61-
throw ErrorFirst()
62-
}
108+
group.addTask { one() }
109+
group.addTask { throw ErrorFirst(latch: latch) }
110+
group.addTask { throw ErrorFirst(latch: latch) }
111+
group.addTask { throw ErrorFirst(latch: latch) }
63112

64-
group.addTask {
65-
throw ErrorSecond()
66-
}
113+
group.addTask { throw ErrorSecond(latch: latch) }
114+
group.addTask { throw ErrorSecond(latch: latch) }
115+
group.addTask { throw ErrorSecond(latch: latch) }
67116

68117
return 12
69118
}
@@ -73,5 +122,12 @@ func shouldStartWith(_ lhs: Any, _ rhs: Any) {
73122
}
74123
// CHECK: deinit main.Error
75124
// CHECK: deinit main.Error
125+
// CHECK: deinit main.Error
126+
127+
// CHECK: deinit main.Error
128+
// CHECK: deinit main.Error
129+
// CHECK: deinit main.Error
130+
await latch.wait()
131+
print("done") // CHECK: done
76132
}
77133
}

0 commit comments

Comments
 (0)