forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responseemitter_test.go
113 lines (94 loc) · 1.77 KB
/
responseemitter_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
package cmds
import (
"context"
"io"
"testing"
"github.com/ipfs/go-ipfs-cmdkit"
)
func TestCopy(t *testing.T) {
req, err := NewRequest(context.Background(), nil, nil, nil, nil, &Command{})
if err != nil {
t.Fatal(err)
}
re1, res1 := NewChanResponsePair(req)
re2, res2 := NewChanResponsePair(req)
go func() {
err := Copy(re2, res1)
if err != nil {
t.Fatal(err)
}
}()
go func() {
err := re1.Emit("test")
if err != nil {
t.Fatal(err)
}
err = re1.Close()
if err != nil {
t.Fatal(err)
}
}()
v, err := res2.Next()
if err != nil {
t.Fatal(err)
}
str := v.(string)
if str != "test" {
t.Fatalf("expected string %#v but got %#v", "test", str)
}
_, err = res2.Next()
if err != io.EOF {
t.Fatalf("expected EOF but got err=%v", err)
}
}
func TestError(t *testing.T) {
req, err := NewRequest(context.Background(), nil, nil, nil, nil, &Command{})
if err != nil {
t.Fatal(err)
}
re, res := NewChanResponsePair(req)
go func() {
err := re.Emit("value1")
if err != nil {
t.Fatal(err)
}
err = re.Emit(cmdkit.Error{Message: "foo"})
if err != nil {
t.Fatal(err)
}
err = re.Emit(&cmdkit.Error{Message: "bar"})
if err != nil {
t.Fatal(err)
}
err = re.Emit("value2")
if err != nil {
t.Fatal(err)
}
err = re.Close()
if err != nil {
t.Fatal(err)
}
}()
v, err := res.Next()
if err != nil {
t.Fatal(err)
}
if v.(string) != "value1" {
t.Errorf("expected string %#v but got %#v", "value1", v)
}
v, err = res.Next()
if err == nil {
t.Errorf("expected error, got %#v", v)
}
v, err = res.Next()
if err == nil {
t.Errorf("expected error, got %#v", v)
}
v, err = res.Next()
if err != nil {
t.Error(err)
}
if v.(string) != "value2" {
t.Errorf("expected string %#v but got %#v", "value1", v)
}
}