Skip to content

Commit 8fcf4f3

Browse files
authored
Use new XCTest async/await support (#1336)
Motivation: Swift 5.5.2 includes XCTest support for async/await. Currently we are working around the lack of support with XCTest expectations. Modifications: - Remove the helper - Make async tests `async throws` Result: Fewer workarounds.
1 parent 61f63a7 commit 8fcf4f3

7 files changed

+405
-487
lines changed

Tests/GRPCTests/AsyncAwaitSupport/AsyncIntegrationTests.swift

Lines changed: 96 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -55,156 +55,138 @@ final class AsyncIntegrationTests: GRPCTestCase {
5555
super.tearDown()
5656
}
5757

58-
func testUnary() {
59-
XCTAsyncTest {
60-
let get = self.echo.makeGetCall(.with { $0.text = "hello" })
58+
func testUnary() async throws {
59+
let get = self.echo.makeGetCall(.with { $0.text = "hello" })
6160

62-
let initialMetadata = try await get.initialMetadata
63-
initialMetadata.assertFirst("200", forName: ":status")
61+
let initialMetadata = try await get.initialMetadata
62+
initialMetadata.assertFirst("200", forName: ":status")
6463

65-
let response = try await get.response
66-
XCTAssertEqual(response.text, "Swift echo get: hello")
64+
let response = try await get.response
65+
XCTAssertEqual(response.text, "Swift echo get: hello")
6766

68-
let trailingMetadata = try await get.trailingMetadata
69-
trailingMetadata.assertFirst("0", forName: "grpc-status")
67+
let trailingMetadata = try await get.trailingMetadata
68+
trailingMetadata.assertFirst("0", forName: "grpc-status")
7069

71-
let status = await get.status
72-
XCTAssertTrue(status.isOk)
73-
}
70+
let status = await get.status
71+
XCTAssertTrue(status.isOk)
7472
}
7573

76-
func testUnaryWrapper() {
77-
XCTAsyncTest {
78-
let response = try await self.echo.get(.with { $0.text = "hello" })
79-
XCTAssertEqual(response.text, "Swift echo get: hello")
80-
}
74+
func testUnaryWrapper() async throws {
75+
let response = try await self.echo.get(.with { $0.text = "hello" })
76+
XCTAssertEqual(response.text, "Swift echo get: hello")
8177
}
8278

83-
func testClientStreaming() {
84-
XCTAsyncTest {
85-
let collect = self.echo.makeCollectCall()
79+
func testClientStreaming() async throws {
80+
let collect = self.echo.makeCollectCall()
8681

87-
try await collect.requestStream.send(.with { $0.text = "boyle" })
88-
try await collect.requestStream.send(.with { $0.text = "jeffers" })
89-
try await collect.requestStream.send(.with { $0.text = "holt" })
90-
try await collect.requestStream.finish()
82+
try await collect.requestStream.send(.with { $0.text = "boyle" })
83+
try await collect.requestStream.send(.with { $0.text = "jeffers" })
84+
try await collect.requestStream.send(.with { $0.text = "holt" })
85+
try await collect.requestStream.finish()
9186

92-
let initialMetadata = try await collect.initialMetadata
93-
initialMetadata.assertFirst("200", forName: ":status")
87+
let initialMetadata = try await collect.initialMetadata
88+
initialMetadata.assertFirst("200", forName: ":status")
9489

95-
let response = try await collect.response
96-
XCTAssertEqual(response.text, "Swift echo collect: boyle jeffers holt")
90+
let response = try await collect.response
91+
XCTAssertEqual(response.text, "Swift echo collect: boyle jeffers holt")
9792

98-
let trailingMetadata = try await collect.trailingMetadata
99-
trailingMetadata.assertFirst("0", forName: "grpc-status")
93+
let trailingMetadata = try await collect.trailingMetadata
94+
trailingMetadata.assertFirst("0", forName: "grpc-status")
10095

101-
let status = await collect.status
102-
XCTAssertTrue(status.isOk)
103-
}
96+
let status = await collect.status
97+
XCTAssertTrue(status.isOk)
10498
}
10599

106-
func testClientStreamingWrapper() {
107-
XCTAsyncTest {
108-
let requests: [Echo_EchoRequest] = [
109-
.with { $0.text = "boyle" },
110-
.with { $0.text = "jeffers" },
111-
.with { $0.text = "holt" },
112-
]
100+
func testClientStreamingWrapper() async throws {
101+
let requests: [Echo_EchoRequest] = [
102+
.with { $0.text = "boyle" },
103+
.with { $0.text = "jeffers" },
104+
.with { $0.text = "holt" },
105+
]
113106

114-
let response = try await self.echo.collect(requests)
115-
XCTAssertEqual(response.text, "Swift echo collect: boyle jeffers holt")
116-
}
107+
let response = try await self.echo.collect(requests)
108+
XCTAssertEqual(response.text, "Swift echo collect: boyle jeffers holt")
117109
}
118110

119-
func testServerStreaming() {
120-
XCTAsyncTest {
121-
let expand = self.echo.makeExpandCall(.with { $0.text = "boyle jeffers holt" })
111+
func testServerStreaming() async throws {
112+
let expand = self.echo.makeExpandCall(.with { $0.text = "boyle jeffers holt" })
122113

123-
let initialMetadata = try await expand.initialMetadata
124-
initialMetadata.assertFirst("200", forName: ":status")
114+
let initialMetadata = try await expand.initialMetadata
115+
initialMetadata.assertFirst("200", forName: ":status")
125116

126-
let responses = try await expand.responseStream.map { $0.text }.collect()
127-
XCTAssertEqual(responses, [
128-
"Swift echo expand (0): boyle",
129-
"Swift echo expand (1): jeffers",
130-
"Swift echo expand (2): holt",
131-
])
117+
let responses = try await expand.responseStream.map { $0.text }.collect()
118+
XCTAssertEqual(responses, [
119+
"Swift echo expand (0): boyle",
120+
"Swift echo expand (1): jeffers",
121+
"Swift echo expand (2): holt",
122+
])
132123

133-
let trailingMetadata = try await expand.trailingMetadata
134-
trailingMetadata.assertFirst("0", forName: "grpc-status")
124+
let trailingMetadata = try await expand.trailingMetadata
125+
trailingMetadata.assertFirst("0", forName: "grpc-status")
135126

136-
let status = await expand.status
137-
XCTAssertTrue(status.isOk)
138-
}
127+
let status = await expand.status
128+
XCTAssertTrue(status.isOk)
139129
}
140130

141-
func testServerStreamingWrapper() {
142-
XCTAsyncTest {
143-
let responseStream = self.echo.expand(.with { $0.text = "boyle jeffers holt" })
144-
let responses = try await responseStream.map { $0.text }.collect()
145-
XCTAssertEqual(responses, [
146-
"Swift echo expand (0): boyle",
147-
"Swift echo expand (1): jeffers",
148-
"Swift echo expand (2): holt",
149-
])
150-
}
131+
func testServerStreamingWrapper() async throws {
132+
let responseStream = self.echo.expand(.with { $0.text = "boyle jeffers holt" })
133+
let responses = try await responseStream.map { $0.text }.collect()
134+
XCTAssertEqual(responses, [
135+
"Swift echo expand (0): boyle",
136+
"Swift echo expand (1): jeffers",
137+
"Swift echo expand (2): holt",
138+
])
151139
}
152140

153-
func testBidirectionalStreaming() {
154-
XCTAsyncTest {
155-
let update = self.echo.makeUpdateCall()
141+
func testBidirectionalStreaming() async throws {
142+
let update = self.echo.makeUpdateCall()
156143

157-
var responseIterator = update.responseStream.map { $0.text }.makeAsyncIterator()
144+
var responseIterator = update.responseStream.map { $0.text }.makeAsyncIterator()
158145

159-
for (i, name) in ["boyle", "jeffers", "holt"].enumerated() {
160-
try await update.requestStream.send(.with { $0.text = name })
161-
let response = try await responseIterator.next()
162-
XCTAssertEqual(response, "Swift echo update (\(i)): \(name)")
163-
}
146+
for (i, name) in ["boyle", "jeffers", "holt"].enumerated() {
147+
try await update.requestStream.send(.with { $0.text = name })
148+
let response = try await responseIterator.next()
149+
XCTAssertEqual(response, "Swift echo update (\(i)): \(name)")
150+
}
164151

165-
try await update.requestStream.finish()
152+
try await update.requestStream.finish()
166153

167-
// This isn't right after we make the call as servers are not guaranteed to send metadata back
168-
// immediately. Concretely, we don't send initial metadata back until the first response
169-
// message is sent by the server.
170-
let initialMetadata = try await update.initialMetadata
171-
initialMetadata.assertFirst("200", forName: ":status")
154+
// This isn't right after we make the call as servers are not guaranteed to send metadata back
155+
// immediately. Concretely, we don't send initial metadata back until the first response
156+
// message is sent by the server.
157+
let initialMetadata = try await update.initialMetadata
158+
initialMetadata.assertFirst("200", forName: ":status")
172159

173-
let trailingMetadata = try await update.trailingMetadata
174-
trailingMetadata.assertFirst("0", forName: "grpc-status")
160+
let trailingMetadata = try await update.trailingMetadata
161+
trailingMetadata.assertFirst("0", forName: "grpc-status")
175162

176-
let status = await update.status
177-
XCTAssertTrue(status.isOk)
178-
}
163+
let status = await update.status
164+
XCTAssertTrue(status.isOk)
179165
}
180166

181-
func testBidirectionalStreamingWrapper() {
182-
XCTAsyncTest {
183-
let requests: [Echo_EchoRequest] = [
184-
.with { $0.text = "boyle" },
185-
.with { $0.text = "jeffers" },
186-
.with { $0.text = "holt" },
187-
]
188-
189-
let responseStream = self.echo.update(requests)
190-
let responses = try await responseStream.map { $0.text }.collect()
191-
XCTAssertEqual(responses, [
192-
"Swift echo update (0): boyle",
193-
"Swift echo update (1): jeffers",
194-
"Swift echo update (2): holt",
195-
])
196-
}
167+
func testBidirectionalStreamingWrapper() async throws {
168+
let requests: [Echo_EchoRequest] = [
169+
.with { $0.text = "boyle" },
170+
.with { $0.text = "jeffers" },
171+
.with { $0.text = "holt" },
172+
]
173+
174+
let responseStream = self.echo.update(requests)
175+
let responses = try await responseStream.map { $0.text }.collect()
176+
XCTAssertEqual(responses, [
177+
"Swift echo update (0): boyle",
178+
"Swift echo update (1): jeffers",
179+
"Swift echo update (2): holt",
180+
])
197181
}
198182

199-
func testServerCloseAfterMessage() {
200-
XCTAsyncTest {
201-
let update = self.echo.makeUpdateCall()
202-
try await update.requestStream.send(.with { $0.text = "hello" })
203-
_ = try await update.responseStream.first(where: { _ in true })
204-
XCTAssertNoThrow(try self.server.close().wait())
205-
self.server = nil // So that tearDown() does not call close() again.
206-
try await update.requestStream.finish()
207-
}
183+
func testServerCloseAfterMessage() async throws {
184+
let update = self.echo.makeUpdateCall()
185+
try await update.requestStream.send(.with { $0.text = "hello" })
186+
_ = try await update.responseStream.first(where: { _ in true })
187+
XCTAssertNoThrow(try self.server.close().wait())
188+
self.server = nil // So that tearDown() does not call close() again.
189+
try await update.requestStream.finish()
208190
}
209191
}
210192

0 commit comments

Comments
 (0)