Skip to content

Commit 90a5498

Browse files
mpetrovMrMage
authored andcommitted
Generate client test stubs in a dedicated section. (grpc#403)
This will make it simpler to conditionally turn of generating the implementation, so that test stubs can be compiled separately. See PR grpc#402 for more discussion.
1 parent dc2e8bb commit 90a5498

File tree

2 files changed

+97
-69
lines changed

2 files changed

+97
-69
lines changed

Sources/Examples/Echo/Generated/echo.grpc.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ fileprivate final class Echo_EchoGetCallBase: ClientCallUnaryBase<Echo_EchoReque
3131
override class var method: String { return "/echo.Echo/Get" }
3232
}
3333

34-
class Echo_EchoGetCallTestStub: ClientCallUnaryTestStub, Echo_EchoGetCall {
35-
override class var method: String { return "/echo.Echo/Get" }
36-
}
37-
3834
internal protocol Echo_EchoExpandCall: ClientCallServerStreaming {
3935
/// Do not call this directly, call `receive()` in the protocol extension below instead.
4036
func _receive(timeout: DispatchTime) throws -> Echo_EchoResponse?
@@ -51,10 +47,6 @@ fileprivate final class Echo_EchoExpandCallBase: ClientCallServerStreamingBase<E
5147
override class var method: String { return "/echo.Echo/Expand" }
5248
}
5349

54-
class Echo_EchoExpandCallTestStub: ClientCallServerStreamingTestStub<Echo_EchoResponse>, Echo_EchoExpandCall {
55-
override class var method: String { return "/echo.Echo/Expand" }
56-
}
57-
5850
internal protocol Echo_EchoCollectCall: ClientCallClientStreaming {
5951
/// Send a message to the stream. Nonblocking.
6052
func send(_ message: Echo_EchoRequest, completion: @escaping (Error?) -> Void) throws
@@ -76,12 +68,6 @@ fileprivate final class Echo_EchoCollectCallBase: ClientCallClientStreamingBase<
7668
override class var method: String { return "/echo.Echo/Collect" }
7769
}
7870

79-
/// Simple fake implementation of Echo_EchoCollectCall
80-
/// stores sent values for later verification and finall returns a previously-defined result.
81-
class Echo_EchoCollectCallTestStub: ClientCallClientStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
82-
override class var method: String { return "/echo.Echo/Collect" }
83-
}
84-
8571
internal protocol Echo_EchoUpdateCall: ClientCallBidirectionalStreaming {
8672
/// Do not call this directly, call `receive()` in the protocol extension below instead.
8773
func _receive(timeout: DispatchTime) throws -> Echo_EchoResponse?
@@ -113,10 +99,6 @@ fileprivate final class Echo_EchoUpdateCallBase: ClientCallBidirectionalStreamin
11399
override class var method: String { return "/echo.Echo/Update" }
114100
}
115101

116-
class Echo_EchoUpdateCallTestStub: ClientCallBidirectionalStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateCall {
117-
override class var method: String { return "/echo.Echo/Update" }
118-
}
119-
120102

121103
/// Instantiate Echo_EchoServiceClient, then call methods of this protocol to make API calls.
122104
internal protocol Echo_EchoService: ServiceClient {
@@ -210,6 +192,24 @@ internal final class Echo_EchoServiceClient: ServiceClientBase, Echo_EchoService
210192

211193
}
212194

195+
class Echo_EchoGetCallTestStub: ClientCallUnaryTestStub, Echo_EchoGetCall {
196+
override class var method: String { return "/echo.Echo/Get" }
197+
}
198+
199+
class Echo_EchoExpandCallTestStub: ClientCallServerStreamingTestStub<Echo_EchoResponse>, Echo_EchoExpandCall {
200+
override class var method: String { return "/echo.Echo/Expand" }
201+
}
202+
203+
/// Simple fake implementation of Echo_EchoCollectCall
204+
/// stores sent values for later verification and finall returns a previously-defined result.
205+
class Echo_EchoCollectCallTestStub: ClientCallClientStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoCollectCall {
206+
override class var method: String { return "/echo.Echo/Collect" }
207+
}
208+
209+
class Echo_EchoUpdateCallTestStub: ClientCallBidirectionalStreamingTestStub<Echo_EchoRequest, Echo_EchoResponse>, Echo_EchoUpdateCall {
210+
override class var method: String { return "/echo.Echo/Update" }
211+
}
212+
213213
class Echo_EchoServiceTestStub: ServiceClientTestStubBase, Echo_EchoService {
214214
var getRequests: [Echo_EchoRequest] = []
215215
var getResponses: [Echo_EchoResponse] = []

Sources/protoc-gen-swiftgrpc/Generator-Client.swift

Lines changed: 79 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ extension Generator {
2525
} else {
2626
printCGRPCClient(asynchronousCode: asynchronousCode,
2727
synchronousCode: synchronousCode)
28+
if options.generateTestStubs {
29+
printCGRPCClientTestStubs(asynchronousCode: asynchronousCode,
30+
synchronousCode: synchronousCode)
31+
}
2832
}
2933
}
3034

@@ -52,10 +56,25 @@ extension Generator {
5256
println()
5357
printServiceClientImplementation(asynchronousCode: asynchronousCode,
5458
synchronousCode: synchronousCode)
55-
if options.generateTestStubs {
56-
println()
57-
printServiceClientTestStubs()
59+
}
60+
61+
private func printCGRPCClientTestStubs(asynchronousCode: Bool,
62+
synchronousCode: Bool) {
63+
for method in service.methods {
64+
self.method = method
65+
switch streamingType(method) {
66+
case .unary:
67+
printServiceClientMethodCallUnaryTestStub()
68+
case .serverStreaming:
69+
printServiceClientMethodCallServerStreamingTestStub()
70+
case .clientStreaming:
71+
printServiceClientMethodCallClientStreamingTestStub()
72+
case .bidirectionalStreaming:
73+
printServiceClientMethodCallBidiStreamingTestStub()
74+
}
5875
}
76+
println()
77+
printServiceClientTestStubs(asynchronousCode: asynchronousCode, synchronousCode: synchronousCode)
5978
}
6079

6180
private func printServiceClientMethodCallUnary() {
@@ -66,17 +85,18 @@ extension Generator {
6685
println("override class var method: String { return \(methodPath) }")
6786
outdent()
6887
println("}")
69-
if options.generateTestStubs {
70-
println()
71-
println("class \(callName)TestStub: ClientCallUnaryTestStub, \(callName) {")
72-
indent()
73-
println("override class var method: String { return \(methodPath) }")
74-
outdent()
75-
println("}")
76-
}
7788
println()
7889
}
7990

91+
private func printServiceClientMethodCallUnaryTestStub() {
92+
println()
93+
println("class \(callName)TestStub: ClientCallUnaryTestStub, \(callName) {")
94+
indent()
95+
println("override class var method: String { return \(methodPath) }")
96+
outdent()
97+
println("}")
98+
}
99+
80100
private func printServiceClientMethodCallServerStreaming() {
81101
println("\(access) protocol \(callName): ClientCallServerStreaming {")
82102
indent()
@@ -91,17 +111,18 @@ extension Generator {
91111
println("override class var method: String { return \(methodPath) }")
92112
outdent()
93113
println("}")
94-
if options.generateTestStubs {
95-
println()
96-
println("class \(callName)TestStub: ClientCallServerStreamingTestStub<\(methodOutputName)>, \(callName) {")
97-
indent()
98-
println("override class var method: String { return \(methodPath) }")
99-
outdent()
100-
println("}")
101-
}
102114
println()
103115
}
104116

117+
private func printServiceClientMethodCallServerStreamingTestStub() {
118+
println()
119+
println("class \(callName)TestStub: ClientCallServerStreamingTestStub<\(methodOutputName)>, \(callName) {")
120+
indent()
121+
println("override class var method: String { return \(methodPath) }")
122+
outdent()
123+
println("}")
124+
}
125+
105126
private func printServiceClientMethodCallClientStreaming() {
106127
println("\(options.visibility.sourceSnippet) protocol \(callName): ClientCallClientStreaming {")
107128
indent()
@@ -121,19 +142,20 @@ extension Generator {
121142
println("override class var method: String { return \(methodPath) }")
122143
outdent()
123144
println("}")
124-
if options.generateTestStubs {
125-
println()
126-
println("/// Simple fake implementation of \(callName)")
127-
println("/// stores sent values for later verification and finall returns a previously-defined result.")
128-
println("class \(callName)TestStub: ClientCallClientStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
129-
indent()
130-
println("override class var method: String { return \(methodPath) }")
131-
outdent()
132-
println("}")
133-
}
134145
println()
135146
}
136147

148+
private func printServiceClientMethodCallClientStreamingTestStub() {
149+
println()
150+
println("/// Simple fake implementation of \(callName)")
151+
println("/// stores sent values for later verification and finall returns a previously-defined result.")
152+
println("class \(callName)TestStub: ClientCallClientStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
153+
indent()
154+
println("override class var method: String { return \(methodPath) }")
155+
outdent()
156+
println("}")
157+
}
158+
137159
private func printServiceClientMethodCallBidiStreaming() {
138160
println("\(access) protocol \(callName): ClientCallBidirectionalStreaming {")
139161
indent()
@@ -157,17 +179,18 @@ extension Generator {
157179
println("override class var method: String { return \(methodPath) }")
158180
outdent()
159181
println("}")
160-
if options.generateTestStubs {
161-
println()
162-
println("class \(callName)TestStub: ClientCallBidirectionalStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
163-
indent()
164-
println("override class var method: String { return \(methodPath) }")
165-
outdent()
166-
println("}")
167-
}
168182
println()
169183
}
170184

185+
private func printServiceClientMethodCallBidiStreamingTestStub() {
186+
println()
187+
println("class \(callName)TestStub: ClientCallBidirectionalStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
188+
indent()
189+
println("override class var method: String { return \(methodPath) }")
190+
outdent()
191+
println("}")
192+
}
193+
171194
private func printServiceClientProtocol(asynchronousCode: Bool,
172195
synchronousCode: Bool) {
173196
println("/// Instantiate \(serviceClassName)Client, then call methods of this protocol to make API calls.")
@@ -335,7 +358,8 @@ extension Generator {
335358
println("}")
336359
}
337360

338-
private func printServiceClientTestStubs() {
361+
private func printServiceClientTestStubs(asynchronousCode: Bool,
362+
synchronousCode: Bool) {
339363
println("class \(serviceClassName)TestStub: ServiceClientTestStubBase, \(serviceClassName) {")
340364
indent()
341365
for method in service.methods {
@@ -344,19 +368,23 @@ extension Generator {
344368
case .unary:
345369
println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
346370
println("var \(methodFunctionName)Responses: [\(methodOutputName)] = []")
347-
println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
348-
indent()
349-
println("\(methodFunctionName)Requests.append(request)")
350-
println("defer { \(methodFunctionName)Responses.removeFirst() }")
351-
println("return \(methodFunctionName)Responses.first!")
352-
outdent()
353-
println("}")
354-
println("@discardableResult")
355-
println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
356-
indent()
357-
println("fatalError(\"not implemented\")")
358-
outdent()
359-
println("}")
371+
if synchronousCode {
372+
println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
373+
indent()
374+
println("\(methodFunctionName)Requests.append(request)")
375+
println("defer { \(methodFunctionName)Responses.removeFirst() }")
376+
println("return \(methodFunctionName)Responses.first!")
377+
outdent()
378+
println("}")
379+
}
380+
if asynchronousCode {
381+
println("@discardableResult")
382+
println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
383+
indent()
384+
println("fatalError(\"not implemented\")")
385+
outdent()
386+
println("}")
387+
}
360388
case .serverStreaming:
361389
println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
362390
println("var \(methodFunctionName)Calls: [\(callName)] = []")

0 commit comments

Comments
 (0)