Skip to content

Commit eabd681

Browse files
committed
Workaround SR-15070
1 parent cffd3f3 commit eabd681

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

Tests/GRPCTests/GRPCAsyncClientCallTests.swift

+22-8
Original file line numberDiff line numberDiff line change
@@ -178,33 +178,47 @@ class GRPCAsyncClientCallTests: GRPCTestCase {
178178

179179
await assertThat(try await update.initialMetadata, .is(.equalTo(Self.OKInitialMetadata)))
180180

181-
actor TestResults {
182-
static var numResponses = 0
183-
static var numRequests = 0
184-
}
181+
let counter = RequestResponseCounter()
185182

186183
// Send the requests and get responses in separate concurrent tasks and await the group.
187184
_ = await withThrowingTaskGroup(of: Void.self) { taskGroup in
188185
// Send requests, then end, in a task.
189186
taskGroup.addTask {
190187
for word in ["boyle", "jeffers", "holt"] {
191188
try await update.sendMessage(.with { $0.text = word })
192-
TestResults.numRequests += 1
189+
await counter.incrementRequests()
193190
}
194191
try await update.sendEnd()
195192
}
196193
// Get responses in a separate task.
197194
taskGroup.addTask {
198195
for try await _ in update.responses {
199-
TestResults.numResponses += 1
196+
await counter.incrementResponses()
200197
}
201198
}
202199
}
203-
await assertThat(TestResults.numRequests, .is(.equalTo(3)))
204-
await assertThat(TestResults.numResponses, .is(.equalTo(3)))
200+
201+
await assertThat(await counter.numRequests, .is(.equalTo(3)))
202+
await assertThat(await counter.numResponses, .is(.equalTo(3)))
205203
await assertThat(try await update.trailingMetadata, .is(.equalTo(Self.OKTrailingMetadata)))
206204
await assertThat(await update.status, .hasCode(.ok))
207205
} }
208206
}
209207

208+
// Workaround https://bugs.swift.org/browse/SR-15070 (compiler crashes when defining a class/actor
209+
// in an async context).
210+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
211+
fileprivate actor RequestResponseCounter {
212+
var numResponses = 0
213+
var numRequests = 0
214+
215+
func incrementResponses() async {
216+
self.numResponses += 1
217+
}
218+
219+
func incrementRequests() async {
220+
self.numRequests += 1
221+
}
222+
}
223+
210224
#endif

0 commit comments

Comments
 (0)