Skip to content

Commit 1c33b78

Browse files
authored
Move in-process transport to own module (#1730)
Motivation: The in-process transport is mostly helpful for testing, it should therefore not be built unnecessarily and should live in its own module. Modifications: - Move the in-process transport and its tests to their own modules Results: The in-process transport lives in its own module.
1 parent 39c2f4f commit 1c33b78

File tree

13 files changed

+171
-46
lines changed

13 files changed

+171
-46
lines changed

Package.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ extension Target.Dependency {
133133
static let atomics: Self = .product(name: "Atomics", package: "swift-atomics")
134134

135135
static let grpcCore: Self = .target(name: "GRPCCore")
136+
static let grpcInProcessTransport: Self = .target(name: "GRPCInProcessTransport")
136137
}
137138

138139
// MARK: - Targets
@@ -170,6 +171,13 @@ extension Target {
170171
path: "Sources/GRPCCore"
171172
)
172173

174+
static let grpcInProcessTransport: Target = .target(
175+
name: "GRPCInProcessTransport",
176+
dependencies: [
177+
.grpcCore
178+
]
179+
)
180+
173181
static let cgrpcZlib: Target = .target(
174182
name: cgrpcZlibTargetName,
175183
path: "Sources/CGRPCZlib",
@@ -230,18 +238,27 @@ extension Target {
230238
name: "GRPCCoreTests",
231239
dependencies: [
232240
.grpcCore,
241+
.grpcInProcessTransport,
233242
.dequeModule,
234243
.atomics
235244
]
236245
)
237246

247+
static let grpcInProcessTransportTests: Target = .testTarget(
248+
name: "GRPCInProcessTransportTests",
249+
dependencies: [
250+
.grpcCore,
251+
.grpcInProcessTransport,
252+
]
253+
)
254+
238255
static let grpcCodeGenTests: Target = .testTarget(
239256
name: "GRPCCodeGenTests",
240257
dependencies: [
241258
.grpcCodeGen
242259
]
243260
)
244-
261+
245262
static let interopTestModels: Target = .target(
246263
name: "GRPCInteroperabilityTestModels",
247264
dependencies: [
@@ -468,7 +485,7 @@ extension Target {
468485
"v1Alpha/reflection-v1alpha.proto"
469486
]
470487
)
471-
488+
472489
static let reflectionServer: Target = .executableTarget(
473490
name: "ReflectionServer",
474491
dependencies: [
@@ -486,7 +503,7 @@ extension Target {
486503
.copy("Generated")
487504
]
488505
)
489-
506+
490507
static let grpcCodeGen: Target = .target(
491508
name: "GRPCCodeGen",
492509
path: "Sources/GRPCCodeGen"
@@ -500,7 +517,7 @@ extension Product {
500517
name: grpcProductName,
501518
targets: [grpcTargetName]
502519
)
503-
520+
504521
static let grpcCore: Product = .library(
505522
name: "_GRPCCore",
506523
targets: ["GRPCCore"]
@@ -566,10 +583,12 @@ let package = Package(
566583

567584
// v2
568585
.grpcCore,
586+
.grpcInProcessTransport,
569587
.grpcCodeGen,
570588

571589
// v2 tests
572590
.grpcCoreTests,
591+
.grpcInProcessTransportTests,
573592
.grpcCodeGenTests
574593
]
575594
)

Sources/GRPCCore/Internal/Concurrency Primitives/Lock.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,22 @@ extension UnsafeMutablePointer {
237237
}
238238

239239
@usableFromInline
240-
struct LockedValueBox<Value> {
240+
internal typealias LockedValueBox<Value> = _LockedValueBox<Value>
241+
242+
// TODO: Use 'package' ACL when 5.9 is the minimum Swift version.
243+
public struct _LockedValueBox<Value> {
241244
@usableFromInline
242245
let storage: LockStorage<Value>
243246

244247
@inlinable
245-
init(_ value: Value) {
248+
public init(_ value: Value) {
246249
self.storage = .create(value: value)
247250
}
248251

249252
@inlinable
250-
func withLockedValue<T>(_ mutate: (inout Value) throws -> T) rethrows -> T {
253+
public func withLockedValue<T>(_ mutate: (inout Value) throws -> T) rethrows -> T {
251254
return try self.storage.withLockedValue(mutate)
252255
}
253256
}
254257

255-
extension LockedValueBox: Sendable where Value: Sendable {}
258+
extension _LockedValueBox: Sendable where Value: Sendable {}

Sources/GRPCCore/Streaming/Internal/RPCAsyncSequence+Buffered.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,12 @@ extension RPCAsyncSequence {
2828

2929
return (RPCAsyncSequence(wrapping: stream), RPCWriter.Closable(wrapping: continuation))
3030
}
31+
32+
@inlinable
33+
public static func _makeBackpressuredStream(
34+
of elementType: Element.Type = Element.self,
35+
watermarks: (low: Int, high: Int)
36+
) -> (stream: Self, writer: RPCWriter<Element>.Closable) {
37+
return Self.makeBackpressuredStream(of: elementType, watermarks: watermarks)
38+
}
3139
}

Sources/GRPCCore/Transport/InProcessClientTransport.swift renamed to Sources/GRPCInProcessTransport/InProcessClientTransport.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
17+
import GRPCCore
18+
1819
/// An in-process implementation of a ``ClientTransport``.
1920
///
2021
/// This is useful when you're interested in testing your application without any actual networking layers
@@ -34,6 +35,7 @@
3435
/// block until ``connect(lazily:)`` is called or the task is cancelled.
3536
///
3637
/// - SeeAlso: ``ClientTransport``
38+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
3739
public struct InProcessClientTransport: ClientTransport {
3840
private enum State: Sendable {
3941
struct UnconnectedState {
@@ -96,16 +98,16 @@ public struct InProcessClientTransport: ClientTransport {
9698

9799
public let retryThrottle: RetryThrottle
98100

99-
private let executionConfigurations: MethodConfigurations
100-
private let state: LockedValueBox<State>
101+
private let methodConfiguration: MethodConfigurations
102+
private let state: _LockedValueBox<State>
101103

102104
public init(
103105
server: InProcessServerTransport,
104-
executionConfigurations: MethodConfigurations
106+
methodConfiguration: MethodConfigurations = MethodConfigurations()
105107
) {
106108
self.retryThrottle = RetryThrottle(maximumTokens: 10, tokenRatio: 0.1)
107-
self.executionConfigurations = executionConfigurations
108-
self.state = LockedValueBox(.unconnected(.init(serverTransport: server)))
109+
self.methodConfiguration = methodConfiguration
110+
self.state = _LockedValueBox(.unconnected(.init(serverTransport: server)))
109111
}
110112

111113
/// Establish and maintain a connection to the remote destination.
@@ -222,8 +224,8 @@ public struct InProcessClientTransport: ClientTransport {
222224
descriptor: MethodDescriptor,
223225
_ closure: (RPCStream<Inbound, Outbound>) async throws -> T
224226
) async throws -> T {
225-
let request = RPCAsyncSequence<RPCRequestPart>.makeBackpressuredStream(watermarks: (16, 32))
226-
let response = RPCAsyncSequence<RPCResponsePart>.makeBackpressuredStream(watermarks: (16, 32))
227+
let request = RPCAsyncSequence<RPCRequestPart>._makeBackpressuredStream(watermarks: (16, 32))
228+
let response = RPCAsyncSequence<RPCResponsePart>._makeBackpressuredStream(watermarks: (16, 32))
227229

228230
let clientStream = RPCStream(
229231
descriptor: descriptor,
@@ -330,6 +332,6 @@ public struct InProcessClientTransport: ClientTransport {
330332
public func executionConfiguration(
331333
forMethod descriptor: MethodDescriptor
332334
) -> MethodConfiguration? {
333-
self.executionConfigurations[descriptor]
335+
self.methodConfiguration[descriptor]
334336
}
335337
}

Sources/GRPCCore/Transport/InProcessServerTransport.swift renamed to Sources/GRPCInProcessTransport/InProcessServerTransport.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
17+
import GRPCCore
18+
1819
/// An in-process implementation of a ``ServerTransport``.
1920
///
2021
/// This is useful when you're interested in testing your application without any actual networking layers
@@ -25,6 +26,7 @@
2526
/// To stop listening to new requests, call ``stopListening()``.
2627
///
2728
/// - SeeAlso: ``ClientTransport``
29+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
2830
public struct InProcessServerTransport: ServerTransport, Sendable {
2931
public typealias Inbound = RPCAsyncSequence<RPCRequestPart>
3032
public typealias Outbound = RPCWriter<RPCResponsePart>.Closable
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
17+
#if swift(<5.9)
18+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
19+
extension AsyncStream {
20+
@inlinable
21+
static func makeStream(
22+
of elementType: Element.Type = Element.self,
23+
bufferingPolicy limit: AsyncStream<Element>.Continuation.BufferingPolicy = .unbounded
24+
) -> (stream: AsyncStream<Element>, continuation: AsyncStream<Element>.Continuation) {
25+
var continuation: AsyncStream<Element>.Continuation!
26+
let stream = AsyncStream(Element.self, bufferingPolicy: limit) {
27+
continuation = $0
28+
}
29+
return (stream, continuation)
30+
}
31+
}
32+
#endif

Tests/GRPCCoreTests/Call/Client/Internal/ClientRPCExecutorTestSupport/ClientRPCExecutorTestHarness+Transport.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414
* limitations under the License.
1515
*/
1616
import Atomics
17-
18-
@testable import GRPCCore
17+
import GRPCCore
18+
import GRPCInProcessTransport
1919

2020
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
2121
extension InProcessServerTransport {
2222
func spawnClientTransport(
2323
throttle: RetryThrottle = RetryThrottle(maximumTokens: 10, tokenRatio: 0.1)
2424
) -> InProcessClientTransport {
25-
return InProcessClientTransport(
26-
server: self,
27-
executionConfigurations: .init()
28-
)
25+
return InProcessClientTransport(server: self)
2926
}
3027
}

Tests/GRPCCoreTests/Call/Client/Internal/ClientRPCExecutorTestSupport/ClientRPCExecutorTestHarness.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import Atomics
17+
import GRPCInProcessTransport
1718
import XCTest
1819

1920
@testable import GRPCCore

Tests/GRPCCoreTests/GRPCClientTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
import Atomics
1717
import GRPCCore
18+
import GRPCInProcessTransport
1819
import XCTest
1920

2021
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
@@ -23,7 +24,7 @@ final class GRPCClientTests: XCTestCase {
2324
let server = InProcessServerTransport()
2425
let client = InProcessClientTransport(
2526
server: server,
26-
executionConfigurations: MethodConfigurations()
27+
methodConfiguration: MethodConfigurations()
2728
)
2829

2930
return (client, server)

Tests/GRPCCoreTests/GRPCServerTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
*/
1616
import Atomics
1717
import GRPCCore
18+
import GRPCInProcessTransport
1819
import XCTest
1920

2021
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
2122
final class GRPCServerTests: XCTestCase {
2223
func makeInProcessPair() -> (client: InProcessClientTransport, server: InProcessServerTransport) {
2324
let server = InProcessServerTransport()
24-
let client = InProcessClientTransport(
25-
server: server,
26-
executionConfigurations: MethodConfigurations()
27-
)
25+
let client = InProcessClientTransport(server: server)
2826

2927
return (client, server)
3028
}

0 commit comments

Comments
 (0)