2
0

GRPCInteroperabilityTests.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. override func setUp() {
  29. super.setUp()
  30. self.serverEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  31. self.server = try! makeInteroperabilityTestServer(
  32. host: "localhost",
  33. port: 0,
  34. eventLoopGroup: self.serverEventLoopGroup!,
  35. useTLS: self.useTLS
  36. ).wait()
  37. guard let serverPort = self.server.channel.localAddress?.port else {
  38. XCTFail("Unable to get server port")
  39. return
  40. }
  41. self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  42. self.clientConnection = try! makeInteroperabilityTestClientConnection(
  43. host: "localhost",
  44. port: serverPort,
  45. eventLoopGroup: self.clientEventLoopGroup,
  46. useTLS: self.useTLS
  47. )
  48. }
  49. override func tearDown() {
  50. XCTAssertNoThrow(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.serverEventLoopGroup = nil
  58. super.tearDown()
  59. }
  60. func doRunTest(_ testCase: InteroperabilityTestCase, file: StaticString = #file, 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. XCTAssertNoThrow(try test.run(using: self.clientConnection), file: file, line: line)
  70. }
  71. func testEmptyUnary() {
  72. self.doRunTest(.emptyUnary)
  73. }
  74. func testCacheableUnary() {
  75. self.doRunTest(.cacheableUnary)
  76. }
  77. func testLargeUnary() {
  78. self.doRunTest(.largeUnary)
  79. }
  80. func testClientStreaming() {
  81. self.doRunTest(.clientStreaming)
  82. }
  83. func testServerStreaming() {
  84. self.doRunTest(.serverStreaming)
  85. }
  86. func testPingPong() {
  87. self.doRunTest(.pingPong)
  88. }
  89. func testEmptyStream() {
  90. self.doRunTest(.emptyStream)
  91. }
  92. func testCustomMetadata() {
  93. self.doRunTest(.customMetadata)
  94. }
  95. func testStatusCodeAndMessage() {
  96. self.doRunTest(.statusCodeAndMessage)
  97. }
  98. func testSpecialStatusAndMessage() {
  99. self.doRunTest(.specialStatusMessage)
  100. }
  101. func testUnimplementedMethod() {
  102. self.doRunTest(.unimplementedMethod)
  103. }
  104. func testUnimplementedService() {
  105. self.doRunTest(.unimplementedService)
  106. }
  107. func testCancelAfterBegin() {
  108. self.doRunTest(.cancelAfterBegin)
  109. }
  110. func testCancelAfterFirstResponse() {
  111. self.doRunTest(.cancelAfterFirstResponse)
  112. }
  113. func testTimeoutOnSleepingServer() {
  114. self.doRunTest(.timeoutOnSleepingServer)
  115. }
  116. }
  117. class GRPCSecureInteroperabilityTests: GRPCInsecureInteroperabilityTests {
  118. override var useTLS: Bool { return true }
  119. }