-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashen_test.go
175 lines (152 loc) · 3.51 KB
/
dashen_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
package dashen_test
import (
"testing"
"time"
"github.com/minodisk/dashen"
)
func TestListen(t *testing.T) {
d := dashen.New()
errCh1 := make(chan error, 1)
go func(d *dashen.Dashen) {
if err := d.Listen(); err != nil {
errCh1 <- err
}
}(d)
errCh2 := make(chan error, 1)
go func(d *dashen.Dashen) {
time.Sleep(time.Second)
if err := d.Listen(); err != nil {
errCh2 <- err
}
}(d)
select {
case err := <-errCh1:
t.Fatal("1st time Listen shouldn't return error:", err)
case err := <-errCh2:
if err != dashen.AlreadyListening {
t.Errorf("got %v, want %v", err, dashen.AlreadyListening)
}
}
}
func TestClose(t *testing.T) {
t.Run("close unlistening", func(t *testing.T) {
d := dashen.New()
errCh := make(chan error, 1)
go func(d *dashen.Dashen) {
time.Sleep(time.Second * 3)
if err := d.Close(); err != nil {
errCh <- err
return
}
}(d)
if err := <-errCh; err == nil {
t.Error("shouldn't be able to close before listening")
}
})
t.Run("close listening", func(t *testing.T) {
d := dashen.New()
errCh := make(chan error, 1)
doneCh := make(chan struct{})
go func(d *dashen.Dashen) {
if err := d.Listen(); err != nil {
errCh <- err
return
}
doneCh <- struct{}{}
}(d)
go func(d *dashen.Dashen) {
time.Sleep(time.Second * 3)
if err := d.Close(); err != nil {
errCh <- err
return
}
doneCh <- struct{}{}
}(d)
select {
case err := <-errCh:
t.Errorf("should be able to close after listening: %v", err)
case <-doneCh:
}
})
}
func cb1() {}
func cb2() {}
func TestSubscribe(t *testing.T) {
mac1 := "11:11:11:11:11:11"
mac2 := "22:22:22:22:22:22"
d := dashen.New()
t.Run("can subscribe unique func", func(t *testing.T) {
d.Subscribe(mac1, cb1)
cbs, ok := d.MACCallbacksMap[mac1]
if !ok {
t.Errorf("not registered MAC: %s", mac1)
}
got := len(cbs)
want := 1
if got != want {
t.Errorf("number of callbacks: got %d, want %d", got, want)
}
})
t.Run("not subscribe same func", func(t *testing.T) {
d.Subscribe(mac1, cb1)
cbs, ok := d.MACCallbacksMap[mac1]
if !ok {
t.Errorf("not registered MAC: %s", mac1)
}
got := len(cbs)
want := 1
if got != want {
t.Errorf("number of callbacks: got %d, want %d", got, want)
}
})
t.Run("can subscribe different func", func(t *testing.T) {
d.Subscribe(mac1, cb2)
cbs, ok := d.MACCallbacksMap[mac1]
if !ok {
t.Errorf("not registered MAC: %s", mac1)
}
got := len(cbs)
want := 2
if got != want {
t.Errorf("number of callbacks: got %d, want %d", got, want)
}
})
t.Run("can subscribe different MAC", func(t *testing.T) {
d.Subscribe(mac2, cb1)
d.Subscribe(mac2, cb2)
cbs, ok := d.MACCallbacksMap[mac2]
if !ok {
t.Errorf("not registered MAC: %s", mac2)
}
got := len(cbs)
want := 2
if got != want {
t.Errorf("number of callbacks: got %d, want %d", got, want)
}
})
}
func TestUnsubscribe(t *testing.T) {
mac1 := "11:11:11:11:11:11"
mac2 := "22:22:22:22:22:22"
d := dashen.New()
d.Subscribe(mac1, cb1)
d.Subscribe(mac1, cb2)
d.Subscribe(mac2, cb1)
d.Subscribe(mac2, cb2)
t.Run("can't unsubscribe unknown MAC", func(t *testing.T) {
d.Unsubscribe("33:33:33:33:33:33", cb1)
got := len(d.MACCallbacksMap[mac1])
want := 2
if got != want {
t.Errorf("number of callbacks: got %d, want %d", got, want)
}
})
t.Run("can unsubscribe", func(t *testing.T) {
d.Unsubscribe(mac1, cb1)
got := len(d.MACCallbacksMap[mac1])
want := 1
if got != want {
t.Errorf("number of callbacks: got %d, want %d", got, want)
}
})
}