GRPCInteroperabilityTests.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  140. class GRPCInsecureInteroperabilityAsyncTests: GRPCInsecureInteroperabilityTests {
  141. override func makeProvider() -> CallHandlerProvider {
  142. return TestServiceAsyncProvider()
  143. }
  144. override func testEmptyStream() {
  145. super.testEmptyStream()
  146. }
  147. override func testPingPong() {
  148. super.testPingPong()
  149. }
  150. override func testEmptyUnary() {
  151. super.testEmptyUnary()
  152. }
  153. override func testTimeoutOnSleepingServer() {
  154. super.testTimeoutOnSleepingServer()
  155. }
  156. override func testCacheableUnary() {
  157. super.testCacheableUnary()
  158. }
  159. override func testLargeUnary() {
  160. super.testLargeUnary()
  161. }
  162. override func testServerCompressedUnary() {
  163. super.testServerCompressedUnary()
  164. }
  165. override func testStatusCodeAndMessage() {
  166. super.testStatusCodeAndMessage()
  167. }
  168. override func testUnimplementedService() {
  169. super.testUnimplementedService()
  170. }
  171. override func testCancelAfterBegin() {
  172. super.testCancelAfterBegin()
  173. }
  174. override func testCustomMetadata() {
  175. super.testCustomMetadata()
  176. }
  177. override func testServerStreaming() {
  178. super.testServerStreaming()
  179. }
  180. override func testClientStreaming() {
  181. super.testClientStreaming()
  182. }
  183. override func testUnimplementedMethod() {
  184. super.testUnimplementedMethod()
  185. }
  186. override func testServerCompressedStreaming() {
  187. super.testServerCompressedStreaming()
  188. }
  189. override func testCancelAfterFirstResponse() {
  190. super.testCancelAfterFirstResponse()
  191. }
  192. override func testSpecialStatusAndMessage() {
  193. super.testSpecialStatusAndMessage()
  194. }
  195. override func testClientCompressedStreaming() {
  196. super.testClientCompressedStreaming()
  197. }
  198. override func testClientCompressedUnary() {
  199. super.testClientCompressedUnary()
  200. }
  201. }