InProcessClientTransportTests.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.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()
  27. }
  28. group.addTask {
  29. try await client.connect()
  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.beginGracefulShutdown()
  42. await XCTAssertThrowsErrorAsync(ofType: RPCError.self) {
  43. try await client.connect()
  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()
  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()
  61. } errorHandler: { error in
  62. XCTAssertEqual(error.code, .failedPrecondition)
  63. }
  64. }
  65. }
  66. func testCloseWhenUnconnected() {
  67. let client = makeClient()
  68. XCTAssertNoThrow(client.beginGracefulShutdown())
  69. }
  70. func testCloseWhenClosed() {
  71. let client = makeClient()
  72. client.beginGracefulShutdown()
  73. XCTAssertNoThrow(client.beginGracefulShutdown())
  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()
  80. }
  81. group.addTask {
  82. try await Task.sleep(for: .milliseconds(100))
  83. }
  84. try await group.next()
  85. client.beginGracefulShutdown()
  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(
  93. descriptor: .init(service: "test", method: "test"),
  94. options: .defaults
  95. ) { _ in
  96. // Once the pending stream is opened, close the client to new connections,
  97. // so that, once this closure is executed and this stream is closed,
  98. // the client will return from `connect()`.
  99. client.beginGracefulShutdown()
  100. }
  101. }
  102. group.addTask {
  103. // Add a sleep to make sure connection happens after `withStream` has been called,
  104. // to test pending streams are handled correctly.
  105. try await Task.sleep(for: .milliseconds(100))
  106. try await client.connect()
  107. }
  108. try await group.waitForAll()
  109. }
  110. }
  111. func testOpenStreamWhenClosed() async {
  112. let client = makeClient()
  113. client.beginGracefulShutdown()
  114. await XCTAssertThrowsErrorAsync(ofType: RPCError.self) {
  115. try await client.withStream(
  116. descriptor: .init(service: "test", method: "test"),
  117. options: .defaults
  118. ) { _ in }
  119. } errorHandler: { error in
  120. XCTAssertEqual(error.code, .failedPrecondition)
  121. }
  122. }
  123. func testOpenStreamSuccessfullyAndThenClose() async throws {
  124. let server = InProcessServerTransport()
  125. let client = makeClient(server: server)
  126. try await withThrowingTaskGroup(of: Void.self) { group in
  127. group.addTask {
  128. try await client.connect()
  129. }
  130. group.addTask {
  131. try await client.withStream(
  132. descriptor: .init(service: "test", method: "test"),
  133. options: .defaults
  134. ) { stream in
  135. try await stream.outbound.write(.message([1]))
  136. stream.outbound.finish()
  137. let receivedMessages = try await stream.inbound.reduce(into: []) { $0.append($1) }
  138. XCTAssertEqual(receivedMessages, [.message([42])])
  139. }
  140. }
  141. group.addTask {
  142. try await server.listen { stream in
  143. let receivedMessages = try? await stream.inbound.reduce(into: []) { $0.append($1) }
  144. try? await stream.outbound.write(RPCResponsePart.message([42]))
  145. stream.outbound.finish()
  146. XCTAssertEqual(receivedMessages, [.message([1])])
  147. }
  148. }
  149. group.addTask {
  150. try await Task.sleep(for: .milliseconds(100))
  151. client.beginGracefulShutdown()
  152. }
  153. try await group.next()
  154. group.cancelAll()
  155. }
  156. }
  157. func testExecutionConfiguration() {
  158. let policy = HedgingPolicy(
  159. maximumAttempts: 10,
  160. hedgingDelay: .seconds(1),
  161. nonFatalStatusCodes: []
  162. )
  163. var serviceConfig = ServiceConfig(
  164. methodConfig: [
  165. MethodConfig(
  166. names: [
  167. MethodConfig.Name(service: "", method: "")
  168. ],
  169. executionPolicy: .hedge(policy)
  170. )
  171. ]
  172. )
  173. var client = InProcessClientTransport(
  174. server: InProcessServerTransport(),
  175. serviceConfig: serviceConfig
  176. )
  177. let firstDescriptor = MethodDescriptor(service: "test", method: "first")
  178. XCTAssertEqual(
  179. client.configuration(forMethod: firstDescriptor),
  180. serviceConfig.methodConfig.first
  181. )
  182. let retryPolicy = RetryPolicy(
  183. maximumAttempts: 10,
  184. initialBackoff: .seconds(1),
  185. maximumBackoff: .seconds(1),
  186. backoffMultiplier: 1.0,
  187. retryableStatusCodes: [.unavailable]
  188. )
  189. let overrideConfiguration = MethodConfig(
  190. names: [MethodConfig.Name(service: "test", method: "second")],
  191. executionPolicy: .retry(retryPolicy)
  192. )
  193. serviceConfig.methodConfig.append(overrideConfiguration)
  194. client = InProcessClientTransport(
  195. server: InProcessServerTransport(),
  196. serviceConfig: serviceConfig
  197. )
  198. let secondDescriptor = MethodDescriptor(service: "test", method: "second")
  199. XCTAssertEqual(
  200. client.configuration(forMethod: firstDescriptor),
  201. serviceConfig.methodConfig.first
  202. )
  203. XCTAssertEqual(
  204. client.configuration(forMethod: secondDescriptor),
  205. serviceConfig.methodConfig.last
  206. )
  207. }
  208. func testOpenMultipleStreamsThenClose() async throws {
  209. let server = InProcessServerTransport()
  210. let client = makeClient(server: server)
  211. try await withThrowingTaskGroup(of: Void.self) { group in
  212. group.addTask {
  213. try await client.connect()
  214. }
  215. group.addTask {
  216. try await client.withStream(
  217. descriptor: .init(service: "test", method: "test"),
  218. options: .defaults
  219. ) { stream in
  220. try await Task.sleep(for: .milliseconds(100))
  221. }
  222. }
  223. group.addTask {
  224. try await client.withStream(
  225. descriptor: .init(service: "test", method: "test"),
  226. options: .defaults
  227. ) { stream in
  228. try await Task.sleep(for: .milliseconds(100))
  229. }
  230. }
  231. group.addTask {
  232. try await Task.sleep(for: .milliseconds(50))
  233. client.beginGracefulShutdown()
  234. }
  235. try await group.next()
  236. }
  237. }
  238. func makeClient(
  239. server: InProcessServerTransport = InProcessServerTransport()
  240. ) -> InProcessClientTransport {
  241. let defaultPolicy = RetryPolicy(
  242. maximumAttempts: 10,
  243. initialBackoff: .seconds(1),
  244. maximumBackoff: .seconds(1),
  245. backoffMultiplier: 1.0,
  246. retryableStatusCodes: [.unavailable]
  247. )
  248. let serviceConfig = ServiceConfig(
  249. methodConfig: [
  250. MethodConfig(
  251. names: [MethodConfig.Name(service: "", method: "")],
  252. executionPolicy: .retry(defaultPolicy)
  253. )
  254. ]
  255. )
  256. return InProcessClientTransport(
  257. server: server,
  258. serviceConfig: serviceConfig
  259. )
  260. }
  261. }