-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdl_test.go
214 lines (182 loc) · 3.61 KB
/
sdl_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package sdlspec
import (
"testing"
"time"
)
type HI struct {
n int
}
type HO struct {
n int
}
func TestCaseAndDefault(t *testing.T) {
testVar := 0
die := make(chan Signal)
helloProcess := MakeProcess(func(p *Process) {
start := State(p, "start", func(s Signal) {
switch s.(type) {
case HI:
testVar = 1
default:
testVar = 2
}
})
go start()
}, "hello", die)
helloProcess <- HI{}
time.Sleep(100 * time.Millisecond)
if testVar != 1 {
t.Error("case HI not executed")
}
helloProcess <- HO{}
time.Sleep(100 * time.Millisecond)
if testVar != 2 {
t.Error("case default not executed")
}
// EndProcesses()
close(die)
}
func TestChangingState(t *testing.T) {
testVar := 0
die := make(chan Signal)
SetBufferSize(50)
helloProcess := MakeProcess(func(p *Process) {
var next func()
start := State(p, "start", func(s Signal) {
switch s.(type) {
case HI:
defer next()
return
default:
}
})
next = State(p, "next", func(s Signal) {
switch s.(type) {
case HI:
testVar++
default:
}
})
go start()
}, "hello1", die)
SendSignalsWithDelay(helloProcess, []Signal{
HI{}, HI{}, HI{},
}, 10)
// Done should be encapsulated into the process
//EndProcesses()
time.Sleep(500 * time.Millisecond)
if testVar != 2 {
t.Error("case HI at state next not executed")
}
close(die)
}
func TestSaveSignal(t *testing.T) {
testVar := 0
die := make(chan Signal)
SetBufferSize(50)
helloProcess := MakeProcess(func(p *Process) {
var next func()
start := State(p, "start", func(s Signal) {
switch s.(type) {
case HI:
defer next()
return
case HO:
save(p, s)
default:
}
})
next = State(p, "next", func(s Signal) {
switch s.(type) {
case HO:
testVar++
default:
}
})
go start()
}, "hello1", die)
SendSignalsWithDelay(helloProcess, []Signal{
HO{1}, HO{2}, HO{3}, HI{4}, HI{5},
}, 10)
// Done should be encapsulated into the process
//EndProcesses()
time.Sleep(500 * time.Millisecond)
if testVar != 3 {
t.Error("signal HO not saved at state start")
}
close(die)
}
func TestSaveThenIgnoreSignal(t *testing.T) {
testVar := 0
die := make(chan Signal)
SetBufferSize(50)
helloProcess := MakeProcess(func(p *Process) {
var next1, next2 func()
start := State(p, "start", func(s Signal) {
switch s.(type) {
case HI:
defer next1()
return
case HO:
save(p, s)
default:
}
})
next1 = State(p, "next1", func(s Signal) {
switch s.(type) {
case HI:
defer next2()
return
default:
}
})
next2 = State(p, "next2", func(s Signal) {
switch s.(type) {
case HO:
testVar++
case HI:
default:
}
})
go start()
}, "hello2", die)
SendSignalsWithDelay(helloProcess, []Signal{
HO{1}, HO{2}, HO{3}, HI{4}, HI{5}, HI{6},
}, 10)
// Done should be encapsulated into the process
//EndProcesses()
time.Sleep(500 * time.Millisecond)
if testVar != 0 {
t.Error("signal HO not forgotten at state next1")
}
close(die)
}
func TestChannelConsumer(t *testing.T) {
testVar := 0
die := make(chan Signal)
out := MakeBuffer()
helloStates1 := func(p *Process) {
start := State(p, "start", func(s Signal) {
switch s.(type) {
case HI:
out <- HO{}
testVar = -3
default:
}
})
go start()
}
helloProcess := MakeProcess(helloStates1, "hello3", die)
go ChannelConsumer(die, "OUT", out)
time.Sleep(1000 * time.Millisecond)
SendSignalsWithDelay(helloProcess, []Signal{
HI{}, HI{}, HI{},
}, 10)
time.Sleep(500 * time.Millisecond)
//EndProcesses()
time.Sleep(500 * time.Millisecond)
if testVar != -3 {
t.Error("case HI not executed 3")
}
close(die)
}