InProcessClientTransportTests.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 GRPCCore
  17. import GRPCInProcessTransport
  18. import XCTest
  19. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  20. final class InProcessClientTransportTests: XCTestCase {
  21. struct FailTest: Error {}
  22. func testConnectWhenConnected() async {
  23. let client = makeClient()
  24. await withThrowingTaskGroup(of: Void.self) { group in
  25. group.addTask {
  26. try await client.connect(lazily: false)
  27. }
  28. group.addTask {
  29. try await client.connect(lazily: false)
  30. }
  31. await XCTAssertThrowsErrorAsync(ofType: RPCError.self) {
  32. try await group.next()
  33. } errorHandler: { error in
  34. XCTAssertEqual(error.code, .failedPrecondition)
  35. }
  36. group.cancelAll()
  37. }
  38. }
  39. func testConnectWhenClosed() async {
  40. let client = makeClient()
  41. client.close()
  42. await XCTAssertThrowsErrorAsync(ofType: RPCError.self) {
  43. try await client.connect(lazily: false)
  44. } errorHandler: { error in
  45. XCTAssertEqual(error.code, .failedPrecondition)
  46. }
  47. }
  48. func testConnectWhenClosedAfterCancellation() async throws {
  49. let client = makeClient()
  50. try await withThrowingTaskGroup(of: Void.self) { group in
  51. group.addTask {
  52. try await client.connect(lazily: false)
  53. }
  54. group.addTask {
  55. try await Task.sleep(for: .milliseconds(100))
  56. }
  57. try await group.next()
  58. group.cancelAll()
  59. await XCTAssertThrowsErrorAsync(ofType: RPCError.self) {
  60. try await client.connect(lazily: false)
  61. } errorHandler: { error in
  62. XCTAssertEqual(error.code, .failedPrecondition)
  63. }
  64. }
  65. }
  66. func testCloseWhenUnconnected() {
  67. let client = makeClient()
  68. XCTAssertNoThrow(client.close())
  69. }
  70. func testCloseWhenClosed() {
  71. let client = makeClient()
  72. client.close()
  73. XCTAssertNoThrow(client.close())
  74. }
  75. func testConnectSuccessfullyAndThenClose() async throws {
  76. let client = makeClient()
  77. try await withThrowingTaskGroup(of: Void.self) { group in
  78. group.addTask {
  79. try await client.connect(lazily: false)
  80. }
  81. group.addTask {
  82. try await Task.sleep(for: .milliseconds(100))
  83. }
  84. try await group.next()
  85. client.close()
  86. }
  87. }
  88. func testOpenStreamWhenUnconnected() async throws {
  89. let client = makeClient()
  90. try await withThrowingTaskGroup(of: Void.self) { group in
  91. group.addTask {
  92. try await client.withStream(descriptor: .init(service: "test", method: "test")) { _ in
  93. // Once the pending stream is opened, close the client to new connections,
  94. // so that, once this closure is executed and this stream is closed,
  95. // the client will return from `connect(lazily:)`.
  96. client.close()
  97. }
  98. }
  99. group.addTask {
  100. // Add a sleep to make sure connection happens after `withStream` has been called,
  101. // to test pending streams are handled correctly.
  102. try await Task.sleep(for: .milliseconds(100))
  103. try await client.connect(lazily: false)
  104. }
  105. try await group.waitForAll()
  106. }
  107. }
  108. func testOpenStreamWhenClosed() async {
  109. let client = makeClient()
  110. client.close()
  111. await XCTAssertThrowsErrorAsync(ofType: RPCError.self) {
  112. try await client.withStream(descriptor: .init(service: "test", method: "test")) { _ in }
  113. } errorHandler: { error in
  114. XCTAssertEqual(error.code, .failedPrecondition)
  115. }
  116. }
  117. func testOpenStreamSuccessfullyAndThenClose() async throws {
  118. let server = InProcessServerTransport()
  119. let client = makeClient(server: server)
  120. try await withThrowingTaskGroup(of: Void.self) { group in
  121. group.addTask {
  122. try await client.connect(lazily: false)
  123. }
  124. group.addTask {
  125. try await client.withStream(descriptor: .init(service: "test", method: "test")) { stream in
  126. try await stream.outbound.write(.message([1]))
  127. stream.outbound.finish()
  128. let receivedMessages = try await stream.inbound.reduce(into: []) { $0.append($1) }
  129. XCTAssertEqual(receivedMessages, [.message([42])])
  130. }
  131. }
  132. group.addTask {
  133. for try await stream in try await server.listen() {
  134. let receivedMessages = try await stream.inbound.reduce(into: []) { $0.append($1) }
  135. try await stream.outbound.write(RPCResponsePart.message([42]))
  136. stream.outbound.finish()
  137. XCTAssertEqual(receivedMessages, [.message([1])])
  138. }
  139. }
  140. group.addTask {
  141. try await Task.sleep(for: .milliseconds(100))
  142. client.close()
  143. }
  144. try await group.next()
  145. group.cancelAll()
  146. }
  147. }
  148. func testExecutionConfiguration() {
  149. let policy = HedgingPolicy(
  150. maximumAttempts: 10,
  151. hedgingDelay: .seconds(1),
  152. nonFatalStatusCodes: []
  153. )
  154. let defaultConfiguration = MethodConfiguration(hedgingPolicy: policy)
  155. var configurations = MethodConfigurations()
  156. configurations.setDefaultConfiguration(defaultConfiguration)
  157. var client = InProcessClientTransport(server: .init(), methodConfiguration: 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 = MethodConfiguration(retryPolicy: retryPolicy)
  168. configurations[firstDescriptor] = overrideConfiguration
  169. client = InProcessClientTransport(server: .init(), methodConfiguration: 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: MethodConfiguration? = 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. var methodConfiguration = MethodConfigurations()
  210. methodConfiguration.setDefaultConfiguration(
  211. configuration ?? .init(retryPolicy: defaultPolicy)
  212. )
  213. return InProcessClientTransport(
  214. server: server,
  215. methodConfiguration: methodConfiguration
  216. )
  217. }
  218. }