|
18 | 18 | import Dispatch |
19 | 19 | @testable import gRPC |
20 | 20 |
|
21 | | - func Log(_ message : String) { |
22 | | - FileHandle.standardError.write((message + "\n").data(using:.utf8)!) |
23 | | - } |
24 | | - |
25 | | - |
26 | | - |
27 | 21 | class gRPCTests: XCTestCase { |
28 | | - |
29 | | - func testBasicSanity() { |
30 | | - gRPC.initialize() |
31 | | - let server = gRPC.Server(address:address) |
32 | | - let sem = DispatchSemaphore(value: 0) |
33 | | - |
34 | | - // start the server |
35 | | - DispatchQueue.global().async() { |
36 | | - do { |
37 | | - try runServer(server:server) |
38 | | - } catch (let error) { |
39 | | - XCTFail("server error \(error)") |
40 | | - } |
41 | | - sem.signal() // when the server exits, the test is finished |
42 | | - } |
43 | | - |
44 | | - // run the client |
45 | | - do { |
46 | | - try runClient() |
47 | | - } catch (let error) { |
48 | | - XCTFail("client error \(error)") |
49 | | - } |
50 | | - |
51 | | - // stop the server |
52 | | - server.stop() |
53 | | - |
54 | | - // wait until the server has shut down |
55 | | - _ = sem.wait(timeout: DispatchTime.distantFuture) |
| 22 | + func testConnectivity() { |
| 23 | + runTest(useSSL:false) |
| 24 | + } |
| 25 | + func testConnectivitySecure() { |
| 26 | + runTest(useSSL:true) |
56 | 27 | } |
57 | 28 | } |
58 | 29 |
|
59 | 30 | extension gRPCTests { |
60 | 31 | static var allTests : [(String, (gRPCTests) -> () throws -> Void)] { |
61 | 32 | return [ |
62 | | - ("testBasicSanity", testBasicSanity), |
| 33 | + ("testConnectivity", testConnectivity), |
| 34 | + ("testConnectivitySecure", testConnectivitySecure), |
63 | 35 | ] |
64 | 36 | } |
65 | 37 | } |
66 | 38 |
|
67 | | - let address = "localhost:8081" |
68 | | - let host = "foo.test.google.fr" |
| 39 | + let address = "localhost:8085" |
| 40 | + let host = "example.com" |
69 | 41 | let clientText = "hello, server!" |
70 | 42 | let serverText = "hello, client!" |
71 | 43 | let initialClientMetadata = |
|
85 | 57 | let statusCode = 0 |
86 | 58 | let statusMessage = "OK" |
87 | 59 |
|
| 60 | + func runTest(useSSL: Bool) { |
| 61 | + gRPC.initialize() |
| 62 | + |
| 63 | + let serverRunningSemaphore = DispatchSemaphore(value: 0) |
| 64 | + |
| 65 | + // create the server |
| 66 | + var server : gRPC.Server! |
| 67 | + if useSSL { |
| 68 | + let certificateURL = URL(fileURLWithPath:"Tests/ssl.crt") |
| 69 | + let keyURL = URL(fileURLWithPath:"Tests/ssl.key") |
| 70 | + guard |
| 71 | + let certificate = try? String(contentsOf: certificateURL, encoding: .utf8), |
| 72 | + let key = try? String(contentsOf: keyURL, encoding: .utf8) |
| 73 | + else { |
| 74 | + return |
| 75 | + } |
| 76 | + server = gRPC.Server(address:address, |
| 77 | + key:key, |
| 78 | + certs:certificate) |
| 79 | + } else { |
| 80 | + server = gRPC.Server(address:address) |
| 81 | + } |
| 82 | + |
| 83 | + // start the server |
| 84 | + DispatchQueue.global().async() { |
| 85 | + do { |
| 86 | + |
| 87 | + try runServer(server:server) |
| 88 | + } catch (let error) { |
| 89 | + XCTFail("server error \(error)") |
| 90 | + } |
| 91 | + serverRunningSemaphore.signal() // when the server exits, the test is finished |
| 92 | + } |
| 93 | + |
| 94 | + // run the client |
| 95 | + do { |
| 96 | + try runClient(useSSL:useSSL) |
| 97 | + } catch (let error) { |
| 98 | + XCTFail("client error \(error)") |
| 99 | + } |
| 100 | + |
| 101 | + // stop the server |
| 102 | + server.stop() |
| 103 | + |
| 104 | + // wait until the server has shut down |
| 105 | + _ = serverRunningSemaphore.wait(timeout: DispatchTime.distantFuture) |
| 106 | + } |
| 107 | + |
88 | 108 | func verify_metadata(_ metadata: Metadata, expected: [String:String]) { |
89 | 109 | XCTAssertGreaterThanOrEqual(metadata.count(), expected.count) |
90 | 110 | for i in 0..<metadata.count() { |
|
94 | 114 | } |
95 | 115 | } |
96 | 116 |
|
97 | | - func runClient() throws { |
| 117 | + func runClient(useSSL: Bool) throws { |
98 | 118 | let message = clientText.data(using: .utf8) |
99 | | - let channel = gRPC.Channel(address:address) |
| 119 | + var channel : gRPC.Channel! |
| 120 | + |
| 121 | + if useSSL { |
| 122 | + let certificateURL = URL(fileURLWithPath:"Tests/ssl.crt") |
| 123 | + guard |
| 124 | + let certificates = try? String(contentsOf: certificateURL, encoding: .utf8) |
| 125 | + else { |
| 126 | + return |
| 127 | + } |
| 128 | + let host = "example.com" |
| 129 | + channel = gRPC.Channel(address:address, certificates:certificates, host:host) |
| 130 | + } else { |
| 131 | + channel = gRPC.Channel(address:address) |
| 132 | + } |
| 133 | + |
100 | 134 | channel.host = host |
101 | 135 | for i in 0..<steps { |
102 | | - let sem = DispatchSemaphore(value: 0) |
| 136 | + let sem = DispatchSemaphore(value: 0) |
103 | 137 | let method = hello |
104 | 138 | let call = channel.makeCall(method) |
105 | 139 | let metadata = Metadata(initialClientMetadata) |
|
123 | 157 | } |
124 | 158 | // wait for the call to complete |
125 | 159 | _ = sem.wait(timeout: DispatchTime.distantFuture) |
126 | | - print("finished client step \(i)") |
127 | 160 | } |
128 | 161 | } |
129 | 162 |
|
|
132 | 165 | let sem = DispatchSemaphore(value: 0) |
133 | 166 | server.run() {(requestHandler) in |
134 | 167 | do { |
135 | | - print("handling request \(requestHandler.method)") |
136 | 168 | requestCount += 1 |
137 | 169 | XCTAssertEqual(requestHandler.host, host) |
138 | | - XCTAssertEqual(requestHandler.method, hello) |
| 170 | + XCTAssertEqual(requestHandler.method, hello) |
139 | 171 | let initialMetadata = requestHandler.requestMetadata |
140 | 172 | verify_metadata(initialMetadata, expected: initialClientMetadata) |
141 | 173 | let initialMetadataToSend = Metadata(initialServerMetadata) |
|
0 commit comments