/
worker_test.go
99 lines (79 loc) · 2.18 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
package thermocline_test
import (
"errors"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/bradfitz/iter"
"github.com/fortytw2/thermocline"
"github.com/fortytw2/thermocline/brokers/mem"
)
func TestWorker(t *testing.T) {
t.Parallel()
_, reader, writer := SetupBroker(t)
tn := rand.Intn(256)
//TODO(fortytw2): don't use iter for tests, even though it's nice
for i := range iter.N(tn) {
task, err := thermocline.NewTask(fmt.Sprintf("test %d", i))
if err != nil {
t.Error("could not create test task", err)
}
writer <- task
}
stopper := make(chan struct{})
var worked int64
wg := &sync.WaitGroup{}
for range iter.N(rand.Intn(256)) {
wg.Add(1)
go thermocline.NewWorker(reader, writer, func(task *thermocline.Task) ([]*thermocline.Task, error) {
atomic.AddInt64(&worked, 1)
return nil, nil
}, stopper).Work(wg)
}
time.Sleep(500 * time.Millisecond)
close(stopper)
wg.Wait()
if atomic.LoadInt64(&worked) != int64(tn) {
t.Errorf("%d tasks not worked in basic test after 500ms, instead %d", tn, atomic.LoadInt64(&worked))
}
}
func TestWorkerRetries(t *testing.T) {
t.Parallel()
var broker thermocline.Broker
broker = mem.NewBroker()
reader, err := broker.Read("test", thermocline.NoVersion)
if err != nil {
t.Errorf("could not open queue '%s'", err)
}
writer, err := broker.Write("test", thermocline.NoVersion)
if err != nil {
t.Errorf("could not open queue '%s'", err)
}
tn := rand.Intn(256)
for i := range iter.N(tn) {
task, err := thermocline.NewTask(fmt.Sprintf("test %d", i))
if err != nil {
t.Error("could not create test task", err)
}
writer <- task
}
stopper := make(chan struct{})
var worked int64
wg := &sync.WaitGroup{}
for range iter.N(rand.Intn(256)) {
wg.Add(1)
go thermocline.NewWorker(reader, writer, func(task *thermocline.Task) ([]*thermocline.Task, error) {
atomic.AddInt64(&worked, 1)
return nil, errors.New("cannot process task, herp derup")
}, stopper).Work(wg)
}
time.Sleep(500 * time.Millisecond)
close(stopper)
wg.Wait()
if atomic.LoadInt64(&worked) != int64(tn*3) {
t.Errorf("%d tasks not worked in retry test after 500ms, actually %d", tn*3, atomic.LoadInt64(&worked))
}
}