GRPCInteroperabilityTests.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. import GRPC
  18. import GRPCInteroperabilityTestsImplementation
  19. import NIOCore
  20. import NIOPosix
  21. import XCTest
  22. /// These are the gRPC interoperability tests running on the NIO client and server.
  23. class GRPCInsecureInteroperabilityTests: GRPCTestCase {
  24. var useTLS: Bool { return false }
  25. var serverEventLoopGroup: EventLoopGroup!
  26. var server: Server!
  27. var serverPort: Int!
  28. var clientEventLoopGroup: EventLoopGroup!
  29. var clientConnection: ClientConnection!
  30. override func setUp() {
  31. super.setUp()
  32. self.serverEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  33. self.server = try! makeInteroperabilityTestServer(
  34. host: "localhost",
  35. port: 0,
  36. eventLoopGroup: self.serverEventLoopGroup!,
  37. serviceProviders: [self.makeProvider()],
  38. useTLS: self.useTLS,
  39. logger: self.serverLogger
  40. ).wait()
  41. guard let serverPort = self.server.channel.localAddress?.port else {
  42. XCTFail("Unable to get server port")
  43. return
  44. }
  45. self.serverPort = serverPort
  46. self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  47. }
  48. override func tearDown() {
  49. // This may throw if we shutdown before the channel was ready.
  50. try? self.clientConnection?.close().wait()
  51. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  52. self.clientConnection = nil
  53. self.clientEventLoopGroup = nil
  54. XCTAssertNoThrow(try self.server.close().wait())
  55. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  56. self.server = nil
  57. self.serverPort = nil
  58. self.serverEventLoopGroup = nil
  59. super.tearDown()
  60. }
  61. internal func makeProvider() -> CallHandlerProvider {
  62. return TestServiceProvider()
  63. }
  64. private func doRunTest(_ testCase: InteroperabilityTestCase, line: UInt = #line) {
  65. // Does the server support the test?
  66. let implementedFeatures = TestServiceProvider.implementedFeatures
  67. let missingFeatures = testCase.requiredServerFeatures.subtracting(implementedFeatures)
  68. guard missingFeatures.isEmpty else {
  69. print("\(testCase.name) requires features the server does not implement: \(missingFeatures)")
  70. return
  71. }
  72. let test = testCase.makeTest()
  73. let builder = makeInteroperabilityTestClientBuilder(
  74. group: self.clientEventLoopGroup,
  75. useTLS: self.useTLS
  76. ).withBackgroundActivityLogger(self.clientLogger)
  77. test.configure(builder: builder)
  78. self.clientConnection = builder.connect(host: "localhost", port: self.serverPort)
  79. XCTAssertNoThrow(try test.run(using: self.clientConnection), line: line)
  80. }
  81. func testEmptyUnary() {
  82. self.doRunTest(.emptyUnary)
  83. }
  84. func testCacheableUnary() {
  85. self.doRunTest(.cacheableUnary)
  86. }
  87. func testLargeUnary() {
  88. self.doRunTest(.largeUnary)
  89. }
  90. func testClientCompressedUnary() {
  91. self.doRunTest(.clientCompressedUnary)
  92. }
  93. func testServerCompressedUnary() {
  94. self.doRunTest(.serverCompressedUnary)
  95. }
  96. func testClientStreaming() {
  97. self.doRunTest(.clientStreaming)
  98. }
  99. func testClientCompressedStreaming() {
  100. self.doRunTest(.clientCompressedStreaming)
  101. }
  102. func testServerStreaming() {
  103. self.doRunTest(.serverStreaming)
  104. }
  105. func testServerCompressedStreaming() {
  106. self.doRunTest(.serverCompressedStreaming)
  107. }
  108. func testPingPong() {
  109. self.doRunTest(.pingPong)
  110. }
  111. func testEmptyStream() {
  112. self.doRunTest(.emptyStream)
  113. }
  114. func testCustomMetadata() {
  115. self.doRunTest(.customMetadata)
  116. }
  117. func testStatusCodeAndMessage() {
  118. self.doRunTest(.statusCodeAndMessage)
  119. }
  120. func testSpecialStatusAndMessage() {
  121. self.doRunTest(.specialStatusMessage)
  122. }
  123. func testUnimplementedMethod() {
  124. self.doRunTest(.unimplementedMethod)
  125. }
  126. func testUnimplementedService() {
  127. self.doRunTest(.unimplementedService)
  128. }
  129. func testCancelAfterBegin() {
  130. self.doRunTest(.cancelAfterBegin)
  131. }
  132. func testCancelAfterFirstResponse() {
  133. self.doRunTest(.cancelAfterFirstResponse)
  134. }
  135. func testTimeoutOnSleepingServer() {
  136. self.doRunTest(.timeoutOnSleepingServer)
  137. }
  138. }
  139. #if canImport(NIOSSL)
  140. class GRPCSecureInteroperabilityTests: GRPCInsecureInteroperabilityTests {
  141. override var useTLS: Bool { return true }
  142. override func testEmptyUnary() {
  143. super.testEmptyUnary()
  144. }
  145. override func testCacheableUnary() {
  146. super.testCacheableUnary()
  147. }
  148. override func testLargeUnary() {
  149. super.testLargeUnary()
  150. }
  151. override func testClientCompressedUnary() {
  152. super.testClientCompressedUnary()
  153. }
  154. override func testServerCompressedUnary() {
  155. super.testServerCompressedUnary()
  156. }
  157. override func testClientStreaming() {
  158. super.testClientStreaming()
  159. }
  160. override func testClientCompressedStreaming() {
  161. super.testClientCompressedStreaming()
  162. }
  163. override func testServerStreaming() {
  164. super.testServerStreaming()
  165. }
  166. override func testServerCompressedStreaming() {
  167. super.testServerCompressedStreaming()
  168. }
  169. override func testPingPong() {
  170. super.testPingPong()
  171. }
  172. override func testEmptyStream() {
  173. super.testEmptyStream()
  174. }
  175. override func testCustomMetadata() {
  176. super.testCustomMetadata()
  177. }
  178. override func testStatusCodeAndMessage() {
  179. super.testStatusCodeAndMessage()
  180. }
  181. override func testSpecialStatusAndMessage() {
  182. super.testSpecialStatusAndMessage()
  183. }
  184. override func testUnimplementedMethod() {
  185. super.testUnimplementedMethod()
  186. }
  187. override func testUnimplementedService() {
  188. super.testUnimplementedService()
  189. }
  190. override func testCancelAfterBegin() {
  191. super.testCancelAfterBegin()
  192. }
  193. override func testCancelAfterFirstResponse() {
  194. super.testCancelAfterFirstResponse()
  195. }
  196. override func testTimeoutOnSleepingServer() {
  197. super.testTimeoutOnSleepingServer()
  198. }
  199. }
  200. #endif // canImport(NIOSSL)
  201. #if compiler(>=5.6)
  202. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  203. class GRPCInsecureInteroperabilityAsyncTests: GRPCInsecureInteroperabilityTests {
  204. override func makeProvider() -> CallHandlerProvider {
  205. return TestServiceAsyncProvider()
  206. }
  207. override func testEmptyStream() {
  208. super.testEmptyStream()
  209. }
  210. override func testPingPong() {
  211. super.testPingPong()
  212. }
  213. override func testEmptyUnary() {
  214. super.testEmptyUnary()
  215. }
  216. override func testTimeoutOnSleepingServer() {
  217. super.testTimeoutOnSleepingServer()
  218. }
  219. override func testCacheableUnary() {
  220. super.testCacheableUnary()
  221. }
  222. override func testLargeUnary() {
  223. super.testLargeUnary()
  224. }
  225. override func testServerCompressedUnary() {
  226. super.testServerCompressedUnary()
  227. }
  228. override func testStatusCodeAndMessage() {
  229. super.testStatusCodeAndMessage()
  230. }
  231. override func testUnimplementedService() {
  232. super.testUnimplementedService()
  233. }
  234. override func testCancelAfterBegin() {
  235. super.testCancelAfterBegin()
  236. }
  237. override func testCustomMetadata() {
  238. super.testCustomMetadata()
  239. }
  240. override func testServerStreaming() {
  241. super.testServerStreaming()
  242. }
  243. override func testClientStreaming() {
  244. super.testClientStreaming()
  245. }
  246. override func testUnimplementedMethod() {
  247. super.testUnimplementedMethod()
  248. }
  249. override func testServerCompressedStreaming() {
  250. super.testServerCompressedStreaming()
  251. }
  252. override func testCancelAfterFirstResponse() {
  253. super.testCancelAfterFirstResponse()
  254. }
  255. override func testSpecialStatusAndMessage() {
  256. super.testSpecialStatusAndMessage()
  257. }
  258. override func testClientCompressedStreaming() {
  259. super.testClientCompressedStreaming()
  260. }
  261. override func testClientCompressedUnary() {
  262. super.testClientCompressedUnary()
  263. }
  264. }
  265. #if canImport(NIOSSL)
  266. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  267. class GRPCSecureInteroperabilityAsyncTests: GRPCInsecureInteroperabilityAsyncTests {
  268. override var useTLS: Bool { return true }
  269. override func testServerStreaming() {
  270. super.testServerStreaming()
  271. }
  272. override func testLargeUnary() {
  273. super.testLargeUnary()
  274. }
  275. override func testServerCompressedUnary() {
  276. super.testServerCompressedUnary()
  277. }
  278. override func testUnimplementedMethod() {
  279. super.testUnimplementedMethod()
  280. }
  281. override func testServerCompressedStreaming() {
  282. super.testServerCompressedStreaming()
  283. }
  284. override func testCustomMetadata() {
  285. super.testCustomMetadata()
  286. }
  287. override func testCancelAfterBegin() {
  288. super.testCancelAfterBegin()
  289. }
  290. override func testClientStreaming() {
  291. super.testClientStreaming()
  292. }
  293. override func testCacheableUnary() {
  294. super.testCacheableUnary()
  295. }
  296. override func testSpecialStatusAndMessage() {
  297. super.testSpecialStatusAndMessage()
  298. }
  299. override func testTimeoutOnSleepingServer() {
  300. super.testTimeoutOnSleepingServer()
  301. }
  302. override func testClientCompressedUnary() {
  303. super.testClientCompressedUnary()
  304. }
  305. override func testStatusCodeAndMessage() {
  306. super.testStatusCodeAndMessage()
  307. }
  308. override func testCancelAfterFirstResponse() {
  309. super.testCancelAfterFirstResponse()
  310. }
  311. override func testPingPong() {
  312. super.testPingPong()
  313. }
  314. override func testEmptyStream() {
  315. super.testEmptyStream()
  316. }
  317. override func testEmptyUnary() {
  318. super.testEmptyUnary()
  319. }
  320. override func testUnimplementedService() {
  321. super.testUnimplementedService()
  322. }
  323. override func testClientCompressedStreaming() {
  324. super.testClientCompressedStreaming()
  325. }
  326. }
  327. #endif // canImport(NIOSSL)
  328. #endif // compiler(>=5.6)