/* * Copyright 2017, 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 Dispatch import Foundation @testable import SwiftGRPC import XCTest class gRPCTests: XCTestCase { func testConnectivity() { runTest(useSSL: false) } func testConnectivitySecure() { runTest(useSSL: true) } static var allTests: [(String, (gRPCTests) -> () throws -> Void)] { return [ ("testConnectivity", testConnectivity), ("testConnectivitySecure", testConnectivitySecure) ] } } let address = "localhost:8085" let host = "example.com" let clientText = "hello, server!" let serverText = "hello, client!" let initialClientMetadata = [ "x": "xylophone", "y": "yu", "z": "zither" ] let initialServerMetadata = [ "a": "Apple", "b": "Banana", "c": "Cherry" ] let trailingServerMetadata = [ // We have more than ten entries here to ensure that even large metadata entries work // and aren't limited by e.g. a fixed-size entry buffer. "0": "zero", "1": "one", "2": "two", "3": "three", "4": "four", "5": "five", "6": "six", "7": "seven", "8": "eight", "9": "nine", "10": "ten", "11": "eleven", "12": "twelve" ] let steps = 10 let hello = "/hello.unary" let helloServerStream = "/hello.server-stream" let helloBiDiStream = "/hello.bidi-stream" // Return code/message for unary test let oddStatusCode = StatusCode.ok let oddStatusMessage = "OK" let evenStatusCode = StatusCode.notFound let eventStatusMessage = "Not Found" func runTest(useSSL: Bool) { gRPC.initialize() let serverRunningSemaphore = DispatchSemaphore(value: 0) // create the server let server: Server if useSSL { server = Server(address: address, key: String(data: keyForTests, encoding: .utf8)!, certs: String(data: certificateForTests, encoding: .utf8)!) } else { server = Server(address: address) } // start the server DispatchQueue.global().async { do { try runServer(server: server) } catch (let error) { XCTFail("server error \(error)") } serverRunningSemaphore.signal() // when the server exits, the test is finished } // run the client do { try runClient(useSSL: useSSL) } catch (let error) { XCTFail("client error \(error)") } // stop the server server.stop() // wait until the server has shut down _ = serverRunningSemaphore.wait() } func verify_metadata(_ metadata: Metadata, expected: [String: String], file: StaticString = #file, line: UInt = #line) { XCTAssertGreaterThanOrEqual(metadata.count(), expected.count) var allPresentKeys = Set() for i in 0..