Преглед изворни кода

Remove potentially hard-to-synchronize status messages on endOfStream in Echo example.

Tim Burks пре 7 година
родитељ
комит
8598343e77

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

@@ -108,7 +108,6 @@ Group {
         let responseMessage = try expandCall.receive()
         print("expand received: \(responseMessage.text)")
       } catch Echo_EchoClientError.endOfStream {
-        print("expand closed")
         running = false
       }
     }
@@ -152,7 +151,6 @@ Group {
           let responseMessage = try updateCall.receive()
           print("update received: \(responseMessage.text)")
         } catch Echo_EchoClientError.endOfStream {
-          print("update closed")
           running = false
         } catch (let error) {
           print("error: \(error)")

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

@@ -5,7 +5,6 @@ expand received: Swift echo expand (0): Testing
 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
 
 
@@ -25,7 +24,6 @@ update sending: 2
 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
 
 

+ 8 - 4
Examples/Echo2/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 {
@@ -106,17 +108,19 @@ Group {
         let responseMessage = try expandCall.receive()
         print("expand received: \(responseMessage.text)")
       } catch Echo_EchoClientError.endOfStream {
-        print("expand closed")
         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 +132,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 {
@@ -145,8 +151,6 @@ Group {
           let responseMessage = try updateCall.receive()
           print("update received: \(responseMessage.text)")
         } catch Echo_EchoClientError.endOfStream {
-          print("update closed")
-          sem.signal()
           running = false
         } catch (let error) {
           print("error: \(error)")