ClientCallTests.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright 2020, 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 EchoImplementation
  17. import EchoModel
  18. @testable import GRPC
  19. import NIO
  20. import XCTest
  21. class ClientCallTests: GRPCTestCase {
  22. private var group: MultiThreadedEventLoopGroup!
  23. private var server: Server!
  24. private var connection: ClientConnection!
  25. override func setUp() {
  26. super.setUp()
  27. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  28. self.server = try! Server.insecure(group: self.group)
  29. .withServiceProviders([EchoProvider()])
  30. .withLogger(self.serverLogger)
  31. .bind(host: "localhost", port: 0)
  32. .wait()
  33. let port = self.server.channel.localAddress!.port!
  34. self.connection = ClientConnection.insecure(group: self.group)
  35. .withBackgroundActivityLogger(self.clientLogger)
  36. .connect(host: "localhost", port: port)
  37. }
  38. override func tearDown() {
  39. XCTAssertNoThrow(try self.connection.close().wait())
  40. XCTAssertNoThrow(try self.server.close().wait())
  41. XCTAssertNoThrow(try self.group.syncShutdownGracefully())
  42. super.tearDown()
  43. }
  44. private func makeCall(
  45. path: String,
  46. type: GRPCCallType
  47. ) -> Call<Echo_EchoRequest, Echo_EchoResponse> {
  48. return self.connection.makeCall(path: path, type: type, callOptions: .init(), interceptors: [])
  49. }
  50. private func get() -> Call<Echo_EchoRequest, Echo_EchoResponse> {
  51. return self.makeCall(path: "/echo.Echo/Get", type: .unary)
  52. }
  53. private func collect() -> Call<Echo_EchoRequest, Echo_EchoResponse> {
  54. return self.makeCall(path: "/echo.Echo/Collect", type: .clientStreaming)
  55. }
  56. private func expand() -> Call<Echo_EchoRequest, Echo_EchoResponse> {
  57. return self.makeCall(path: "/echo.Echo/Expand", type: .serverStreaming)
  58. }
  59. private func update() -> Call<Echo_EchoRequest, Echo_EchoResponse> {
  60. return self.makeCall(path: "/echo.Echo/Update", type: .bidirectionalStreaming)
  61. }
  62. private func makeStatusPromise() -> EventLoopPromise<GRPCStatus> {
  63. return self.connection.eventLoop.makePromise()
  64. }
  65. /// Makes a response part handler which succeeds the promise when receiving the status and fails
  66. /// it if an error is received.
  67. private func makeResponsePartHandler<Response>(
  68. for: Response.Type = Response.self,
  69. completing promise: EventLoopPromise<GRPCStatus>
  70. ) -> (GRPCClientResponsePart<Response>) -> Void {
  71. return { part in
  72. switch part {
  73. case .metadata, .message:
  74. ()
  75. case let .end(status, _):
  76. promise.succeed(status)
  77. }
  78. }
  79. }
  80. // MARK: - Tests
  81. func testFullyManualUnary() throws {
  82. let get = self.get()
  83. let statusPromise = self.makeStatusPromise()
  84. get.invoke(
  85. onError: statusPromise.fail(_:),
  86. onResponsePart: self.makeResponsePartHandler(completing: statusPromise)
  87. )
  88. let f1 = get.send(.metadata(get.options.customMetadata))
  89. let f2 = get.send(.message(.with { $0.text = "get" }, .init(compress: false, flush: false)))
  90. let f3 = get.send(.end)
  91. // '.end' will flush, so we can wait on the futures now.
  92. assertThat(try f1.wait(), .doesNotThrow())
  93. assertThat(try f2.wait(), .doesNotThrow())
  94. assertThat(try f3.wait(), .doesNotThrow())
  95. // Status should be ok.
  96. assertThat(try statusPromise.futureResult.wait(), .hasCode(.ok))
  97. }
  98. func testUnaryCall() {
  99. let get = self.get()
  100. let promise = self.makeStatusPromise()
  101. get.invokeUnaryRequest(
  102. .with { $0.text = "get" },
  103. onError: promise.fail(_:),
  104. onResponsePart: self.makeResponsePartHandler(completing: promise)
  105. )
  106. assertThat(try promise.futureResult.wait(), .hasCode(.ok))
  107. }
  108. func testClientStreaming() {
  109. let collect = self.collect()
  110. let promise = self.makeStatusPromise()
  111. collect.invokeStreamingRequests(
  112. onError: promise.fail(_:),
  113. onResponsePart: self.makeResponsePartHandler(completing: promise)
  114. )
  115. collect.send(
  116. .message(.with { $0.text = "collect" }, .init(compress: false, flush: false)),
  117. promise: nil
  118. )
  119. collect.send(.end, promise: nil)
  120. assertThat(try promise.futureResult.wait(), .hasCode(.ok))
  121. }
  122. func testServerStreaming() {
  123. let expand = self.expand()
  124. let promise = self.makeStatusPromise()
  125. expand.invokeUnaryRequest(
  126. .with { $0.text = "expand" },
  127. onError: promise.fail(_:),
  128. onResponsePart: self.makeResponsePartHandler(completing: promise)
  129. )
  130. assertThat(try promise.futureResult.wait(), .hasCode(.ok))
  131. }
  132. func testBidirectionalStreaming() {
  133. let update = self.update()
  134. let promise = self.makeStatusPromise()
  135. update.invokeStreamingRequests(
  136. onError: promise.fail(_:),
  137. onResponsePart: self.makeResponsePartHandler(completing: promise)
  138. )
  139. update.send(
  140. .message(.with { $0.text = "update" }, .init(compress: false, flush: false)),
  141. promise: nil
  142. )
  143. update.send(.end, promise: nil)
  144. assertThat(try promise.futureResult.wait(), .hasCode(.ok))
  145. }
  146. func testSendBeforeInvoke() throws {
  147. let get = self.get()
  148. assertThat(try get.send(.end).wait(), .throws())
  149. }
  150. func testCancelBeforeInvoke() throws {
  151. let get = self.get()
  152. assertThat(try get.cancel().wait(), .throws())
  153. }
  154. func testCancelMidRPC() throws {
  155. let get = self.get()
  156. let promise = self.makeStatusPromise()
  157. get.invoke(
  158. onError: promise.fail(_:),
  159. onResponsePart: self.makeResponsePartHandler(completing: promise)
  160. )
  161. // Cancellation should succeed.
  162. assertThat(try get.cancel().wait(), .doesNotThrow())
  163. // The status promise will fail.
  164. assertThat(
  165. try promise.futureResult.wait(),
  166. .throws(.instanceOf(GRPCError.RPCCancelledByClient.self))
  167. )
  168. // Cancellation should now fail, we've already cancelled.
  169. assertThat(try get.cancel().wait(), .throws(.instanceOf(GRPCError.AlreadyComplete.self)))
  170. }
  171. }