Skip to content

Commit

Permalink
Allow to finish() GRPCAsyncRequestStreamWriter sync, non-throwing (#…
Browse files Browse the repository at this point in the history
…1504)

* Add sync method variant for finishing RequestStreamWriter

* Adjust call sites to use sync RequestStreamWriter finish

* Remove async variant of GRPCAsyncRequestStreamWriter's .finish()

Co-authored-by: George Barnett <[email protected]>
  • Loading branch information
schwmi and glbrntt authored Oct 19, 2022
1 parent 930667f commit 95e6a82
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Sources/Examples/RouteGuide/Client/RouteGuideClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ extension RouteGuideExample {
}
}

try await recordRoute.requestStream.finish()
recordRoute.requestStream.finish()
let summary = try await recordRoute.response

print(
Expand Down Expand Up @@ -188,7 +188,7 @@ extension RouteGuideExample {
try await Task.sleep(nanoseconds: UInt64.random(in: UInt64(2e8) ... UInt64(1e9)))
}

try await routeChat.requestStream.finish()
routeChat.requestStream.finish()
}

// Add a task to print each message received on the response stream.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public struct GRPCAsyncRequestStreamWriter<Request: Sendable>: Sendable {
}

/// Finish the request stream for the RPC. This must be called when there are no more requests to be sent.
public func finish() async throws {
public func finish() {
self.asyncWriter.finish()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ extension GRPCClient {
for try await request in requests {
try await call.requestStream.send(request)
}
try await call.requestStream.finish()
call.requestStream.finish()
} catch {
// If we throw then cancel the call. We will rely on the response throwing an appropriate
// error below.
Expand Down Expand Up @@ -452,7 +452,7 @@ extension GRPCClient {
for try await request in requests {
try await call.requestStream.send(request)
}
try await call.requestStream.finish()
call.requestStream.finish()
} onCancel: {
call.cancel()
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/GRPCTests/AsyncAwaitSupport/AsyncClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ final class AsyncClientCancellationTests: GRPCTestCase {
let collect = echo.makeCollectCall()
// Send and close.
try await collect.requestStream.send(.with { $0.text = "foo" })
try await collect.requestStream.finish()
collect.requestStream.finish()

// Await the response and status.
_ = try await collect.response
Expand Down Expand Up @@ -294,7 +294,7 @@ final class AsyncClientCancellationTests: GRPCTestCase {
let update = echo.makeUpdateCall()
// Send and close.
try await update.requestStream.send(.with { $0.text = "foo" })
try await update.requestStream.finish()
update.requestStream.finish()

// Await the response and status.
let responseCount = try await update.responseStream.count()
Expand Down
6 changes: 3 additions & 3 deletions Tests/GRPCTests/AsyncAwaitSupport/AsyncIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ final class AsyncIntegrationTests: GRPCTestCase {
try await collect.requestStream.send(.with { $0.text = "boyle" })
try await collect.requestStream.send(.with { $0.text = "jeffers" })
try await collect.requestStream.send(.with { $0.text = "holt" })
try await collect.requestStream.finish()
collect.requestStream.finish()

let initialMetadata = try await collect.initialMetadata
initialMetadata.assertFirst("200", forName: ":status")
Expand Down Expand Up @@ -149,7 +149,7 @@ final class AsyncIntegrationTests: GRPCTestCase {
XCTAssertEqual(response, "Swift echo update (\(i)): \(name)")
}

try await update.requestStream.finish()
update.requestStream.finish()

// This isn't right after we make the call as servers are not guaranteed to send metadata back
// immediately. Concretely, we don't send initial metadata back until the first response
Expand Down Expand Up @@ -186,7 +186,7 @@ final class AsyncIntegrationTests: GRPCTestCase {
_ = try await update.responseStream.first(where: { _ in true })
XCTAssertNoThrow(try self.server.close().wait())
self.server = nil // So that tearDown() does not call close() again.
try await update.requestStream.finish()
update.requestStream.finish()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class InterceptorsAsyncTests: GRPCTestCase {
let call = self.echo.makeCollectCall(callOptions: .init())
try await call.requestStream.send(.with { $0.text = "1 2" })
try await call.requestStream.send(.with { $0.text = "3 4" })
try await call.requestStream.finish()
call.requestStream.finish()

await assertThat(
try await call.response,
Expand Down Expand Up @@ -153,7 +153,7 @@ class InterceptorsAsyncTests: GRPCTestCase {
let call = self.echo.makeUpdateCall(callOptions: .init())
try await call.requestStream.send(.with { $0.text = "1 2" })
try await call.requestStream.send(.with { $0.text = "3 4" })
try await call.requestStream.finish()
call.requestStream.finish()

var count = 0
for try await response in call.responseStream {
Expand Down
8 changes: 4 additions & 4 deletions Tests/GRPCTests/GRPCAsyncClientCallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class GRPCAsyncClientCallTests: GRPCTestCase {
for word in ["boyle", "jeffers", "holt"] {
try await collect.requestStream.send(.with { $0.text = word })
}
try await collect.requestStream.finish()
collect.requestStream.finish()

await assertThat(try await collect.initialMetadata, .is(.equalTo(Self.OKInitialMetadata)))
await assertThat(try await collect.response, .doesNotThrow())
Expand Down Expand Up @@ -138,7 +138,7 @@ class GRPCAsyncClientCallTests: GRPCTestCase {
try await update.requestStream.send(request)
}
try await update.requestStream.send(requests)
try await update.requestStream.finish()
update.requestStream.finish()

let numResponses = try await update.responseStream.map { _ in 1 }.reduce(0, +)

Expand All @@ -163,7 +163,7 @@ class GRPCAsyncClientCallTests: GRPCTestCase {
await assertThat(try await responseStreamIterator.next(), .is(.notNil()))
}

try await update.requestStream.finish()
update.requestStream.finish()

await assertThat(try await responseStreamIterator.next(), .is(.nil()))

Expand Down Expand Up @@ -191,7 +191,7 @@ class GRPCAsyncClientCallTests: GRPCTestCase {
try await update.requestStream.send(.with { $0.text = word })
await counter.incrementRequests()
}
try await update.requestStream.finish()
update.requestStream.finish()
}
// Get responses in a separate task.
taskGroup.addTask {
Expand Down

0 comments on commit 95e6a82

Please sign in to comment.