/
luxury_test.go
150 lines (129 loc) · 2.79 KB
/
luxury_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
package luxury
import (
"log"
"path/filepath"
"testing"
"context"
)
func TestWorkflow(t *testing.T) {
testConf, err := filepath.Abs("./test/workflow.yaml")
if err != nil {
t.Fatal(err)
}
counter := 0
wf := New()
ok := func(ctx context.Context) (context.Context, string, error) {
counter = counter + 1
return ctx, "OK", nil
}
NewAgent(wf.Context, ok, "start", "step1", "step2", "step3", "step4")
err = wf.Load(testConf)
if err != nil {
t.Log(testConf)
t.Error(err)
}
err = wf.Handle("start")
if err != nil {
t.Error(err)
}
if counter != 3 {
t.Error(counter, "is not 3")
}
counter = 0
err = wf.Handle("step1")
if err != nil {
t.Error(err)
}
if counter != 1 {
t.Error(counter, "is not 1")
}
}
func TestContextValueChange(t *testing.T) {
testConf, err := filepath.Abs("./test/workflow.yaml")
if err != nil {
t.Fatal(err)
}
okFn := func(ctx context.Context) (context.Context, string, error) {
ctx = context.WithValue(ctx, "key", "value")
return ctx, "OK", nil
}
wf := New()
NewAgent(wf.Context, okFn, "start", "step1", "step2", "step3", "step4")
err = wf.Load(testConf)
if err != nil {
t.Log(testConf)
t.Error(err)
}
err = wf.Handle("start")
if err != nil {
t.Error(err)
}
value, ok := wf.Context.Value("key").(string)
t.Log(value, ok)
if !ok {
t.Error("value is not string")
}
if value != "value" {
t.Error("value is not value", value)
}
}
func TestDynamicStep(t *testing.T) {
log.Println("test dynamic")
testConf, err := filepath.Abs("./test/dynamic_step.yaml")
if err != nil {
t.Fatal(err)
}
starterFn := func() {
}
dynamicFn := func(ctx context.Context) context.Context {
q := Queue{}
d := Duty{}
d["default"] = "ok"
q.Add("dynamic", d)
t.Log("add d")
t.Log(q)
d2 := Duty{}
d2["OK"] = "complete"
q.Add("ok", d2)
ctx = context.WithValue(ctx, "queue", q)
return ctx
}
okFn := func(ctx context.Context) (context.Context, string, error) {
ctx = context.WithValue(ctx, "key", "value")
return ctx, "OK", nil
}
completeFn := func(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, "complete", "finish")
return ctx
}
wf := New()
NewAgent(wf.Context, starterFn, "start")
NewAgent(wf.Context, dynamicFn, "dynamic")
NewAgent(wf.Context, completeFn, "complete")
NewAgent(wf.Context, okFn, "ok")
err = wf.Load(testConf)
if err != nil {
t.Log(testConf)
t.Error(err)
}
err = wf.Handle("start")
if err != nil {
t.Error(err)
}
value, ok := wf.Context.Value("key").(string)
t.Log(value, ok)
if !ok {
t.Error("value is not string")
}
if value != "value" {
t.Error("value is not value", value)
}
value, ok = wf.Context.Value("complete").(string)
t.Log(value, ok)
if !ok {
t.Error("complete is not string")
}
if value != "finish" {
t.Error("complete is not finish", value)
}
}