1- /*
2- * Copyright 2017, 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 XCTest
17- import Foundation
18- import Dispatch
19- @testable import gRPC
1+ /*
2+ * Copyright 2017, 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 XCTest
17+ import Foundation
18+ import Dispatch
19+ @testable import gRPC
2020
21- func Log( _ message : String ) {
21+ func Log( _ message : String ) {
2222 FileHandle . standardError. write ( ( message + " \n " ) . data ( using: . utf8) !)
23- }
23+ }
2424
25- class gRPCTests : XCTestCase {
25+ class gRPCTests : XCTestCase {
2626
2727 func testBasicSanity( ) {
2828 gRPC. initialize ( )
29- let latch = CountDownLatch ( 2 )
29+
30+ let sem = DispatchSemaphore ( value: 0 )
31+
32+ // start the server
3033 DispatchQueue . global ( ) . async ( ) {
3134 do {
32- try server ( )
35+ try runServer ( )
3336 } catch ( let error) {
3437 XCTFail ( " server error \( error) " )
3538 }
36- latch . signal ( )
39+ sem . signal ( ) // when the server exits, the test is finished
3740 }
38- DispatchQueue . global ( ) . async ( ) {
39- do {
40- try client ( )
41- } catch ( let error) {
42- XCTFail ( " client error \( error) " )
43- }
44- latch. signal ( )
41+
42+ // run the client
43+ do {
44+ try runClient ( )
45+ } catch ( let error) {
46+ XCTFail ( " client error \( error) " )
4547 }
46- latch. wait ( )
48+
49+ // stop the server
50+ server. stop ( )
51+
52+ // wait until the server has shut down
53+ _ = sem. wait ( timeout: DispatchTime . distantFuture)
4754 }
48- }
55+ }
4956
50- extension gRPCTests {
57+ extension gRPCTests {
5158 static var allTests : [ ( String , ( gRPCTests ) -> ( ) throws -> Void ) ] {
5259 return [
5360 ( " testBasicSanity " , testBasicSanity) ,
5461 ]
5562 }
56- }
63+ }
5764
58- let address = " localhost:8999 "
59- let host = " foo.test.google.fr "
60- let clientText = " hello, server! "
61- let serverText = " hello, client! "
62- let initialClientMetadata =
65+ let address = " localhost:8998 "
66+ let host = " foo.test.google.fr "
67+ let clientText = " hello, server! "
68+ let serverText = " hello, client! "
69+ let initialClientMetadata =
6370 [ " x " : " xylophone " ,
6471 " y " : " yu " ,
6572 " z " : " zither " ]
66- let initialServerMetadata =
73+ let initialServerMetadata =
6774 [ " a " : " Apple " ,
6875 " b " : " Banana " ,
6976 " c " : " Cherry " ]
70- let trailingServerMetadata =
77+ let trailingServerMetadata =
7178 [ " 0 " : " zero " ,
7279 " 1 " : " one " ,
7380 " 2 " : " two " ]
74- let steps = 30
75- let hello = " /hello "
76- let goodbye = " /goodbye "
77- let statusCode = 0
78- let statusMessage = " OK "
81+ let steps = 1
82+ let hello = " /hello "
83+ let statusCode = 0
84+ let statusMessage = " OK "
7985
80- func verify_metadata( _ metadata: Metadata , expected: [ String : String ] ) {
86+ let server = gRPC. Server ( address: address)
87+
88+ func verify_metadata( _ metadata: Metadata , expected: [ String : String ] ) {
8189 XCTAssertGreaterThanOrEqual ( metadata. count ( ) , expected. count)
8290 for i in 0 ..< metadata. count ( ) {
8391 if expected [ metadata. key ( i) ] != nil {
8492 XCTAssertEqual ( metadata. value ( i) , expected [ metadata. key ( i) ] )
8593 }
8694 }
87- }
95+ }
8896
89- func client ( ) throws {
97+ func runClient ( ) throws {
9098 let message = clientText. data ( using: . utf8)
9199 let channel = gRPC. Channel ( address: address)
92100 channel. host = host
93101 for i in 0 ..< steps {
94- let latch = CountDownLatch ( 1 )
95- let method = ( i < steps - 1 ) ? hello : goodbye
102+ let sem = DispatchSemaphore ( value : 0 )
103+ let method = hello
96104 let call = channel. makeCall ( method)
97105 let metadata = Metadata ( initialClientMetadata)
98106 try call. start ( . unary, metadata: metadata, message: message) {
@@ -111,27 +119,23 @@ func client() throws {
111119 let trailingMetadata = response. trailingMetadata!
112120 verify_metadata ( trailingMetadata, expected: trailingServerMetadata)
113121 // report completion
114- latch . signal ( )
122+ sem . signal ( )
115123 }
116124 // wait for the call to complete
117- latch. wait ( )
125+ _ = sem. wait ( timeout: DispatchTime . distantFuture)
126+ print ( " finished client step \( i) " )
118127 }
119- usleep ( 500 ) // temporarily delay calls to the channel destructor
120- }
128+ }
121129
122- func server( ) throws {
123- let server = gRPC. Server ( address: address)
130+ func runServer( ) throws {
124131 var requestCount = 0
125- let latch = CountDownLatch ( 1 )
132+ let sem = DispatchSemaphore ( value : 0 )
126133 server. run ( ) { ( requestHandler) in
127134 do {
135+ print ( " handling request \( requestHandler. method) " )
128136 requestCount += 1
129137 XCTAssertEqual ( requestHandler. host, host)
130- if ( requestCount < steps) {
131138 XCTAssertEqual ( requestHandler. method, hello)
132- } else {
133- XCTAssertEqual ( requestHandler. method, goodbye)
134- }
135139 let initialMetadata = requestHandler. requestMetadata
136140 verify_metadata ( initialMetadata, expected: initialClientMetadata)
137141 let initialMetadataToSend = Metadata ( initialServerMetadata)
@@ -140,9 +144,6 @@ func server() throws {
140144 let messageString = String ( data: messageData!, encoding: . utf8)
141145 XCTAssertEqual ( messageString, clientText)
142146 }
143- if requestHandler. method == goodbye {
144- server. stop ( )
145- }
146147 let replyMessage = serverText
147148 let trailingMetadataToSend = Metadata ( trailingServerMetadata)
148149 try requestHandler. sendResponse ( message: replyMessage. data ( using: . utf8) !,
@@ -154,9 +155,9 @@ func server() throws {
154155 }
155156 }
156157 server. onCompletion ( ) {
157- // exit the server thread
158- latch . signal ( )
158+ // return from runServer()
159+ sem . signal ( )
159160 }
160161 // wait for the server to exit
161- latch . wait ( )
162- }
162+ _ = sem . wait ( timeout : DispatchTime . distantFuture )
163+ }
0 commit comments