-
Notifications
You must be signed in to change notification settings - Fork 42
/
chan_test.go
158 lines (132 loc) · 3.05 KB
/
chan_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package cmds
import (
"context"
"errors"
"fmt"
"io"
"sync"
"testing"
)
func TestChanResponsePair(t *testing.T) {
type testcase struct {
values []interface{}
closeErr error
}
mkTest := func(tc testcase) func(*testing.T) {
return func(t *testing.T) {
cmd := &Command{}
req, err := NewRequest(context.TODO(), nil, nil, nil, nil, cmd)
if err != nil {
t.Fatal("error building request", err)
}
re, res := NewChanResponsePair(req)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for _, v := range tc.values {
v2, err := res.Next()
if err != nil {
t.Error("Next returned unexpected error:", err)
}
if v != v2 {
t.Errorf("Next returned unexpected value %q, expected %q", v2, v)
}
}
_, err := res.Next()
if tc.closeErr == nil || tc.closeErr == io.EOF {
if err == nil {
t.Error("Next returned nil error, expecting io.EOF")
} else if err != io.EOF {
t.Errorf("Next returned error %q, expecting io.EOF", err)
}
} else {
if err != tc.closeErr {
t.Errorf("Next returned error %q, expecting %q", err, tc.closeErr)
}
}
wg.Done()
}()
for _, v := range tc.values {
err := re.Emit(v)
if err != nil {
t.Error("Emit returned unexpected error:", err)
}
}
re.CloseWithError(tc.closeErr)
wg.Wait()
}
}
tcs := []testcase{
{values: []interface{}{1, 2, 3}},
{values: []interface{}{1, 2, 3}, closeErr: io.EOF},
{values: []interface{}{1, 2, 3}, closeErr: errors.New("an error occured")},
}
for i, tc := range tcs {
t.Run(fmt.Sprint(i), mkTest(tc))
}
}
func TestSingle1(t *testing.T) {
cmd := &Command{}
req, err := NewRequest(context.TODO(), nil, nil, nil, nil, cmd)
if err != nil {
t.Fatal("error building request", err)
}
re, res := NewChanResponsePair(req)
wait := make(chan struct{})
go func() {
re.Emit(Single{42})
err := re.Close()
if err != ErrClosingClosedEmitter {
t.Errorf("expected double close error, got %v", err)
}
close(wait)
}()
v, err := res.Next()
if err != nil {
t.Fatal(err)
}
if v != 42 {
t.Fatal("expected 42, got", v)
}
_, err = res.Next()
if err != io.EOF {
t.Fatal("expected EOF, got", err)
}
<-wait
}
func TestSingle2(t *testing.T) {
cmd := &Command{}
req, err := NewRequest(context.TODO(), nil, nil, nil, nil, cmd)
if err != nil {
t.Fatal("error building request", err)
}
re, res := NewChanResponsePair(req)
re.Close()
go func() {
err := re.Emit(Single{42})
if err != ErrClosedEmitter {
t.Error("expected closed emitter error, got", err)
return
}
}()
_, err = res.Next()
if err != io.EOF {
t.Fatal("expected EOF, got", err)
}
}
func TestDoubleClose(t *testing.T) {
cmd := &Command{}
req, err := NewRequest(context.TODO(), nil, nil, nil, nil, cmd)
if err != nil {
t.Fatal("error building request", err)
}
re, _ := NewChanResponsePair(req)
err = re.Close()
if err != nil {
t.Fatal("unexpected error closing re:", err)
}
err = re.Close()
if err != ErrClosingClosedEmitter {
t.Fatal("expected closed emitter error, got", err)
}
}