Repository navigation
Expand file tree
/
Copy pathcontext_test.go
More file actions
317 lines (296 loc) · 8.48 KB
/
Copy pathcontext_test.go
File metadata and controls
317 lines (296 loc) · 8.48 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package context
import (
"errors"
"fmt"
"github.com/root-gg/utils"
"testing"
"time"
)
func TestMain(t *testing.T) {
root := NewContext("ROOT")
child := root.Fork("fork1")
child.Fork("fork2")
if child.Status() != Running {
t.Errorf("Invalid child status %s instead of %s", child.Status(), Running)
}
child.Finalize(nil)
if child.Status() != Success {
t.Errorf("Invalid child status %s instead of %s", child.Status(), Success)
}
children := root.AllChildren()
if len(children) != 2 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 2)
}
}
func TestDefaultName(t *testing.T) {
root := NewContext("")
defaultName := "TestDefaultName"
if root.Name() != defaultName {
t.Errorf("Invalid child default name %s instead of %s", root.Name, defaultName)
}
child := root.Fork("")
if child.Name() != defaultName {
t.Errorf("Invalid child default name %s instead of %s", child.Name, defaultName)
}
}
func TestDates(t *testing.T) {
root := NewContext("ROOT")
fmt.Printf("StartDate : %s\n", root.StartDate().String())
fmt.Printf("Running since : %s\n", root.Elapsed().String())
if root.EndDate() != nil {
t.Error("EndDate on running context")
}
root.Finalize(Success)
fmt.Printf("EndDate : %s\n", root.StartDate().String())
fmt.Printf("Has run : %s\n", root.Elapsed().String())
}
func TestTimers(t *testing.T) {
root := NewContext("ROOT")
root.Time("t1").Stop()
root.Time("t2")
timers := root.Timers()
if len(timers) != 2 {
t.Errorf("Invalid timer count %d instead of %d", len(root.Timers()), 2)
}
if timers[0].Status() != utils.Stopped {
t.Errorf("Invalid timer %s status %s instead of %s", timers[0].Name(), timers[0].Status(), utils.Stopped)
}
if timers[1].Status() != utils.Running {
t.Errorf("Invalid timer %s status %s instead of %s", timers[1].Name(), timers[1].Status(), utils.Running)
}
}
func TestFinalize(t *testing.T) {
root := NewContext("ROOT")
child := root.Fork("fork1")
go func() { child.Finalize(Success) }()
child.Wait()
if child.Status() != Success {
t.Errorf("Invalid child status %s instead of %s", child.Status(), Success)
}
}
func TestWaitAllChildren(t *testing.T) {
root := NewContext("ROOT")
child1 := root.Fork("fork1")
child2 := child1.Fork("fork2")
child3 := child2.Fork("fork3")
go func() {
time.Sleep(100 * time.Millisecond)
child1.Finalize(Success)
time.Sleep(100 * time.Millisecond)
child3.Finalize(Success)
time.Sleep(100 * time.Millisecond)
child2.Finalize(Success)
}()
root.WaitAllChildren()
children := root.AllChildren()
if len(children) != 3 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 3)
}
for _, child := range children {
if child.Status() != Success {
t.Errorf("Invalid child status %s instead of %s", child.Status(), Timedout)
}
}
}
func TestStatusOverride(t *testing.T) {
root := NewContext("ROOT")
child := root.Fork("fork1")
var err = errors.New("error")
go func() { child.Finalize(err) }()
child.Wait()
child.Finalize(Success)
if child.Status() != err {
t.Errorf("Invalid child status %s instead of %s", child.Status(), err)
}
}
func TestTimeoutOk(t *testing.T) {
root := NewContext("ROOT")
child := root.ForkWithTimeout("", 200*time.Millisecond)
go func() {
time.Sleep(100 * time.Millisecond)
child.Finalize(Success)
}()
<-child.Done()
if child.Status() != Success {
t.Errorf("Invalid child status %s instead of %s", child.Status(), Success)
}
}
func TestTimeoutKo(t *testing.T) {
root := NewContext("ROOT")
child := root.ForkWithTimeout("", 100*time.Millisecond)
go func() {
time.Sleep(200 * time.Millisecond)
child.Finalize(Success)
}()
<-child.Done()
if child.Status() != Timedout {
t.Errorf("Invalid child status %s instead of %s", child.Status(), Timedout)
}
}
func TestTimeoutDates(t *testing.T) {
root := NewContextWithTimeout("", 100*time.Millisecond)
fmt.Printf("Deadline is : %s\n", root.Deadline().String())
fmt.Printf("Remaining time : %s\n", root.Remaining().String())
child := root.Fork("")
if child.Deadline() != *child.StartDate() {
t.Errorf("Invalid deadline for non timed context : %s\n", child.Deadline().String())
}
if child.Remaining().Seconds() > 0 {
t.Errorf("Invalid remaining for non timed context : %s\n", child.Remaining().String())
}
}
func TestCancel(t *testing.T) {
root := NewContext("ROOT")
root.Fork("").Fork("").Fork("")
root.Cancel()
children := root.AllChildren()
if len(children) != 3 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 3)
}
for _, child := range children {
if child.Status() != Canceled {
t.Errorf("Invalid child status %s instead of %s", child.Status(), Timedout)
}
}
}
func TestAutoCancel(t *testing.T) {
root := NewContext("ROOT")
child := root.Fork("fork1").AutoCancel()
child.Fork("").Fork("").Fork("")
child.Finalize(Success)
time.Sleep(100 * time.Millisecond)
children := child.AllChildren()
if len(children) != 3 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 3)
}
for _, child := range children {
if child.Status() != Canceled {
t.Errorf("Invalid child status %s instead of %s", child.Status(), Canceled)
}
}
}
func TestDetach(t *testing.T) {
root := NewContext("ROOT")
child := root.Fork("")
child.Fork("").Fork("")
children := root.AllChildren()
if len(children) != 3 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 3)
}
root.DetachChild(child)
children = root.AllChildren()
if len(children) != 0 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 1)
}
}
func TestAutoDetach(t *testing.T) {
root := NewContext("ROOT")
child := root.Fork("fork1").AutoDetach()
children := root.AllChildren()
if len(children) != 1 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 1)
}
child.Finalize(Success)
time.Sleep(100 * time.Millisecond)
children = root.AllChildren()
if len(children) != 0 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 0)
}
}
func TestDetachChild(t *testing.T) {
root := NewContext("ROOT")
child := root.Fork("fork1")
children := root.AllChildren()
if len(children) != 1 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 1)
}
root.DetachChild(child)
children = root.AllChildren()
if len(children) != 0 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 0)
}
}
func TestAutoDetachChild(t *testing.T) {
root := NewContext("ROOT")
child := root.Fork("fork1")
root.AutoDetachChild(child)
children := root.AllChildren()
if len(children) != 1 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 1)
}
child.Finalize(Success)
time.Sleep(100 * time.Millisecond)
children = root.AllChildren()
if len(children) != 0 {
t.Errorf("Invalid childen count %d instead of %d", len(children), 0)
}
}
func TestValue(t *testing.T) {
root := NewContext("ROOT")
root.Set("foo", "bar")
value, ok := root.Get("foo")
if !ok {
t.Error("Missing value for key \"foo\"")
}
if value.(string) != "bar" {
t.Error("Invalid value \"%s\" for key \"foo\" sould be \"bar\"", value)
}
child := root.Fork("fork1")
value, ok = child.Get("foo")
if !ok {
t.Error("Missing value for key \"foo\" in child context")
}
if value.(string) != "bar" {
t.Error("Invalid value \"%s\" for key \"foo\" child context sould be \"bar\"", value)
}
}
func TestMissingValue(t *testing.T) {
root := NewContext("ROOT")
root.Set("go", "lang")
child := root.Fork("scala")
child.Set("sca", "la")
child2 := child.Fork("java")
child2.Get("ja")
value, ok := child.Get("foo")
if ok {
t.Error("Missing key \"ja\" should be missing")
}
if value != nil {
t.Error("Missing value \"%s\" for key \"foo\" should be missing", value)
}
}
func TestValueOverride(t *testing.T) {
root := NewContext("ROOT")
root.Set("foo", "bar")
child := root.Fork("")
child.Set("foo", "baz")
value, ok := root.Get("foo")
if !ok {
t.Error("Missing value for key foo")
}
if value.(string) != "bar" {
t.Error("Invalid value \"%s\" for key foo sould be \"bar\"", value)
}
value, ok = child.Get("foo")
if !ok {
t.Error("Missing value for key foo in child context")
}
if value.(string) != "baz" {
t.Error("Invalid value \"%s\" for key foo child context sould be \"baz\"", value)
}
}
func TestDisplay(t *testing.T) {
root := NewContext("ROOT")
fork1 := root.Fork("fork1")
fork1.Fork("fork11")
fork1.Fork("fork12").Fork("fork121")
fork1.Finalize(Success)
fork1.Cancel()
fork2 := root.Fork("fork2")
fork2.Fork("fork21")
fork2.Fork("fork22").Fork("fork221")
fork2.Time("t1").Stop()
fork2.Time("t2").Stop()
fork2.Time("t3")
fmt.Println(root.String())
}