forked from zimbatm/socketmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_group_test.go
52 lines (47 loc) · 1 KB
/
process_group_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"bytes"
"log"
"os"
"sync"
"testing"
"time"
)
func Test_logOutput(t *testing.T) {
t.Run("wg waits for logOutput", func(t *testing.T) {
var stdErr bytes.Buffer
log.SetOutput(&stdErr)
log.SetFlags(0)
ioReader, ioWriter, err := os.Pipe()
if err != nil {
t.Fatal("unexpected error", err)
}
inputs := []string{"foo", "bar"}
want := "[1] foo\n[1] bar\n"
var waitForWrite sync.WaitGroup
waitForWrite.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
for _, input := range inputs {
_, err = ioWriter.WriteString(input + "\n")
if err != nil {
t.Error("unexpected error", err)
return
}
time.Sleep(5 * time.Millisecond)
}
err = ioWriter.Close()
if err != nil {
t.Error("unexpected error", err)
}
}(&waitForWrite)
var wg sync.WaitGroup
logOutput(ioReader, 1, &wg)
wg.Wait()
got := stdErr.String()
if got != want {
t.Fatalf("unexpected output:\n\twanted: %q\n\tgot: %q", want, got)
}
waitForWrite.Wait()
})
}