-
Notifications
You must be signed in to change notification settings - Fork 86
/
cli_test.go
96 lines (83 loc) · 1.96 KB
/
cli_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
package worker
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
gocontext "context"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/travis-ci/worker/context"
)
func TestNewCLI(t *testing.T) {
c := NewCLI(nil)
assert.NotNil(t, c)
assert.Nil(t, c.c)
assert.True(t, c.bootTime.String() < time.Now().UTC().String())
}
func TestCLI_heartbeatHandler(t *testing.T) {
i := NewCLI(nil)
i.heartbeatSleep = time.Duration(0)
i.heartbeatErrSleep = time.Duration(0)
ctx, cancel := gocontext.WithCancel(gocontext.Background())
logger := context.LoggerFromContext(ctx).WithField("self", "cli_test")
i.ctx = ctx
i.cancel = cancel
i.logger = logger
logrus.SetLevel(logrus.FatalLevel)
i.ProcessorPool = NewProcessorPool(&ProcessorPoolConfig{
Context: ctx,
}, nil, nil, nil, nil)
n := 0
done := make(chan struct{})
mux := http.NewServeMux()
mux.HandleFunc(`/`, func(w http.ResponseWriter, req *http.Request) {
n++
switch n {
case 1:
t.Logf("responding 404")
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "no\n")
return
case 2:
t.Logf("responding 200 with busted JSON")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "{bork\n")
return
case 3:
t.Logf("responding 200 with unexpected JSON")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"etat": "sous"}`)
return
case 4:
t.Logf("responding 200 with JSON of mismatched type")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"state": true}`)
return
case 5:
t.Logf("responding 200 with state=up")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"state": "up"}`)
return
default:
t.Logf("responding 200 with state=down")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"state": "down"}`)
done <- struct{}{}
return
}
})
ts := httptest.NewServer(mux)
defer ts.Close()
go i.heartbeatHandler(ts.URL, "")
for {
select {
case <-done:
cancel()
return
default:
time.Sleep(time.Millisecond)
}
}
}