forked from kamildrazkiewicz/go-flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goflow_test.go
201 lines (173 loc) · 5.2 KB
/
goflow_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
package goflow
import (
"context"
"errors"
"testing"
"time"
)
type ReqInfo struct {
srcID string
}
func TestNew(t *testing.T) {
gf := New()
if gf == nil {
t.Error("New() error")
}
}
func TestAdd1(t *testing.T) {
ctx := context.Background()
gf := New()
gf.Add("test", false, []string{"dep1"}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
return "test result", 0, nil
})
_, err := gf.Do(ctx)
if err.Error() != "Error: Function \"dep1\" not exists!" {
t.Error("Not existing function error")
}
}
func TestAdd2(t *testing.T) {
gf := New()
ctx := context.Background()
gf.Add("test", false, []string{"test"}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
return "test result", 0, nil
})
_, err := gf.Do(ctx)
if err.Error() != "Error: Function \"test\" depends of it self!" {
t.Error("Self denepdency error")
}
}
func TestDo1(t *testing.T) {
ctx := context.Background()
gf := New()
gf.Add("test", false, []string{}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
return "test result", 0, nil
})
res, err := gf.Do(ctx)
if err != nil || res["test"] != "test result" {
t.Error("Incorrect result")
}
}
func TestDo2(t *testing.T) {
var shouldBeFalse bool = false
ctx := context.Background()
gf := New()
gf.Add("first", false, []string{}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
time.Sleep(time.Second * 1)
shouldBeFalse = true
return "first result", 0, nil
})
gf.Add("second", false, []string{"first"}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
shouldBeFalse = false
return "second result", 0, nil
})
_, err := gf.Do(ctx)
if err != nil || shouldBeFalse == true {
t.Error("Incorrect goroutines execution order")
}
}
func TestDo3(t *testing.T) {
ctx := context.Background()
gf := New()
gf.Add("first", false, []string{}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
return "first result", 0, nil
})
gf.Add("second", false, []string{"first"}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
return "second result", 0, nil
})
res, err := gf.Do(ctx)
firstResult := res["first"]
secondResult := res["second"]
if err != nil || firstResult != "first result" || secondResult != "second result" {
t.Error("Incorrect results")
}
}
func TestDo4(t *testing.T) {
ctx := context.Background()
gf := New()
gf.Add("first", false, []string{}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
return "first result", 0, errors.New("some error")
})
gf.Add("second", false, []string{"first"}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
return "second result", 0, nil
})
_, err := gf.Do(ctx)
if err.Error() != "execute err, please check task" {
t.Error("Incorrect error value")
}
}
func TestAwaysPanic(t *testing.T) {
ctx := context.Background()
gf := New()
gf.Add("first", true, []string{}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
panic("test")
return "first result", 0, errors.New("some error")
})
gf.Add("second", true, []string{"first"}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
panic("test")
return "second result", 0, nil
})
gf.Add("third", true, []string{"first"}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
panic("test")
return "third result", 0, nil
})
_, err := gf.Do(ctx)
if err != nil {
t.Error("Incorrect error value")
}
}
func TestDoSkipPanic(t *testing.T) {
ctx := context.Background()
gf := New()
gf.Add("first", true, []string{}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
panic("test")
return "first result", 0, errors.New("some error")
})
gf.Add("second", false, []string{"first"}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
return "second result", 0, nil
})
res, err := gf.Do(ctx)
firstResult := res["first"]
secondResult := res["second"]
if err != nil || firstResult != nil || secondResult != "second result" {
t.Error("Incorrect results")
}
if gf.Funcs["first"].ErrCode != ErrCodePanic || gf.Funcs["second"].ErrCode == ErrCodePanic {
t.Error("Incorrect ErrCode")
}
}
func TestDoSkipErr(t *testing.T) {
ctx := context.Background()
const UserDefineErrCode = 7
gf := New()
gf.Add("first", true, []string{}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
return "first result", UserDefineErrCode, errors.New("some error")
})
gf.Add("second", false, []string{"first"}, func(ctx context.Context, res map[string]interface{}) (
interface{}, int64, error) {
return "second result", 0, nil
})
res, err := gf.Do(ctx)
firstResult := res["first"]
secondResult := res["second"]
if err != nil || firstResult != "first result" || secondResult != "second result" {
t.Error("Incorrect results")
}
if gf.Funcs["first"].ErrCode != UserDefineErrCode || gf.Funcs["second"].ErrCode != 0 {
t.Error("Incorrect ErrCode")
}
}