-
Notifications
You must be signed in to change notification settings - Fork 22
/
actor_test.go
133 lines (114 loc) · 2.83 KB
/
actor_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
package fpgo
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestActorCommon(t *testing.T) {
var expectedInt int
var actual int
var resultChannel chan interface{}
// Test difference channel size cases
for channelSize := 0; channelSize < 5; channelSize++ {
actual = 0
expectedInt = 1400
// Channel for results
resultChannel = make(chan interface{}, channelSize+1)
// Message CMDs
cmdSpawn := "spawn"
cmdShutdown := "shutdown"
// Testee
actorRoot := Actor.New(func(self *ActorDef[interface{}], input interface{}) {
// SPAWN: for ROOT
if input == cmdSpawn {
self.Spawn(func(self *ActorDef[interface{}], input interface{}) {
// SHUTDOWN: for Children
if input == cmdShutdown {
self.Close()
return
}
// INT cases: Children
val, _ := Maybe.Just(input).ToInt()
resultChannel <- val * 10
})
return
}
// SHUTDOWN: for ROOT
if input == cmdShutdown {
for _, child := range self.children {
child.Send(cmdShutdown)
}
self.Close()
close(resultChannel)
return
}
// INT cases: ROOT
intVal, _ := Maybe.Just(input).ToInt()
if intVal > 0 {
for _, child := range self.children {
child.Send(intVal)
}
}
})
// Sequential Send messages(async)
go func() {
actorRoot.Send(cmdSpawn)
actorRoot.Send(10)
actorRoot.Send(cmdSpawn)
actorRoot.Send(20)
actorRoot.Send(cmdSpawn)
actorRoot.Send(30)
}()
i := 0
for val := range resultChannel {
intVal, _ := Maybe.Just(val).ToInt()
actual += intVal
i++
if i == 5 {
go actorRoot.Send(cmdShutdown)
}
}
assert.Equal(t, expectedInt, actual)
}
}
func TestActorAsk(t *testing.T) {
var expectedInt int
var actual int
var err error
// Testee
actorRoot := Actor.New(func(self *ActorDef[interface{}], input interface{}) {
// Ask cases: ROOT
switch val := input.(type) {
case *AskDef[interface{}, int]:
intVal, _ := Maybe.Just(val.Message).ToInt()
// NOTE If negative, hanging for testing Ask.timeout
if intVal < 0 {
break
}
val.Reply(intVal * 10)
break
}
})
// var timer *time.Timer
var timeout time.Duration
timeout = 10 * time.Millisecond
// Normal cases
expectedInt = 10
actual = AskNewGenerics[interface{}, int](1).AskOnce(actorRoot)
assert.Equal(t, expectedInt, actual)
// Ask with Timeout
expectedInt = 20
actual, _ = AskNewGenerics[interface{}, int](2).AskOnceWithTimeout(actorRoot, timeout)
assert.Equal(t, expectedInt, actual)
// Ask channel
expectedInt = 30
ch := AskNewGenerics[interface{}, int](3).AskChannel(actorRoot)
actual = <-ch
close(ch)
assert.Equal(t, expectedInt, actual)
// Timeout cases
expectedInt = 0
actual, err = AskNewGenerics[interface{}, int](-1).AskOnceWithTimeout(actorRoot, timeout)
assert.Equal(t, expectedInt, actual)
assert.Equal(t, ErrActorAskTimeout, err)
}