InProcessClientTransportTests.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. var serviceConfiguration = ServiceConfiguration(
  155. methodConfiguration: [
  156. MethodConfiguration(
  157. names: [
  158. MethodConfiguration.Name(service: "", method: "")
  159. ],
  160. executionPolicy: .hedge(policy)
  161. )
  162. ]
  163. )
  164. var client = InProcessClientTransport(
  165. server: InProcessServerTransport(),
  166. serviceConfiguration: serviceConfiguration
  167. )
  168. let firstDescriptor = MethodDescriptor(service: "test", method: "first")
  169. XCTAssertEqual(
  170. client.configuration(forMethod: firstDescriptor),
  171. serviceConfiguration.methodConfiguration.first
  172. )
  173. let retryPolicy = RetryPolicy(
  174. maximumAttempts: 10,
  175. initialBackoff: .seconds(1),
  176. maximumBackoff: .seconds(1),
  177. backoffMultiplier: 1.0,
  178. retryableStatusCodes: [.unavailable]
  179. )
  180. let overrideConfiguration = MethodConfiguration(
  181. names: [MethodConfiguration.Name(service: "test", method: "second")],
  182. executionPolicy: .retry(retryPolicy)
  183. )
  184. serviceConfiguration.methodConfiguration.append(overrideConfiguration)
  185. client = InProcessClientTransport(
  186. server: InProcessServerTransport(),
  187. serviceConfiguration: serviceConfiguration
  188. )
  189. let secondDescriptor = MethodDescriptor(service: "test", method: "second")
  190. XCTAssertEqual(
  191. client.configuration(forMethod: firstDescriptor),
  192. serviceConfiguration.methodConfiguration.first
  193. )
  194. XCTAssertEqual(
  195. client.configuration(forMethod: secondDescriptor),
  196. serviceConfiguration.methodConfiguration.last
  197. )
  198. }
  199. func testOpenMultipleStreamsThenClose() async throws {
  200. let server = InProcessServerTransport()
  201. let client = makeClient(server: server)
  202. try await withThrowingTaskGroup(of: Void.self) { group in
  203. group.addTask {
  204. try await client.connect(lazily: false)
  205. }
  206. group.addTask {
  207. try await client.withStream(descriptor: .init(service: "test", method: "test")) { stream in
  208. try await Task.sleep(for: .milliseconds(100))
  209. }
  210. }
  211. group.addTask {
  212. try await client.withStream(descriptor: .init(service: "test", method: "test")) { stream in
  213. try await Task.sleep(for: .milliseconds(100))
  214. }
  215. }
  216. group.addTask {
  217. try await Task.sleep(for: .milliseconds(50))
  218. client.close()
  219. }
  220. try await group.next()
  221. }
  222. }
  223. func makeClient(
  224. server: InProcessServerTransport = InProcessServerTransport()
  225. ) -> InProcessClientTransport {
  226. let defaultPolicy = RetryPolicy(
  227. maximumAttempts: 10,
  228. initialBackoff: .seconds(1),
  229. maximumBackoff: .seconds(1),
  230. backoffMultiplier: 1.0,
  231. retryableStatusCodes: [.unavailable]
  232. )
  233. let serviceConfiguration = ServiceConfiguration(
  234. methodConfiguration: [
  235. MethodConfiguration(
  236. names: [MethodConfiguration.Name(service: "", method: "")],
  237. executionPolicy: .retry(defaultPolicy)
  238. )
  239. ]
  240. )
  241. return InProcessClientTransport(
  242. server: server,
  243. serviceConfiguration: serviceConfiguration
  244. )
  245. }
  246. }