-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
60 lines (56 loc) · 1 KB
/
main_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
53
54
55
56
57
58
59
60
package main
import (
"bytes"
"github.com/stretchr/testify/require"
"io"
"strings"
"testing"
"testing/iotest"
"time"
)
func Test_runLoop(t *testing.T) {
t.Parallel()
exitCmd := strings.NewReader("exit\n")
type args struct {
r io.Reader
}
tests := []struct {
name string
args args
wantW string
wantErrW string
}{
{
name: "no error",
args: args{
r: exitCmd,
},
},
{
name: "read error should have no effect",
args: args{
r: iotest.ErrReader(io.EOF),
},
wantErrW: "EOF",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
w := &bytes.Buffer{}
errW := &bytes.Buffer{}
exit := make(chan struct{}, 2)
// run the loop for 10ms
go runLoop(tt.args.r, w, errW, exit)
time.Sleep(10 * time.Millisecond)
exit <- struct{}{}
require.NotEmpty(t, w.String())
if tt.wantErrW != "" {
require.Contains(t, errW.String(), tt.wantErrW)
} else {
require.Empty(t, errW.String())
}
})
}
}