GRPCInteroperabilityTests.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  202. class GRPCInsecureInteroperabilityAsyncTests: GRPCInsecureInteroperabilityTests {
  203. override func makeProvider() -> CallHandlerProvider {
  204. return TestServiceAsyncProvider()
  205. }
  206. override func testEmptyStream() {
  207. super.testEmptyStream()
  208. }
  209. override func testPingPong() {
  210. super.testPingPong()
  211. }
  212. override func testEmptyUnary() {
  213. super.testEmptyUnary()
  214. }
  215. override func testTimeoutOnSleepingServer() {
  216. super.testTimeoutOnSleepingServer()
  217. }
  218. override func testCacheableUnary() {
  219. super.testCacheableUnary()
  220. }
  221. override func testLargeUnary() {
  222. super.testLargeUnary()
  223. }
  224. override func testServerCompressedUnary() {
  225. super.testServerCompressedUnary()
  226. }
  227. override func testStatusCodeAndMessage() {
  228. super.testStatusCodeAndMessage()
  229. }
  230. override func testUnimplementedService() {
  231. super.testUnimplementedService()
  232. }
  233. override func testCancelAfterBegin() {
  234. super.testCancelAfterBegin()
  235. }
  236. override func testCustomMetadata() {
  237. super.testCustomMetadata()
  238. }
  239. override func testServerStreaming() {
  240. super.testServerStreaming()
  241. }
  242. override func testClientStreaming() {
  243. super.testClientStreaming()
  244. }
  245. override func testUnimplementedMethod() {
  246. super.testUnimplementedMethod()
  247. }
  248. override func testServerCompressedStreaming() {
  249. super.testServerCompressedStreaming()
  250. }
  251. override func testCancelAfterFirstResponse() {
  252. super.testCancelAfterFirstResponse()
  253. }
  254. override func testSpecialStatusAndMessage() {
  255. super.testSpecialStatusAndMessage()
  256. }
  257. override func testClientCompressedStreaming() {
  258. super.testClientCompressedStreaming()
  259. }
  260. override func testClientCompressedUnary() {
  261. super.testClientCompressedUnary()
  262. }
  263. }
  264. #if canImport(NIOSSL)
  265. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  266. class GRPCSecureInteroperabilityAsyncTests: GRPCInsecureInteroperabilityAsyncTests {
  267. override var useTLS: Bool { return true }
  268. override func testServerStreaming() {
  269. super.testServerStreaming()
  270. }
  271. override func testLargeUnary() {
  272. super.testLargeUnary()
  273. }
  274. override func testServerCompressedUnary() {
  275. super.testServerCompressedUnary()
  276. }
  277. override func testUnimplementedMethod() {
  278. super.testUnimplementedMethod()
  279. }
  280. override func testServerCompressedStreaming() {
  281. super.testServerCompressedStreaming()
  282. }
  283. override func testCustomMetadata() {
  284. super.testCustomMetadata()
  285. }
  286. override func testCancelAfterBegin() {
  287. super.testCancelAfterBegin()
  288. }
  289. override func testClientStreaming() {
  290. super.testClientStreaming()
  291. }
  292. override func testCacheableUnary() {
  293. super.testCacheableUnary()
  294. }
  295. override func testSpecialStatusAndMessage() {
  296. super.testSpecialStatusAndMessage()
  297. }
  298. override func testTimeoutOnSleepingServer() {
  299. super.testTimeoutOnSleepingServer()
  300. }
  301. override func testClientCompressedUnary() {
  302. super.testClientCompressedUnary()
  303. }
  304. override func testStatusCodeAndMessage() {
  305. super.testStatusCodeAndMessage()
  306. }
  307. override func testCancelAfterFirstResponse() {
  308. super.testCancelAfterFirstResponse()
  309. }
  310. override func testPingPong() {
  311. super.testPingPong()
  312. }
  313. override func testEmptyStream() {
  314. super.testEmptyStream()
  315. }
  316. override func testEmptyUnary() {
  317. super.testEmptyUnary()
  318. }
  319. override func testUnimplementedService() {
  320. super.testUnimplementedService()
  321. }
  322. override func testClientCompressedStreaming() {
  323. super.testClientCompressedStreaming()
  324. }
  325. }
  326. #endif // canImport(NIOSSL)