2
0
Эх сурвалжийг харах

Add interoperability tests to XCTest (#436)

* Add interoperability tests to XCTest

* Update test manifests
George Barnett 6 жил өмнө
parent
commit
6eef038edd

+ 5 - 1
Package.swift

@@ -107,7 +107,11 @@ let package = Package(
     .testTarget(name: "SwiftGRPCTests",
                 dependencies: ["SwiftGRPC"]),
     .testTarget(name: "SwiftGRPCNIOTests",
-                dependencies: ["SwiftGRPC", "SwiftGRPCNIO", "SwiftGRPCNIOSampleData"]),
+                dependencies: [
+                  "SwiftGRPC",
+                  "SwiftGRPCNIO",
+                  "SwiftGRPCNIOSampleData",
+                  "SwiftGRPCNIOInteroperabilityTests"]),
   ],
   cLanguageStandard: .gnu11,
   cxxLanguageStandard: .cxx11)

+ 36 - 0
Sources/SwiftGRPCNIOInteroperabilityTests/InteroperabilityTestCase.swift

@@ -104,4 +104,40 @@ extension InteroperabilityTestCase {
       return TimeoutOnSleepingServer()
     }
   }
+
+  /// The set of server features required to run this test.
+  public var requiredServerFeatures: Set<ServerFeature> {
+    switch self {
+    case .emptyUnary:
+      return [.emptyCall]
+    case .cacheableUnary:
+      return [.cacheableUnaryCall]
+    case .largeUnary:
+      return [.unaryCall]
+    case .clientStreaming:
+      return [.streamingInputCall]
+    case .serverStreaming:
+      return [.streamingOutputCall]
+    case .pingPong:
+      return [.fullDuplexCall]
+    case .emptyStream:
+      return [.fullDuplexCall]
+    case .customMetadata:
+      return [.unaryCall, .fullDuplexCall, .echoMetadata]
+    case .statusCodeAndMessage:
+      return [.unaryCall, .fullDuplexCall, .echoStatus]
+    case .specialStatusMessage:
+      return [.unaryCall, .echoStatus]
+    case .unimplementedMethod:
+      return []
+    case .unimplementedService:
+      return []
+    case .cancelAfterBegin:
+      return [.streamingInputCall]
+    case .cancelAfterFirstResponse:
+      return [.fullDuplexCall]
+    case .timeoutOnSleepingServer:
+      return [.fullDuplexCall]
+    }
+  }
 }

+ 147 - 0
Tests/SwiftGRPCNIOTests/GRPCInteroperabilityTests.swift

@@ -0,0 +1,147 @@
+/*
+ * Copyright 2019, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import Foundation
+import SwiftGRPCNIO
+import SwiftGRPCNIOInteroperabilityTests
+import NIO
+import XCTest
+
+/// These are the gRPC interoperability tests running on the NIO client and server.
+class GRPCInsecureInteroperabilityTests: XCTestCase {
+  var useTLS: Bool { return false }
+
+  var serverEventLoopGroup: EventLoopGroup!
+  var server: GRPCServer!
+
+  var clientEventLoopGroup: EventLoopGroup!
+  var clientConnection: GRPCClientConnection!
+
+  override func setUp() {
+    super.setUp()
+
+    self.serverEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
+    self.server = try! makeInteroperabilityTestServer(
+      host: "localhost",
+      port: 0,
+      eventLoopGroup: self.serverEventLoopGroup!,
+      useTLS: self.useTLS
+    ).wait()
+
+    guard let serverPort = self.server.channel.localAddress?.port else {
+      XCTFail("Unable to get server port")
+      return
+    }
+
+    self.clientEventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
+    self.clientConnection = try! makeInteroperabilityTestClientConnection(
+      host: "localhost",
+      port: serverPort,
+      eventLoopGroup: self.clientEventLoopGroup,
+      useTLS: self.useTLS
+    ).wait()
+  }
+
+  override func tearDown() {
+    XCTAssertNoThrow(try self.clientConnection.close().wait())
+    XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
+    self.clientConnection = nil
+    self.clientEventLoopGroup = nil
+
+    XCTAssertNoThrow(try self.server.close().wait())
+    XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
+    self.server = nil
+    self.serverEventLoopGroup = nil
+
+    super.tearDown()
+  }
+
+  func doRunTest(_ testCase: InteroperabilityTestCase, file: StaticString = #file, line: UInt = #line) {
+    // Does the server support the test?
+    let implementedFeatures = TestServiceProvider_NIO.implementedFeatures
+    let missingFeatures = testCase.requiredServerFeatures.subtracting(implementedFeatures)
+    guard missingFeatures.isEmpty else {
+      print("\(testCase.name) requires features the server does not implement: \(missingFeatures)")
+      return
+    }
+
+    let test = testCase.makeTest()
+    XCTAssertNoThrow(try test.run(using: self.clientConnection), file: file, line: line)
+  }
+
+  func testEmptyUnary() {
+    self.doRunTest(.emptyUnary)
+  }
+
+  func testCacheableUnary() {
+    self.doRunTest(.cacheableUnary)
+  }
+
+  func testLargeUnary() {
+    self.doRunTest(.largeUnary)
+  }
+
+  func testClientStreaming() {
+    self.doRunTest(.clientStreaming)
+  }
+
+  func testServerStreaming() {
+    self.doRunTest(.serverStreaming)
+  }
+
+  func testPingPong() {
+    self.doRunTest(.pingPong)
+  }
+
+  func testEmptyStream() {
+    self.doRunTest(.emptyStream)
+  }
+
+  func testCustomMetadata() {
+    self.doRunTest(.customMetadata)
+  }
+
+  func testStatusCodeAndMessage() {
+    self.doRunTest(.statusCodeAndMessage)
+  }
+
+  func testSpecialStatusAndMessage() {
+    self.doRunTest(.specialStatusMessage)
+  }
+
+  func testUnimplementedMethod() {
+    self.doRunTest(.unimplementedMethod)
+  }
+
+  func testUnimplementedService() {
+    self.doRunTest(.unimplementedService)
+  }
+
+  func testCancelAfterBegin() {
+    self.doRunTest(.cancelAfterBegin)
+  }
+
+  func testCancelAfterFirstResponse() {
+    self.doRunTest(.cancelAfterFirstResponse)
+  }
+
+  func testTimeoutOnSleepingServer() {
+    self.doRunTest(.timeoutOnSleepingServer)
+  }
+}
+
+class GRPCSecureInteroperabilityTests: GRPCInsecureInteroperabilityTests {
+  override var useTLS: Bool { return true }
+}

+ 48 - 0
Tests/SwiftGRPCNIOTests/XCTestManifests.swift

@@ -24,6 +24,52 @@ extension GRPCChannelHandlerTests {
     ]
 }
 
+extension GRPCInsecureInteroperabilityTests {
+    // DO NOT MODIFY: This is autogenerated, use:
+    //   `swift test --generate-linuxmain`
+    // to regenerate.
+    static let __allTests__GRPCInsecureInteroperabilityTests = [
+        ("testCacheableUnary", testCacheableUnary),
+        ("testCancelAfterBegin", testCancelAfterBegin),
+        ("testCancelAfterFirstResponse", testCancelAfterFirstResponse),
+        ("testClientStreaming", testClientStreaming),
+        ("testCustomMetadata", testCustomMetadata),
+        ("testEmptyStream", testEmptyStream),
+        ("testEmptyUnary", testEmptyUnary),
+        ("testLargeUnary", testLargeUnary),
+        ("testPingPong", testPingPong),
+        ("testServerStreaming", testServerStreaming),
+        ("testSpecialStatusAndMessage", testSpecialStatusAndMessage),
+        ("testStatusCodeAndMessage", testStatusCodeAndMessage),
+        ("testTimeoutOnSleepingServer", testTimeoutOnSleepingServer),
+        ("testUnimplementedMethod", testUnimplementedMethod),
+        ("testUnimplementedService", testUnimplementedService),
+    ]
+}
+
+extension GRPCSecureInteroperabilityTests {
+    // DO NOT MODIFY: This is autogenerated, use:
+    //   `swift test --generate-linuxmain`
+    // to regenerate.
+    static let __allTests__GRPCSecureInteroperabilityTests = [
+        ("testCacheableUnary", testCacheableUnary),
+        ("testCancelAfterBegin", testCancelAfterBegin),
+        ("testCancelAfterFirstResponse", testCancelAfterFirstResponse),
+        ("testClientStreaming", testClientStreaming),
+        ("testCustomMetadata", testCustomMetadata),
+        ("testEmptyStream", testEmptyStream),
+        ("testEmptyUnary", testEmptyUnary),
+        ("testLargeUnary", testLargeUnary),
+        ("testPingPong", testPingPong),
+        ("testServerStreaming", testServerStreaming),
+        ("testSpecialStatusAndMessage", testSpecialStatusAndMessage),
+        ("testStatusCodeAndMessage", testStatusCodeAndMessage),
+        ("testTimeoutOnSleepingServer", testTimeoutOnSleepingServer),
+        ("testUnimplementedMethod", testUnimplementedMethod),
+        ("testUnimplementedService", testUnimplementedService),
+    ]
+}
+
 extension HTTP1ToRawGRPCServerCodecTests {
     // DO NOT MODIFY: This is autogenerated, use:
     //   `swift test --generate-linuxmain`
@@ -196,6 +242,8 @@ public func __allTests() -> [XCTestCaseEntry] {
     return [
         testCase(ClientThrowingWhenServerReturningErrorTests.__allTests__ClientThrowingWhenServerReturningErrorTests),
         testCase(GRPCChannelHandlerTests.__allTests__GRPCChannelHandlerTests),
+        testCase(GRPCInsecureInteroperabilityTests.__allTests__GRPCInsecureInteroperabilityTests),
+        testCase(GRPCSecureInteroperabilityTests.__allTests__GRPCSecureInteroperabilityTests),
         testCase(HTTP1ToRawGRPCServerCodecTests.__allTests__HTTP1ToRawGRPCServerCodecTests),
         testCase(LengthPrefixedMessageReaderTests.__allTests__LengthPrefixedMessageReaderTests),
         testCase(NIOClientCancellingTests.__allTests__NIOClientCancellingTests),