-
Notifications
You must be signed in to change notification settings - Fork 86
/
multi_source_job_queue_test.go
107 lines (87 loc) · 2.77 KB
/
multi_source_job_queue_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
package worker
import (
"fmt"
"testing"
"time"
gocontext "context"
"github.com/stretchr/testify/assert"
"github.com/travis-ci/worker/context"
)
func TestNewMultiSourceJobQueue(t *testing.T) {
ctx := gocontext.TODO()
logger := context.LoggerFromContext(ctx)
jq0 := &fakeJobQueue{c: make(chan Job)}
jq1 := &fakeJobQueue{c: make(chan Job)}
msjq := NewMultiSourceJobQueue(jq0, jq1)
assert.NotNil(t, msjq)
buildJobChan, err := msjq.Jobs(ctx)
assert.Nil(t, err)
assert.NotNil(t, buildJobChan)
done := make(chan struct{})
go func() {
logger.Debugf("about to <-%#v [\"buildJobChan\"]", buildJobChan)
<-buildJobChan
logger.Debugf("<-%#v [\"buildJobChan\"]", buildJobChan)
logger.Debugf("about to <-%#v [\"buildJobChan\"]", buildJobChan)
<-buildJobChan
logger.Debugf("<-%#v [\"buildJobChan\"]", buildJobChan)
logger.Debugf("about to %#v [\"done\"] <- {}", done)
done <- struct{}{}
logger.Debugf("%#v [\"done\"] <- {}", done)
}()
go func() {
logger.Debugf("about to %#v [\"jq0.c\"] <- &fakeJob{}", jq0.c)
jq0.c <- &fakeJob{}
logger.Debugf("%#v [\"jq0.c\"] <- &fakeJob{}", jq0.c)
logger.Debugf("about to %#v [\"done\"] <- {}", done)
done <- struct{}{}
logger.Debugf("%#v [\"done\"] <- {}", done)
}()
go func() {
logger.Debugf("about to %#v [\"jq1.c\"] <- &fakeJob{}", jq1.c)
jq1.c <- &fakeJob{}
logger.Debugf("%#v [\"jq1.c\"] <- &fakeJob{}", jq1.c)
logger.Debugf("about to %#v [\"done\"] <- {}", done)
done <- struct{}{}
logger.Debugf("%#v [\"done\"] <- {}", done)
}()
doneCount := 0
for doneCount < 3 {
logger.Debugf("entering for loop")
timeout := 5 * time.Second
select {
case <-time.After(timeout):
assert.FailNow(t, fmt.Sprintf("jobs were not received within %v", timeout))
case <-done:
logger.Debugf("<-%#v [\"done\"]", done)
doneCount++
}
}
}
func TestMultiSourceJobQueue_Name(t *testing.T) {
jq0 := &fakeJobQueue{c: make(chan Job)}
jq1 := &fakeJobQueue{c: make(chan Job)}
msjq := NewMultiSourceJobQueue(jq0, jq1)
assert.Equal(t, "fake,fake", msjq.Name())
}
func TestMultiSourceJobQueue_Cleanup(t *testing.T) {
jq0 := &fakeJobQueue{c: make(chan Job)}
jq1 := &fakeJobQueue{c: make(chan Job)}
msjq := NewMultiSourceJobQueue(jq0, jq1)
err := msjq.Cleanup()
assert.Nil(t, err)
assert.True(t, jq0.cleanedUp)
assert.True(t, jq1.cleanedUp)
}
func TestMultiSourceJobQueue_Jobs_uniqueChannels(t *testing.T) {
jq0 := &fakeJobQueue{c: make(chan Job)}
jq1 := &fakeJobQueue{c: make(chan Job)}
msjq := NewMultiSourceJobQueue(jq0, jq1)
buildJobChan0, err := msjq.Jobs(gocontext.TODO())
assert.Nil(t, err)
assert.NotNil(t, buildJobChan0)
buildJobChan1, err := msjq.Jobs(gocontext.TODO())
assert.Nil(t, err)
assert.NotNil(t, buildJobChan1)
assert.NotEqual(t, fmt.Sprintf("%#v", buildJobChan0), fmt.Sprintf("%#v", buildJobChan1))
}