Skip to content

Commit

Permalink
Wait for process to finish before flushing channel
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Jan 16, 2020
1 parent 94924dd commit d3335fe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,21 @@ func main() {
os.Exit(1)
}

ch := make(chan bool)

go func() {
io.Copy(io.MultiWriter(producer, os.Stdout), stdout)
ch <- true
}()

go func() {
io.Copy(io.MultiWriter(producer, os.Stderr), stderr)
ch <- true
}()

err = cmd.Run()
<-ch
<-ch
producer.Close()
if err != nil {
fmt.Printf("Err: %v\n", err)
Expand Down
9 changes: 2 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"io/ioutil"
"strings"
"testing"
Expand All @@ -10,7 +9,7 @@ import (
func TestConsumerAndProducer(t *testing.T) {
p, err := NewProducer("redis://localhost:6379", "testKey")
if err != nil {
t.Errorf("Create Producer Error: %v", err)
t.Fatalf("Create Producer Error: %v", err)
}

_, err = p.Write([]byte("Here's a "))
Expand All @@ -25,8 +24,6 @@ func TestConsumerAndProducer(t *testing.T) {

p.Close()

fmt.Println("Writing done.")

c, err := NewConsumer("redis://localhost:6379", "testKey")
bytes, err := ioutil.ReadAll(c)
if err != nil {
Expand All @@ -42,7 +39,7 @@ func TestConsumerAndProducer(t *testing.T) {
func TestLargeMessage(t *testing.T) {
p, err := NewProducer("redis://localhost:6379", "testKey2")
if err != nil {
t.Errorf("Create Producer Error: %v", err)
t.Fatalf("Create Producer Error: %v", err)
}

longString := strings.Repeat("a", 10000)
Expand All @@ -54,8 +51,6 @@ func TestLargeMessage(t *testing.T) {

p.Close()

fmt.Println("Writing done.")

c, err := NewConsumer("redis://localhost:6379", "testKey2")
bytes, err := ioutil.ReadAll(c)
if err != nil {
Expand Down

0 comments on commit d3335fe

Please sign in to comment.