-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathjob_test.go
122 lines (107 loc) · 3.83 KB
/
job_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
package goflow
import (
"testing"
"github.com/philippgille/gokv/gomap"
)
func TestJob(t *testing.T) {
j := &Job{Name: "example", Schedule: "* * * * *"}
j.Add(&Task{
Name: "add-one-one",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo $((1 + 1))"}},
})
j.Add(&Task{
Name: "sleep-two",
Operator: Command{Cmd: "sleep", Args: []string{"2"}},
})
j.Add(&Task{
Name: "add-two-four",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo $((2 + 4))"}},
})
j.Add(&Task{
Name: "add-three-four",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo $((3 + 4))"}},
})
j.Add(&Task{
Name: "whoops-with-constant-delay",
Operator: Command{Cmd: "whoops", Args: []string{}},
Retries: 5,
RetryDelay: ConstantDelay{1},
})
j.Add(&Task{
Name: "whoops-with-exponential-backoff",
Operator: Command{Cmd: "whoops", Args: []string{}},
Retries: 1,
RetryDelay: ExponentialBackoff{},
})
j.Add(&Task{
Name: "totally-skippable",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo 'everything succeeded'"}},
TriggerRule: "allSuccessful",
})
j.Add(&Task{
Name: "clean-up",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo 'cleaning up now'"}},
TriggerRule: "allDone",
})
j.Add(&Task{
Name: "failure",
Operator: RandomFailure{1},
})
j.SetDownstream(j.Task("add-one-one"), j.Task("sleep-two"))
j.SetDownstream(j.Task("sleep-two"), j.Task("add-two-four"))
j.SetDownstream(j.Task("add-one-one"), j.Task("add-three-four"))
j.SetDownstream(j.Task("add-one-one"), j.Task("whoops-with-constant-delay"))
j.SetDownstream(j.Task("add-one-one"), j.Task("whoops-with-exponential-backoff"))
j.SetDownstream(j.Task("whoops-with-constant-delay"), j.Task("totally-skippable"))
j.SetDownstream(j.Task("whoops-with-exponential-backoff"), j.Task("totally-skippable"))
j.SetDownstream(j.Task("totally-skippable"), j.Task("clean-up"))
store := gomap.NewStore(gomap.DefaultOptions)
go j.run(store, j.newExecution())
for {
if j.allDone() {
break
}
}
if j.loadTaskState("add-one-one") != successful {
t.Errorf("Got status %v, expected %v", j.loadTaskState("add-one-one"), successful)
}
if j.loadTaskState("sleep-two") != successful {
t.Errorf("Got status %v, expected %v", j.loadTaskState("sleep-two"), successful)
}
if j.loadTaskState("add-two-four") != successful {
t.Errorf("Got status %v, expected %v", j.loadTaskState("add-two-four"), successful)
}
if j.loadTaskState("add-three-four") != successful {
t.Errorf("Got status %v, expected %v", j.loadTaskState("add-three-four"), successful)
}
if j.loadTaskState("whoops-with-constant-delay") != failed {
t.Errorf("Got status %v, expected %v", j.loadTaskState("whoops-with-constant-delay"), failed)
}
if j.loadTaskState("whoops-with-exponential-backoff") != failed {
t.Errorf("Got status %v, expected %v", j.loadTaskState("whoops-with-exponential-backoff"), failed)
}
if j.loadTaskState("totally-skippable") != skipped {
t.Errorf("Got status %v, expected %v", j.loadTaskState("totally-skippable"), skipped)
}
if j.loadTaskState("clean-up") != successful {
t.Errorf("Got status %v, expected %v", j.loadTaskState("clean-up"), successful)
}
if j.loadTaskState("failure") != failed {
t.Errorf("Got status %v, expected %v", j.loadTaskState("failure"), failed)
}
}
func TestCyclicJob(t *testing.T) {
j := &Job{Name: "cyclic", Schedule: "* * * * *"}
j.Add(&Task{
Name: "add-two-four",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo $((2 + 4))"}},
})
j.Add(&Task{
Name: "add-three-four",
Operator: Command{Cmd: "sh", Args: []string{"-c", "echo $((3 + 4))"}},
})
j.SetDownstream(j.Task("add-two-four"), j.Task("add-three-four"))
j.SetDownstream(j.Task("add-three-four"), j.Task("add-two-four"))
store := gomap.NewStore(gomap.DefaultOptions)
j.run(store, j.newExecution())
}