forked from asticode/go-astilectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher_test.go
57 lines (52 loc) · 1.12 KB
/
dispatcher_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
package astilectron
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDispatcher(t *testing.T) {
// Init
var d = newDispatcher()
var wg = sync.WaitGroup{}
var dispatched = []int{}
var m sync.Mutex
// Test adding listener
d.addListener("1", "1", func(e Event) (deleteListener bool) {
m.Lock()
dispatched = append(dispatched, 1)
m.Unlock()
wg.Done()
return
})
d.addListener("1", "1", func(e Event) (deleteListener bool) {
m.Lock()
dispatched = append(dispatched, 2)
m.Unlock()
wg.Done()
return true
})
d.addListener("1", "1", func(e Event) (deleteListener bool) {
m.Lock()
dispatched = append(dispatched, 3)
m.Unlock()
wg.Done()
return true
})
d.addListener("1", "2", func(e Event) (deleteListener bool) {
m.Lock()
dispatched = append(dispatched, 4)
m.Unlock()
wg.Done()
return
})
assert.Len(t, d.l["1"]["1"], 3)
// Test dispatch
wg.Add(4)
d.dispatch(Event{Name: "2", TargetID: "1"})
d.dispatch(Event{Name: "1", TargetID: "1"})
wg.Wait()
for _, v := range []int{1, 2, 3, 4} {
assert.Contains(t, dispatched, v)
}
assert.Len(t, d.listeners("1", "1"), 1)
}