/
meta_test.go
64 lines (48 loc) · 904 Bytes
/
meta_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
package meta
import (
"testing"
"time"
)
type A struct {
done Signal
}
type B struct{}
func (B) sum(a, b int) int {
return a + b
}
type twoNumbers struct {
a, b int
}
func TestConnect(t *testing.T) {
var a A
var b B
pipe := make(chan int)
Connect(&a.done, func(call *Call) {
if nums, ok := call.Data.(twoNumbers); ok {
pipe <- b.sum(nums.a, nums.b)
} else {
t.Fatal("transferred data is rubbish")
}
})
go func() {
<-time.After(10 * time.Millisecond)
a.done.Emit(twoNumbers{40, 2})
}()
x := <-pipe
if x != 42 {
t.Fatal("the whole thing doesn't really work")
return
}
}
func TestDisconnect(t *testing.T) {
var a A
cn1 := Connect(&a.done, func(*Call) {
t.Fatal("slot 1 got executed somehow")
})
cn2 := Connect(&a.done, func(*Call) {
t.Fatal("slot 2 got executed somehow")
})
Disconnect(&a.done, cn1)
Disconnect(&a.done, cn2)
a.done.Emit(nil)
}