GRPCInteroperabilityTests.swift 4.6 KB

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