-
Notifications
You must be signed in to change notification settings - Fork 2
/
callback_test.go
72 lines (58 loc) · 1.65 KB
/
callback_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
package hnsquery
import (
"github.com/miekg/dns"
"testing"
)
func TestCallback(t *testing.T) {
c := newCGOHSK()
cb1 := CallbackFunc(func(rrs []dns.RR, err error) {})
cb2 := CallbackFunc(func(rrs []dns.RR, err error) {})
cb3 := CallbackFunc(func(rrs []dns.RR, err error) {})
// addCallback should return true
// for initial
initial := c.addCallback("test", &cb1)
if !initial {
t.Fatal("got initial = false, want true")
}
if cbs, ok := c.getCallbacks("test"); !ok || len(cbs) != 1 {
t.Fatal("want callbacks len = 1")
}
// remove only callback in list
c.removeCallback("test", &cb1)
if _, ok := c.getCallbacks("test"); ok {
t.Fatal("got callbacks, want none")
}
// add two callbacks to test2
c.addCallback("test2", &cb1)
if init2 := c.addCallback("test2", &cb2); init2 {
t.Fatal("an initial callback exists")
}
// single callback to test3
c.addCallback("test3", &cb3)
if cbs, ok := c.getCallbacks("test2"); !ok || len(cbs) != 2 {
t.Fatal("want callbacks len = 2")
}
// removing one callback shouldn't mess with the others
c.removeCallback("test2", &cb1)
cbs, ok := c.getCallbacks("test2")
if !ok || len(cbs) != 1 {
t.Fatal("want callbacks len = 1")
}
// the right callback should've been removed
if cbs[0] != &cb2 {
t.Fatal("removed incorrect callback")
}
// last callback for test2 should be removed
c.removeCallback("test2", &cb2)
if _, ok := c.getCallbacks("test2"); ok {
t.Fatal("want no callbacks")
}
// remove only callback for test3
c.removeCallback("test3", &cb2)
if _, ok := c.getCallbacks("test3"); ok {
t.Fatal("want no callbacks")
}
if len(c.callbacks) != 0 {
t.Fatal("want no callbacks in map")
}
}