Browse Source

Fix races in Echo samples.

For this demo, each call should block until its completion handler is called.
Tim Burks 8 years ago
parent
commit
3bafd530b8

+ 8 - 2
Examples/Echo/PackageManager/Sources/main.swift

@@ -97,8 +97,10 @@ Group {
     var requestMessage = Echo_EchoRequest()
     requestMessage.text = message
     print("expand sending: " + requestMessage.text)
+    let sem = DispatchSemaphore(value: 0)
     let expandCall = try service.expand(requestMessage) { result in
       print("expand completed with result \(result)")
+      sem.signal()
     }
     var running = true
     while running {
@@ -110,13 +112,16 @@ Group {
         running = false
       }
     }
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
   }
 
   $0.command("collect", sslFlag, addressOption("localhost"), portOption, messageOption,
              description: "Perform a client-streaming collect().") { ssl, address, port, message in
     let service = buildEchoService(ssl, address, port, message)
+    let sem = DispatchSemaphore(value: 0)
     let collectCall = try service.collect { result in
       print("collect completed with result \(result)")
+      sem.signal()
     }
     let parts = message.components(separatedBy: " ")
     for part in parts {
@@ -128,16 +133,18 @@ Group {
     }
     let responseMessage = try collectCall.closeAndReceive()
     print("collect received: \(responseMessage.text)")
+    _ = sem.wait(timeout: DispatchTime.distantFuture)
   }
 
   $0.command("update", sslFlag, addressOption("localhost"), portOption, messageOption,
              description: "Perform a bidirectional-streaming update().") { ssl, address, port, message in
     let service = buildEchoService(ssl, address, port, message)
+    let sem = DispatchSemaphore(value: 0)
     let updateCall = try service.update { result in
       print("update completed with result \(result)")
+      sem.signal()
     }
 
-    let sem = DispatchSemaphore(value: 0)
     DispatchQueue.global().async {
       var running = true
       while running {
@@ -146,7 +153,6 @@ Group {
           print("update received: \(responseMessage.text)")
         } catch Echo_EchoClientError.endOfStream {
           print("update closed")
-          sem.signal()
           running = false
         } catch (let error) {
           print("error: \(error)")

+ 9 - 0
Examples/Echo/PackageManager/test.gold

@@ -6,11 +6,17 @@ expand received: Swift echo expand (1): 1
 expand received: Swift echo expand (2): 2
 expand received: Swift echo expand (3): 3
 expand closed
+expand completed with result status ok: OK
+
+
 collect sending: Testing
 collect sending: 1
 collect sending: 2
 collect sending: 3
 collect received: Swift echo collect: Testing 1 2 3
+collect completed with result status ok: OK
+
+
 update sending: Testing
 update received: Swift echo update (1): Testing
 update sending: 1
@@ -20,3 +26,6 @@ update received: Swift echo update (3): 2
 update sending: 3
 update received: Swift echo update (4): 3
 update closed
+update completed with result status ok: OK
+
+