GRPCInteroperabilityTests.swift 4.6 KB

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