forked from travis-ci/worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_job_queue_test.go
94 lines (83 loc) · 2.14 KB
/
http_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
package worker
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
gocontext "context"
"github.com/stretchr/testify/assert"
)
func TestHTTPJobQueue(t *testing.T) {
hjq, err := NewHTTPJobQueue(nil, "test", "fake", "fake", nil)
assert.Nil(t, err)
assert.NotNil(t, hjq)
}
func TestHTTPJobQueue_Jobs(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc(`/jobs/pop`, func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"job_id":"100001"}`)
})
mux.HandleFunc(`/jobs/100001/claim`, func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
})
mux.HandleFunc(`/jobs/100001`, func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, strings.Replace(`{
"data": {
"type": "job",
"job": {
"id": 100001,
"number": "42.1",
"queued_at": "2011-04-01T11:05:55Z"
},
"source": {
"id": 100001,
"number": "42"
},
"repository": {
"id": 8490324,
"slug": "travis-ci/nonexistent-repository"
},
"uuid": "fafafaf",
"config": {},
"vm_type": "test",
"meta": {
"state_update_count": 0
}
}
}`, "\t", " ", -1))
})
mux.HandleFunc(`/`, func(w http.ResponseWriter, req *http.Request) {
t.Fatalf("unknown URL requested: %#v", req.URL.Path)
})
jobBoardServer := httptest.NewServer(mux)
defer jobBoardServer.Close()
jobBoardURL, _ := url.Parse(jobBoardServer.URL)
hjq, err := NewHTTPJobQueue(jobBoardURL, "test", "fake", "fake", nil)
assert.Nil(t, err)
assert.NotNil(t, hjq)
ctx := gocontext.TODO()
buildJobChan, err := hjq.Jobs(ctx)
assert.Nil(t, err)
assert.NotNil(t, buildJobChan)
select {
case job := <-buildJobChan:
assert.NotNil(t, job)
case <-time.After(time.Second):
t.Fatalf("failed to recv job")
}
}
func TestHTTPJobQueue_Name(t *testing.T) {
hjq, err := NewHTTPJobQueue(nil, "test", "fake", "fake", nil)
assert.Nil(t, err)
assert.Equal(t, "http", hjq.Name())
}
func TestHTTPJobQueue_Cleanup(t *testing.T) {
hjq, err := NewHTTPJobQueue(nil, "test", "fake", "fake", nil)
assert.Nil(t, err)
assert.Nil(t, hjq.Cleanup())
}