Skip to content

Commit 78785d7

Browse files
committed
Add a convenience method for creating in-process client/server (grpc#1738)
Motivation: To use the in-process transport users must create a server and then a client depending on that server. It rarely makes sense to create one without the other and I have written more or less the same extension in three places to do this for me and return a pair. This should just be made into API. Modifications: - Add a `InProcessTransport.makePair()` convenience method - Replace use of old helpers Result: It's easier to create in-process transport.
1 parent 962b0b4 commit 78785d7

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2023, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import GRPCCore
17+
18+
public enum InProcessTransport {
19+
/// Returns a pair containing an ``InProcessServerTransport`` and an ``InProcessClientTransport``.
20+
///
21+
/// This function is purely for convenience and does no more than constructing a server transport
22+
/// and a client using that server transport.
23+
///
24+
/// - Parameters:
25+
/// - methodConfiguration: Method specific configuration used by the client transport to
26+
/// determine how RPCs should be executed.
27+
/// - retryThrottle: The retry throttle the client transport uses to determine whether a call
28+
/// should be retried.
29+
/// - Returns: A tuple containing the connected server and client in-process transports.
30+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
31+
public static func makePair(
32+
methodConfiguration: MethodConfigurations = MethodConfigurations(),
33+
retryThrottle: RetryThrottle? = nil
34+
) -> (server: InProcessServerTransport, client: InProcessClientTransport) {
35+
let server = InProcessServerTransport()
36+
let client = InProcessClientTransport(
37+
server: server,
38+
methodConfiguration: methodConfiguration,
39+
retryThrottle: retryThrottle
40+
)
41+
return (server, client)
42+
}
43+
}

Tests/GRPCCoreTests/GRPCClientTests.swift

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,12 @@ import XCTest
2020

2121
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
2222
final class GRPCClientTests: XCTestCase {
23-
func makeInProcessPair() -> (client: InProcessClientTransport, server: InProcessServerTransport) {
24-
let server = InProcessServerTransport()
25-
let client = InProcessClientTransport(
26-
server: server,
27-
methodConfiguration: MethodConfigurations()
28-
)
29-
30-
return (client, server)
31-
}
32-
3323
func withInProcessConnectedClient(
3424
services: [any RegistrableRPCService],
3525
interceptors: [any ClientInterceptor] = [],
3626
_ body: (GRPCClient, GRPCServer) async throws -> Void
3727
) async throws {
38-
let inProcess = self.makeInProcessPair()
28+
let inProcess = InProcessTransport.makePair()
3929
let client = GRPCClient(transport: inProcess.client, interceptors: interceptors)
4030
let server = GRPCServer(transports: [inProcess.server], services: services)
4131

@@ -325,7 +315,7 @@ final class GRPCClientTests: XCTestCase {
325315
}
326316

327317
func testCancelRunningClient() async throws {
328-
let inProcess = self.makeInProcessPair()
318+
let inProcess = InProcessTransport.makePair()
329319
let client = GRPCClient(transport: inProcess.client)
330320

331321
try await withThrowingTaskGroup(of: Void.self) { group in
@@ -374,7 +364,7 @@ final class GRPCClientTests: XCTestCase {
374364
}
375365

376366
func testRunStoppedClient() async throws {
377-
let (clientTransport, _) = self.makeInProcessPair()
367+
let (_, clientTransport) = InProcessTransport.makePair()
378368
let client = GRPCClient(transport: clientTransport)
379369
// Run the client.
380370
let task = Task { try await client.run() }
@@ -390,7 +380,7 @@ final class GRPCClientTests: XCTestCase {
390380
}
391381

392382
func testRunAlreadyRunningClient() async throws {
393-
let (clientTransport, _) = self.makeInProcessPair()
383+
let (_, clientTransport) = InProcessTransport.makePair()
394384
let client = GRPCClient(transport: clientTransport)
395385
// Run the client.
396386
let task = Task { try await client.run() }

Tests/GRPCCoreTests/GRPCServerTests.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,12 @@ import XCTest
2020

2121
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
2222
final class GRPCServerTests: XCTestCase {
23-
func makeInProcessPair() -> (client: InProcessClientTransport, server: InProcessServerTransport) {
24-
let server = InProcessServerTransport()
25-
let client = InProcessClientTransport(server: server)
26-
27-
return (client, server)
28-
}
29-
3023
func withInProcessClientConnectedToServer(
3124
services: [any RegistrableRPCService],
3225
interceptors: [any ServerInterceptor] = [],
3326
_ body: (InProcessClientTransport, GRPCServer) async throws -> Void
3427
) async throws {
35-
let inProcess = self.makeInProcessPair()
28+
let inProcess = InProcessTransport.makePair()
3629
let server = GRPCServer(
3730
transports: [inProcess.server],
3831
services: services,
@@ -298,7 +291,7 @@ final class GRPCServerTests: XCTestCase {
298291
}
299292

300293
func testCancelRunningServer() async throws {
301-
let inProcess = self.makeInProcessPair()
294+
let inProcess = InProcessTransport.makePair()
302295
let task = Task {
303296
let server = GRPCServer(transports: [inProcess.server], services: [BinaryEcho()])
304297
try await server.run()
@@ -353,7 +346,8 @@ final class GRPCServerTests: XCTestCase {
353346

354347
func testRunServerDrainsRunningTransportsWhenOneFailsToStart() async throws {
355348
// Register the in process transport first and allow it to come up.
356-
let inProcess = self.makeInProcessPair()
349+
let inProcess = InProcessTransport.makePair()
350+
357351
// Register a transport waits for a signal before throwing.
358352
let signal = AsyncStream.makeStream(of: Void.self)
359353
let server = GRPCServer(

0 commit comments

Comments
 (0)