/* * 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 XCTest import Foundation import Dispatch @testable import gRPC 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 = ["0": "zero", "1": "one", "2": "two"] let steps = 10 let hello = "/hello" let statusCode = 0 let statusMessage = "OK" func runTest(useSSL: Bool) { gRPC.initialize() let serverRunningSemaphore = DispatchSemaphore(value: 0) // create the server var server : gRPC.Server! if useSSL { let certificateURL = URL(fileURLWithPath:"Tests/ssl.crt") let keyURL = URL(fileURLWithPath:"Tests/ssl.key") guard let certificate = try? String(contentsOf: certificateURL, encoding: .utf8), let key = try? String(contentsOf: keyURL, encoding: .utf8) else { return } server = gRPC.Server(address:address, key:key, certs:certificate) } else { server = gRPC.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(timeout: DispatchTime.distantFuture) } func verify_metadata(_ metadata: Metadata, expected: [String:String]) { XCTAssertGreaterThanOrEqual(metadata.count(), expected.count) for i in 0..