InProcessClientTransportTests.swift 7.7 KB

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