GRPCInteroperabilityTests.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. useTLS: self.useTLS,
  38. logger: self.serverLogger
  39. ).wait()
  40. guard let serverPort = self.server.channel.localAddress?.port else {
  41. XCTFail("Unable to get server port")
  42. return
  43. }
  44. self.serverPort = serverPort
  45. self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  46. }
  47. override func tearDown() {
  48. // This may throw if we shutdown before the channel was ready.
  49. try? self.clientConnection?.close().wait()
  50. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  51. self.clientConnection = nil
  52. self.clientEventLoopGroup = nil
  53. XCTAssertNoThrow(try self.server.close().wait())
  54. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  55. self.server = nil
  56. self.serverPort = nil
  57. self.serverEventLoopGroup = nil
  58. super.tearDown()
  59. }
  60. private func doRunTest(_ testCase: InteroperabilityTestCase, line: UInt = #line) {
  61. // Does the server support the test?
  62. let implementedFeatures = TestServiceProvider.implementedFeatures
  63. let missingFeatures = testCase.requiredServerFeatures.subtracting(implementedFeatures)
  64. guard missingFeatures.isEmpty else {
  65. print("\(testCase.name) requires features the server does not implement: \(missingFeatures)")
  66. return
  67. }
  68. let test = testCase.makeTest()
  69. let builder = makeInteroperabilityTestClientBuilder(
  70. group: self.clientEventLoopGroup,
  71. useTLS: self.useTLS
  72. ).withBackgroundActivityLogger(self.clientLogger)
  73. test.configure(builder: builder)
  74. self.clientConnection = builder.connect(host: "localhost", port: self.serverPort)
  75. XCTAssertNoThrow(try test.run(using: self.clientConnection), line: line)
  76. }
  77. func testEmptyUnary() {
  78. self.doRunTest(.emptyUnary)
  79. }
  80. func testCacheableUnary() {
  81. self.doRunTest(.cacheableUnary)
  82. }
  83. func testLargeUnary() {
  84. self.doRunTest(.largeUnary)
  85. }
  86. func testClientCompressedUnary() {
  87. self.doRunTest(.clientCompressedUnary)
  88. }
  89. func testServerCompressedUnary() {
  90. self.doRunTest(.serverCompressedUnary)
  91. }
  92. func testClientStreaming() {
  93. self.doRunTest(.clientStreaming)
  94. }
  95. func testClientCompressedStreaming() {
  96. self.doRunTest(.clientCompressedStreaming)
  97. }
  98. func testServerStreaming() {
  99. self.doRunTest(.serverStreaming)
  100. }
  101. func testServerCompressedStreaming() {
  102. self.doRunTest(.serverCompressedStreaming)
  103. }
  104. func testPingPong() {
  105. self.doRunTest(.pingPong)
  106. }
  107. func testEmptyStream() {
  108. self.doRunTest(.emptyStream)
  109. }
  110. func testCustomMetadata() {
  111. self.doRunTest(.customMetadata)
  112. }
  113. func testStatusCodeAndMessage() {
  114. self.doRunTest(.statusCodeAndMessage)
  115. }
  116. func testSpecialStatusAndMessage() {
  117. self.doRunTest(.specialStatusMessage)
  118. }
  119. func testUnimplementedMethod() {
  120. self.doRunTest(.unimplementedMethod)
  121. }
  122. func testUnimplementedService() {
  123. self.doRunTest(.unimplementedService)
  124. }
  125. func testCancelAfterBegin() {
  126. self.doRunTest(.cancelAfterBegin)
  127. }
  128. func testCancelAfterFirstResponse() {
  129. self.doRunTest(.cancelAfterFirstResponse)
  130. }
  131. func testTimeoutOnSleepingServer() {
  132. self.doRunTest(.timeoutOnSleepingServer)
  133. }
  134. }
  135. #if canImport(NIOSSL)
  136. class GRPCSecureInteroperabilityTests: GRPCInsecureInteroperabilityTests {
  137. override var useTLS: Bool { return true }
  138. }
  139. #endif // canImport(NIOSSL)