GRPCInteroperabilityTests.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. }
  143. #endif // canImport(NIOSSL)
  144. #if compiler(>=5.6)
  145. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  146. class GRPCInsecureInteroperabilityAsyncTests: GRPCInsecureInteroperabilityTests {
  147. override func makeProvider() -> CallHandlerProvider {
  148. return TestServiceAsyncProvider()
  149. }
  150. }
  151. #if canImport(NIOSSL)
  152. @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
  153. class GRPCSecureInteroperabilityAsyncTests: GRPCInsecureInteroperabilityAsyncTests {
  154. override var useTLS: Bool { return true }
  155. }
  156. #endif // canImport(NIOSSL)
  157. #endif // compiler(>=5.6)