-
Notifications
You must be signed in to change notification settings - Fork 5
/
invoker_test.go
74 lines (68 loc) · 1.41 KB
/
invoker_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
package sqsd
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type invokerTestPayload struct {
Sleep time.Duration `json:"sleep"`
Status int `json:"status"`
}
func TestHTTPInvoker(t *testing.T) {
mux := &http.ServeMux{}
mux.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
p := &invokerTestPayload{}
_ = json.NewDecoder(r.Body).Decode(p)
time.Sleep(p.Sleep)
w.WriteHeader(p.Status)
})
srv := httptest.NewServer(mux)
defer srv.Close()
i, err := NewHTTPInvoker(srv.URL+"/echo", 300*time.Millisecond)
assert.NoError(t, err)
for _, tt := range []struct {
label string
status int
sleep int64
expectedErr bool
}{
{
label: "200",
status: http.StatusOK,
},
{
label: "400",
status: http.StatusBadRequest,
},
{
label: "500",
status: http.StatusInternalServerError,
expectedErr: true,
},
{
label: "timeout",
sleep: 500,
status: http.StatusOK,
expectedErr: true,
},
} {
t.Run(tt.label, func(t *testing.T) {
b, _ := json.Marshal(invokerTestPayload{
Status: tt.status,
Sleep: time.Duration(tt.sleep) * time.Millisecond,
})
err := i.Invoke(context.Background(), Message{
Payload: string(b),
})
if tt.expectedErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}