forked from pcelvng/task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher_test.go
69 lines (59 loc) · 1.7 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
package task
import (
"context"
"testing"
"time"
"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(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)
}
}