-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlauncher_test.go
147 lines (130 loc) · 3.48 KB
/
launcher_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
package task
import (
"context"
"encoding/json"
"net/url"
"testing"
"time"
"github.com/hydronica/trial"
"github.com/jbsmith7741/uri"
"github.com/pcelvng/task/bus/nop"
)
type testWorker struct {
waitTime time.Duration
}
func newTestWorker(info string) Worker {
infoOpt := struct {
WaitTime string `uri:"wait-time"`
}{}
uri.Unmarshal(info, &infoOpt)
d, _ := time.ParseDuration(infoOpt.WaitTime)
return &testWorker{waitTime: d}
}
func (t *testWorker) DoTask(ctx context.Context) (Result, string) {
time.Sleep(t.waitTime)
return CompleteResult, ""
}
func TestLauncher_Status(t *testing.T) {
//setup
prevMsg := nop.FakeMsg
defer func() {
nop.FakeMsg = prevMsg
}()
c, _ := nop.NewConsumer("")
p, _ := nop.NewProducer("")
// Test MaxInProgress is 100
nop.FakeMsg = []byte(`{"type":"test","info":"?wait-time=5s"}`)
l := NewLauncherFromBus(newTestWorker, c, p, &LauncherOptions{MaxInProgress: 100})
l.DoTasks()
time.Sleep(100 * time.Millisecond)
sts := l.Stats()
if sts.TasksRunning != 100 {
t.Errorf("Tasks running should be 100 !=%d", sts.TasksRunning)
}
// Test Average Run time - single worker
nop.FakeMsg = []byte(`{"type":"test","info":"?wait-time=10ms"}`)
l = NewLauncherFromBus(newTestWorker, c, p, nil)
l.DoTasks()
time.Sleep(100 * time.Millisecond)
sts = l.Stats()
if sts.MeanTaskTime != "10ms" {
t.Errorf("Status should display MeanTaskTime: 10ms %v", sts.MeanTaskTime)
}
// Test Average Run time - multiple workers
nop.FakeMsg = []byte(`{"type":"test","info":"?wait-time=10ms"}`)
l = NewLauncherFromBus(newTestWorker, c, p, &LauncherOptions{MaxInProgress: 20})
l.DoTasks()
time.Sleep(100 * time.Millisecond)
sts = l.Stats()
if sts.MeanTaskTime != "10ms" {
t.Errorf("Status should display MeanTaskTime: 10ms %v", sts.MeanTaskTime)
}
}
type tMetaWorker struct {
Info string
Meta
}
var _ meta = tMetaWorker{}
func (w tMetaWorker) DoTask(_ context.Context) (Result, string) {
u, err := url.Parse(w.Info)
if err != nil {
return Failed(err)
}
for k, v := range u.Query() {
w.Meta.SetMeta(k, v...)
}
return Completed("done")
}
func TestDoLaunch(t *testing.T) {
wrkFn := func(info string) Worker {
return tMetaWorker{Info: info, Meta: make(map[string][]string)}
}
type input struct {
meta string
info string
}
fn := func(i trial.Input) (interface{}, error) {
// Create a worker that converts converts all info data
// into meta. This test is designed to test maintaining
// the meta data across tasks without duplicating them.
in := i.Interface().(input)
p, err := nop.NewProducer("")
if err != nil {
return nil, err
}
launcher := &Launcher{
stopCtx: context.Background(),
lastCtx: context.Background(),
slots: make(chan int, 10),
opt: NewLauncherOptions(""),
producer: p,
newWkr: wrkFn,
}
launcher.wg.Add(1)
tsk := &Task{Info: in.info, Meta: in.meta}
launcher.doLaunch(tsk)
if err := json.Unmarshal([]byte(p.Messages["done"][0]), tsk); err != nil {
return nil, err
}
return tsk.Meta, nil
}
cases := trial.Cases{
"empty": {
Input: input{meta: "", info: ""},
Expected: "",
},
"preserve from task": {
Input: input{meta: "meta=value"},
Expected: "meta=value",
},
"append meta": {
Input: input{meta: "test-key=master", info: "?worker=abc"},
Expected: "test-key=master&worker=abc",
},
"override meta": {
Input: input{meta: "key=abc&name=test", info: "?key=xyz&count=10"},
Expected: "count=10&key=xyz&name=test",
},
}
trial.New(fn, cases).SubTest(t)
}