-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmethod_runner_test.go
200 lines (181 loc) · 4.33 KB
/
method_runner_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
package gogadgets_test
import (
"time"
"github.com/cswank/gogadgets"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("method runner", func() {
var (
m *gogadgets.MethodRunner
in, out chan gogadgets.Message
)
BeforeEach(func() {
in = make(chan gogadgets.Message)
out = make(chan gogadgets.Message)
m = &gogadgets.MethodRunner{}
})
Context("running methods", func() {
It("runs a method with a user wait command", func() {
go m.Start(out, in)
msg := gogadgets.Message{
Type: gogadgets.METHOD,
Method: gogadgets.Method{
Steps: []string{
"turn on lab led",
"wait for 0.1 seconds",
"turn off lab led",
"wait for user to turn off power",
"shutdown",
},
},
}
out <- msg
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(0))
msg = <-in
Expect(msg.Type).To(Equal("command"))
Expect(msg.Body).To(Equal("turn on lab led"))
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(1))
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(1))
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(1))
<-in
msg = <-in
Expect(msg.Type).To(Equal("command"))
Expect(msg.Body).To(Equal("turn off lab led"))
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(3))
out <- gogadgets.Message{
Type: "update",
Body: "wait for user to turn off power",
}
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(4))
msg = <-in
Expect(msg.Type).To(Equal("command"))
Expect(msg.Body).To(Equal("shutdown"))
})
It("runs a method with a wait command", func() {
go m.Start(out, in)
msg := gogadgets.Message{
Type: gogadgets.METHOD,
Method: gogadgets.Method{
Steps: []string{
"wait for sun temperature <= 100 C",
"shutdown",
},
},
}
out <- msg
msg = <-in
out <- gogadgets.Message{
Sender: "sum temperature",
Value: gogadgets.Value{
Value: 99.9,
Units: "C",
},
}
var x bool
select {
case <-in:
x = false
case <-time.After(100 * time.Millisecond):
x = true
}
Expect(x).To(BeTrue())
out <- gogadgets.Message{
Sender: "sun temperature",
Type: "update",
Value: gogadgets.Value{
Value: 100.0,
Units: "C",
},
}
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(1))
msg = <-in
Expect(msg.Type).To(Equal("command"))
Expect(msg.Body).To(Equal("shutdown"))
})
It("resumes a method with a wait for time command", func() {
go m.Start(out, in)
msg := gogadgets.Message{
Type: gogadgets.METHOD,
Method: gogadgets.Method{
Step: 1,
Time: 30,
Steps: []string{
"turn on sun",
"wait for 60 seconds",
"shutdown",
},
},
}
out <- msg
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(1))
Expect(msg.Method.Time).To(Equal(30))
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(1))
Expect(msg.Method.Time).To(Equal(29))
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(1))
Expect(msg.Method.Time).To(Equal(28))
})
It("resumes a method with a wait for user command", func() {
go m.Start(out, in)
msg := gogadgets.Message{
Type: gogadgets.METHOD,
Method: gogadgets.Method{
Step: 1,
Steps: []string{
"turn on sun",
"wait for user to say ok",
"turn off sun",
"shutdown",
},
},
}
out <- msg
out <- gogadgets.Message{
Type: "update",
Body: "wait for user to say ok",
}
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(2))
})
})
It("resumes a method", func() {
go m.Start(out, in)
msg := gogadgets.Message{
Type: gogadgets.METHOD,
Method: gogadgets.Method{
Step: 1,
Steps: []string{
"turn on sun",
"turn off sun",
"turn on sun",
"shutdown",
},
},
}
out <- msg
msg = <-in
Expect(msg.Type).To(Equal("method update"))
Expect(msg.Method.Step).To(Equal(2))
})
})