-
Notifications
You must be signed in to change notification settings - Fork 42
/
single_test.go
108 lines (86 loc) · 1.94 KB
/
single_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
package cmds
import (
"context"
"io"
"sync"
"testing"
)
func TestSingleChan(t *testing.T) {
req, err := NewRequest(context.Background(), nil, nil, nil, nil, &Command{})
if err != nil {
t.Fatal(err)
}
re, res := NewChanResponsePair(req)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if err := EmitOnce(re, "test"); err != nil {
t.Error(err)
return
}
err := re.Emit("test")
if err != ErrClosedEmitter {
t.Errorf("expected emit error %q, got: %v", ErrClosedEmitter, err)
}
err = re.Close()
if err != ErrClosingClosedEmitter {
t.Errorf("expected close error %q, got: %v", ErrClosingClosedEmitter, err)
}
}()
v, err := res.Next()
if err != nil {
t.Fatal(err)
}
if str, ok := v.(string); !ok || str != "test" {
t.Fatalf("expected %#v, got %#v", "foo", str)
}
if _, err = res.Next(); err != io.EOF {
t.Fatalf("expected %#v, got %#v", io.EOF, err)
}
wg.Wait()
}
func TestSingleWriter(t *testing.T) {
req, err := NewRequest(context.Background(), nil, nil, nil, nil, &Command{})
if err != nil {
t.Fatal(err)
}
pr, pw := io.Pipe()
re, err := NewWriterResponseEmitter(pw, req)
if err != nil {
t.Fatal(err)
}
res, err := NewReaderResponse(pr, req)
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
if err := EmitOnce(re, "test"); err != nil {
t.Error(err)
return
}
err := re.Emit("this should not be sent")
if err != ErrClosedEmitter {
t.Errorf("expected emit error %q, got: %v", ErrClosedEmitter, err)
}
err = re.Close()
if err != ErrClosingClosedEmitter {
t.Errorf("expected close error %q, got: %v", ErrClosingClosedEmitter, err)
}
wg.Done()
}()
v, err := res.Next()
if err != nil {
t.Fatal(err)
}
if str, ok := v.(string); !ok || str != "test" {
t.Fatalf("expected %#v, got %#v", "foo", str)
}
if v, err = res.Next(); err != io.EOF {
t.Log(v, err)
t.Fatalf("expected %#v, got %#v", io.EOF, err)
}
wg.Wait()
}