-
Notifications
You must be signed in to change notification settings - Fork 77
/
handle_test.go
183 lines (165 loc) · 4.79 KB
/
handle_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package gotalk
import (
"bytes"
"io"
"testing"
)
func checkReqHandler(t *testing.T, s *Sock, h *Handlers, name, input, expectedOutput string) {
if a := h.FindBufferRequestHandler(name); a == nil {
t.Errorf("handler '%s' not found", name)
} else if outbuf, err := a(s, name, []byte(input)); err != nil {
t.Errorf("handler '%s' returned an error: %s", name, err.Error())
} else if bytes.Equal(outbuf, []byte(expectedOutput)) == false {
t.Errorf("handler '%s' returned '%s', expected '%s'", name, string(outbuf), expectedOutput)
}
}
func checkNotHandler(t *testing.T, s *Sock, h *Handlers, name, input string) {
if a := h.FindNotificationHandler(name); h == nil {
t.Errorf("handler '%s' not found", name)
} else {
a(s, name, []byte(input))
}
}
// end of helpers
// ----------------------------------------------------------------------------------------
func TestHandlersBasics(t *testing.T) {
assertNotNil(t, NewHandlers()) // legacy function
}
func TestRequestFuncHandlers(t *testing.T) {
h := &Handlers{}
// Handlers.Handle panic() when the signature is incorrect, so treat panic as test failure
defer recoverAsFail(t)
invocationCount := 0
// All possible handler func permutations (with `int` value types)
h.Handle("a", func(s *Sock, op string, p int) (int, error) {
if op != "a" {
t.Errorf("expected op='a' but got '%s'", op)
}
invocationCount++
return p + 1, nil
})
h.Handle("b", func(s *Sock, p int) (int, error) {
invocationCount++
return p + 1, nil
})
h.Handle("c", func(p int) (int, error) {
invocationCount++
return p + 1, nil
})
h.Handle("d", func(s *Sock) (int, error) {
invocationCount++
return 1, nil
})
h.Handle("e", func() (int, error) {
invocationCount++
return 1, nil
})
h.Handle("f", func(s *Sock, op string, p int) error {
if op != "f" {
t.Errorf("expected op='f' but got '%s'", op)
}
invocationCount++
return nil
})
h.Handle("g", func(s *Sock, p int) error {
invocationCount++
return nil
})
h.Handle("h", func(p int) error {
invocationCount++
return nil
})
h.Handle("i", func(s *Sock) error {
invocationCount++
return nil
})
h.Handle("j", func() error {
invocationCount++
return nil
})
h.Handle("", func(s *Sock, op string, p int) error {
if op != "fallback1" && op != "fallback2" {
t.Errorf("expected op='fallback1'||'fallback2' but got '%s'", op)
}
invocationCount++
return nil
})
s := NewSock(h)
// name, input, expectedOutput
checkReqHandler(t, s, h, "a", "1", "2")
checkReqHandler(t, s, h, "b", "1", "2")
checkReqHandler(t, s, h, "c", "1", "2")
checkReqHandler(t, s, h, "d", "", "1")
checkReqHandler(t, s, h, "e", "", "1")
checkReqHandler(t, s, h, "f", "1", "")
checkReqHandler(t, s, h, "g", "1", "")
checkReqHandler(t, s, h, "h", "1", "")
checkReqHandler(t, s, h, "i", "", "")
checkReqHandler(t, s, h, "j", "", "")
checkReqHandler(t, s, h, "fallback1", "1", "")
checkReqHandler(t, s, h, "fallback2", "1", "")
if invocationCount != 12 {
t.Error("not all handlers were invoked")
}
}
func TestNotificationFuncHandlers(t *testing.T) {
h := &Handlers{}
defer recoverAsFail(t)
invocationCount := 0
// All possible handler func permutations (with `int` value types)
h.HandleNotification("a", func(s *Sock, name string, p int) {
if name != "a" {
t.Errorf("expected name='a' but got '%s'", name)
}
if p != 1 {
t.Errorf("expected p='1' but got '%v'", p)
}
invocationCount++
})
h.HandleNotification("b", func(name string, p int) {
if name != "b" {
t.Errorf("expected name='b' but got '%s'", name)
}
if p != 2 {
t.Errorf("expected p='2' but got '%v'", p)
}
invocationCount++
})
h.HandleNotification("c", func(p int) {
if p != 3 {
t.Errorf("expected p='3' but got '%v'", p)
}
invocationCount++
})
h.HandleNotification("", func(name string, p int) {
if name != "fallback1" && name != "fallback2" {
t.Errorf("expected name='fallback1'||'fallback2' but got '%s'", name)
}
if p != 4 {
t.Errorf("expected p='4' but got '%v'", p)
}
invocationCount++
})
s := NewSock(h)
checkNotHandler(t, s, h, "a", "1")
checkNotHandler(t, s, h, "b", "2")
checkNotHandler(t, s, h, "c", "3")
checkNotHandler(t, s, h, "fallback1", "4")
checkNotHandler(t, s, h, "fallback2", "4")
if invocationCount != 5 {
t.Error("not all handlers were invoked")
}
}
func TestPkgLevelHandlers(t *testing.T) {
// package-level handler functions are wrappers: F(...)=>DefaultHandlers.F(...)
Handle("a", func() error { return nil })
HandleBufferRequest("a", func(s *Sock, op string, payload []byte) ([]byte, error) {
return nil, nil
})
HandleStreamRequest("a", func(s *Sock, name string, rch chan []byte, out io.WriteCloser) error {
out.Close()
return nil
})
HandleNotification("a", func() {})
HandleBufferNotification("a", func(s *Sock, name string, payload []byte) {})
}