forked from rsms/gotalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_test.go
164 lines (143 loc) · 4.47 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
package gotalk
import (
"testing"
"bytes"
"runtime/debug"
)
func recoverAsFail(t *testing.T) {
if v := recover(); v != nil {
t.Log(v)
t.Log(string(debug.Stack()))
t.Fail()
}
}
func checkReqHandler(t *testing.T, s *Sock, h *Handlers, name, input, expectedOutput string) {
if hv := h.FindRequestHandler(name); hv == nil {
t.Errorf("handler '%s' not found", name)
} else if a, ok := hv.(BufferReqHandler); ok == false {
t.Errorf("handler '%s' is not a BufferReqHandler", 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 TestRequestFuncHandlers(t *testing.T) {
h := NewHandlers()
// 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.HandleRequest("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.HandleRequest("b", func(s *Sock, p int) (int, error) {
invocationCount++
return p+1, nil
})
h.HandleRequest("c", func(p int) (int, error) {
invocationCount++
return p+1, nil
})
h.HandleRequest("d", func(s *Sock) (int, error) {
invocationCount++
return 1, nil
})
h.HandleRequest("e", func() (int, error) {
invocationCount++
return 1, nil
})
h.HandleRequest("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.HandleRequest("g", func(s *Sock, p int) error {
invocationCount++
return nil
})
h.HandleRequest("h", func(p int) error {
invocationCount++
return nil
})
h.HandleRequest("i", func(s *Sock) error {
invocationCount++
return nil
})
h.HandleRequest("j", func() error {
invocationCount++
return nil
})
h.HandleRequest("", 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)
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 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))
}
}
func TestNotificationFuncHandlers(t *testing.T) {
h := NewHandlers()
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")
}
}