InProcessClientTransportTests.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 XCTest
  17. @testable import GRPCCore
  18. final class InProcessClientTransportTests: XCTestCase {
  19. struct FailTest: Error {}
  20. func testConnectWhenConnected() async {
  21. let client = makeClient()
  22. await withThrowingTaskGroup(of: Void.self) { group in
  23. group.addTask {
  24. try await client.connect(lazily: false)
  25. }
  26. group.addTask {
  27. try await client.connect(lazily: false)
  28. }
  29. await XCTAssertThrowsRPCErrorAsync {
  30. try await group.next()
  31. } errorHandler: { error in
  32. XCTAssertEqual(error.code, .failedPrecondition)
  33. }
  34. group.cancelAll()
  35. }
  36. }
  37. func testConnectWhenClosed() async {
  38. let client = makeClient()
  39. client.close()
  40. await XCTAssertThrowsRPCErrorAsync {
  41. try await client.connect(lazily: false)
  42. } errorHandler: { error in
  43. XCTAssertEqual(error.code, .failedPrecondition)
  44. }
  45. }
  46. func testConnectWhenClosedAfterCancellation() async throws {
  47. let client = makeClient()
  48. try await withThrowingTaskGroup(of: Void.self) { group in
  49. group.addTask {
  50. try await client.connect(lazily: false)
  51. }
  52. group.addTask {
  53. try await Task.sleep(for: .milliseconds(100))
  54. }
  55. try await group.next()
  56. group.cancelAll()
  57. await XCTAssertThrowsRPCErrorAsync {
  58. try await client.connect(lazily: false)
  59. } errorHandler: { error in
  60. XCTAssertEqual(error.code, .failedPrecondition)
  61. }
  62. }
  63. }
  64. func testCloseWhenUnconnected() {
  65. let client = makeClient()
  66. XCTAssertNoThrow(client.close())
  67. }
  68. func testCloseWhenClosed() {
  69. let client = makeClient()
  70. client.close()
  71. XCTAssertNoThrow(client.close())
  72. }
  73. func testConnectSuccessfullyAndThenClose() async throws {
  74. let client = makeClient()
  75. try await withThrowingTaskGroup(of: Void.self) { group in
  76. group.addTask {
  77. try await client.connect(lazily: false)
  78. }
  79. group.addTask {
  80. try await Task.sleep(for: .milliseconds(100))
  81. }
  82. try await group.next()
  83. client.close()
  84. }
  85. }
  86. func testOpenStreamWhenUnconnected() async throws {
  87. let client = makeClient()
  88. try await withThrowingTaskGroup(of: Void.self) { group in
  89. group.addTask {
  90. try await client.withStream(descriptor: .init(service: "test", method: "test")) { _ in
  91. // Once the pending stream is opened, close the client to new connections,
  92. // so that, once this closure is executed and this stream is closed,
  93. // the client will return from `connect(lazily:)`.
  94. client.close()
  95. }
  96. }
  97. group.addTask {
  98. // Add a sleep to make sure connection happens after `withStream` has been called,
  99. // to test pending streams are handled correctly.
  100. try await Task.sleep(for: .milliseconds(100))
  101. try await client.connect(lazily: false)
  102. }
  103. try await group.waitForAll()
  104. }
  105. }
  106. func testOpenStreamWhenClosed() async {
  107. let client = makeClient()
  108. client.close()
  109. await XCTAssertThrowsRPCErrorAsync {
  110. try await client.withStream(descriptor: .init(service: "test", method: "test")) { _ in }
  111. } errorHandler: { error in
  112. XCTAssertEqual(error.code, .failedPrecondition)
  113. }
  114. }
  115. func testOpenStreamSuccessfullyAndThenClose() async throws {
  116. let server = InProcessServerTransport()
  117. let client = makeClient(server: server)
  118. try await withThrowingTaskGroup(of: Void.self) { group in
  119. group.addTask {
  120. try await client.connect(lazily: false)
  121. }
  122. group.addTask {
  123. try await client.withStream(descriptor: .init(service: "test", method: "test")) { stream in
  124. try await stream.outbound.write(.message([1]))
  125. stream.outbound.finish()
  126. let receivedMessages = try await stream.inbound.collect()
  127. XCTAssertEqual(receivedMessages, [.message([42])])
  128. }
  129. }
  130. group.addTask {
  131. for try await stream in server.listen() {
  132. let receivedMessages = try await stream.inbound.collect()
  133. try await stream.outbound.write(RPCResponsePart.message([42]))
  134. stream.outbound.finish()
  135. XCTAssertEqual(receivedMessages, [.message([1])])
  136. }
  137. }
  138. group.addTask {
  139. try await Task.sleep(for: .milliseconds(100))
  140. client.close()
  141. }
  142. try await group.next()
  143. group.cancelAll()
  144. }
  145. }
  146. func testExecutionConfiguration() {
  147. let policy = HedgingPolicy(
  148. maximumAttempts: 10,
  149. hedgingDelay: .seconds(1),
  150. nonFatalStatusCodes: []
  151. )
  152. let defaultConfiguration = ClientRPCExecutionConfiguration(hedgingPolicy: policy)
  153. var configurations = ClientRPCExecutionConfigurationCollection(
  154. defaultConfiguration: defaultConfiguration
  155. )
  156. var client = InProcessClientTransport(server: .init(), executionConfigurations: configurations)
  157. let firstDescriptor = MethodDescriptor(service: "test", method: "first")
  158. XCTAssertEqual(client.executionConfiguration(forMethod: firstDescriptor), defaultConfiguration)
  159. let retryPolicy = RetryPolicy(
  160. maximumAttempts: 10,
  161. initialBackoff: .seconds(1),
  162. maximumBackoff: .seconds(1),
  163. backoffMultiplier: 1.0,
  164. retryableStatusCodes: [.unavailable]
  165. )
  166. let overrideConfiguration = ClientRPCExecutionConfiguration(retryPolicy: retryPolicy)
  167. configurations[firstDescriptor] = overrideConfiguration
  168. client = InProcessClientTransport(server: .init(), executionConfigurations: configurations)
  169. let secondDescriptor = MethodDescriptor(service: "test", method: "second")
  170. XCTAssertEqual(client.executionConfiguration(forMethod: firstDescriptor), overrideConfiguration)
  171. XCTAssertEqual(client.executionConfiguration(forMethod: secondDescriptor), defaultConfiguration)
  172. }
  173. func testOpenMultipleStreamsThenClose() async throws {
  174. let server = InProcessServerTransport()
  175. let client = makeClient(server: server)
  176. try await withThrowingTaskGroup(of: Void.self) { group in
  177. group.addTask {
  178. try await client.connect(lazily: false)
  179. }
  180. group.addTask {
  181. try await client.withStream(descriptor: .init(service: "test", method: "test")) { stream in
  182. try await Task.sleep(for: .milliseconds(100))
  183. }
  184. }
  185. group.addTask {
  186. try await client.withStream(descriptor: .init(service: "test", method: "test")) { stream in
  187. try await Task.sleep(for: .milliseconds(100))
  188. }
  189. }
  190. group.addTask {
  191. try await Task.sleep(for: .milliseconds(50))
  192. client.close()
  193. }
  194. try await group.next()
  195. }
  196. }
  197. func makeClient(
  198. configuration: ClientRPCExecutionConfiguration? = nil,
  199. server: InProcessServerTransport = InProcessServerTransport()
  200. ) -> InProcessClientTransport {
  201. let defaultPolicy = RetryPolicy(
  202. maximumAttempts: 10,
  203. initialBackoff: .seconds(1),
  204. maximumBackoff: .seconds(1),
  205. backoffMultiplier: 1.0,
  206. retryableStatusCodes: [.unavailable]
  207. )
  208. return InProcessClientTransport(
  209. server: server,
  210. executionConfigurations: .init(
  211. defaultConfiguration: configuration ?? .init(retryPolicy: defaultPolicy)
  212. )
  213. )
  214. }
  215. }