-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactor_test.go
More file actions
72 lines (58 loc) · 1.28 KB
/
actor_test.go
File metadata and controls
72 lines (58 loc) · 1.28 KB
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 simple_actor
import (
"sync"
"testing"
"time"
)
func TestActor(t *testing.T) {
const (
Add Event = iota
Multiply
)
start := 0
lock := sync.RWMutex{}
a := New()
a.Register(Add, func(args ...Arg) {
x := args[0].(*int)
inc := args[1].(int)
lock.Lock()
*x += inc
lock.Unlock()
})
a.Register(Multiply, func(args ...Arg) {
x := args[0].(*int)
mul := args[1].(int)
lock.Lock()
*x *= mul
lock.Unlock()
})
a.Cast(Add, &start, 1)
a.Cast(Multiply, &start, 3)
if err := a.(*actor).waitForEmptyChan(time.Second * 5); err != nil {
t.Errorf("failed to wait for channel drain: %v", err)
}
lock.RLock()
if start != 3 {
t.Errorf("start should be %d", 3)
}
lock.RUnlock()
if err := a.Close(); err != nil {
t.Errorf("failed to close actor: %v", err)
}
}
func TestActor_Error(t *testing.T) {
a := New()
defer a.Close()
if err := a.Cast(0); err == nil {
t.Error("casting an unregistered event should fail")
}
if err := a.Register(0, nil); err == nil {
t.Error("register an event with nil handler should fail")
}
if err := a.Register(0, func(args ...Arg) {}); err != nil {
t.Errorf("register a valid event failed: %v", err)
}
if err := a.Register(0, func(args ...Arg) {}); err == nil {
t.Errorf("re-register a valid event should fail")
}
}