-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher_test.go
101 lines (75 loc) · 1.91 KB
/
dispatcher_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
package minion_test
import (
"sync"
"sync/atomic"
"testing"
"github.com/jsvensson/minion"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type MinionTestSuite struct {
suite.Suite
counter *uint64
}
type TestJob struct {
wg *sync.WaitGroup
counter *uint64
}
// Perform increments the test counter.
func (tj TestJob) Perform() {
atomic.AddUint64(tj.counter, 1)
tj.wg.Done()
}
func TestMinionTestSuite(t *testing.T) {
suite.Run(t, new(MinionTestSuite))
}
func (t *MinionTestSuite) SetupTest() {
t.counter = new(uint64)
}
func (t *MinionTestSuite) TestEnqueueSingleJob() {
disp := minion.NewDispatcher(1, 1)
disp.Run()
wg := &sync.WaitGroup{}
wg.Add(1)
disp.Enqueue(TestJob{wg, t.counter})
wg.Wait()
actual := atomic.LoadUint64(t.counter)
assert.Equal(t.T(), uint64(1), actual)
}
func (t *MinionTestSuite) TestEnqueueManyJobs() {
disp := minion.NewDispatcher(3, 10)
disp.Run()
wg := &sync.WaitGroup{}
for i := 0; i < 50; i++ {
wg.Add(1)
disp.Enqueue(TestJob{wg, t.counter})
}
wg.Wait()
actual := atomic.LoadUint64(t.counter)
assert.Equal(t.T(), uint64(50), actual)
}
func (t *MinionTestSuite) TestTryEnqueueSingleJob() {
disp := minion.NewDispatcher(1, 1)
disp.Run()
wg := &sync.WaitGroup{}
wg.Add(1)
disp.TryEnqueue(TestJob{wg, t.counter})
wg.Wait()
actual := atomic.LoadUint64(t.counter)
assert.Equal(t.T(), uint64(1), actual)
}
func (t *MinionTestSuite) TestTryEnqueueBlockedJob() {
disp := minion.NewDispatcher(1, 1)
wg := &sync.WaitGroup{}
// First call gets enqueued
wg.Add(1)
enqueued := disp.TryEnqueue(TestJob{wg, t.counter})
assert.True(t.T(), enqueued, "first job should not be blocked")
// Second call never gets enqueued
enqueued = disp.TryEnqueue(TestJob{wg, t.counter})
assert.False(t.T(), enqueued, "second job should be blocked")
disp.Run()
wg.Wait()
actual := atomic.LoadUint64(t.counter)
assert.Equal(t.T(), uint64(1), actual)
}