-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker_test.go
140 lines (117 loc) · 2.9 KB
/
worker_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
package qpool
import (
"context"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
const timeoutMsg = "Test timed out waiting for value retrieval"
func TestWorker(t *testing.T) {
Convey("Given a worker", t, func() {
Convey("It should process a job successfully", func() {
pool := &Q{
ctx: context.Background(),
workers: make(chan chan Job, 1),
space: NewQSpace(),
metrics: NewMetrics(),
}
worker := &Worker{
pool: pool,
jobs: make(chan Job, 1),
}
Reset(func() {
close(worker.jobs)
pool.space.Close()
})
job := Job{
ID: "job_success",
Fn: func() (any, error) { return "result", nil },
StartTime: time.Now(),
TTL: 10 * time.Second,
Dependencies: []string{},
}
worker.jobs <- job
go worker.run()
result := pool.space.Await(job.ID)
select {
case <-time.After(2 * time.Second):
t.Fatal(timeoutMsg)
case value := <-result:
So(value.Error, ShouldBeNil)
So(value.Value, ShouldEqual, "result")
}
})
Convey("It should handle job timeout", func() {
pool := &Q{
ctx: context.Background(),
workers: make(chan chan Job, 1),
space: NewQSpace(),
metrics: NewMetrics(),
}
worker := &Worker{
pool: pool,
jobs: make(chan Job, 1),
}
Reset(func() {
close(worker.jobs)
pool.space.Close()
})
job := Job{
ID: "job_timeout",
Fn: func() (any, error) {
time.Sleep(200 * time.Millisecond)
return nil, nil
},
StartTime: time.Now(),
TTL: 100 * time.Millisecond,
Dependencies: []string{},
}
ctx, cancel := context.WithTimeout(pool.ctx, 100*time.Millisecond)
defer cancel()
worker.pool.ctx = ctx
worker.jobs <- job
go worker.run()
result := pool.space.Await(job.ID)
select {
case <-time.After(2 * time.Second):
t.Fatal(timeoutMsg)
case value := <-result:
So(value.Error, ShouldNotBeNil)
So(value.Error.Error(), ShouldContainSubstring, "timed out")
}
})
Convey("It should not process a job if dependencies are not met", func() {
pool := &Q{
ctx: context.Background(),
workers: make(chan chan Job, 1),
space: NewQSpace(),
metrics: NewMetrics(),
}
worker := &Worker{
pool: pool,
jobs: make(chan Job, 1),
}
Reset(func() {
close(worker.jobs)
pool.space.Close()
})
job := Job{
ID: "job_dependency",
Fn: func() (any, error) { return "result", nil },
StartTime: time.Now(),
TTL: 10 * time.Second,
Dependencies: []string{"dep1"},
}
worker.jobs <- job
go worker.run()
result := pool.space.Await(job.ID)
select {
case <-time.After(2 * time.Second):
t.Fatal(timeoutMsg)
case value := <-result:
So(value.Error, ShouldNotBeNil)
So(value.Error.Error(), ShouldContainSubstring, "dependency dep1 failed")
}
})
})
}