-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathapi_retries_test.go
More file actions
92 lines (75 loc) · 2.24 KB
/
Copy pathapi_retries_test.go
File metadata and controls
92 lines (75 loc) · 2.24 KB
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
package workers
import (
"context"
"encoding/json"
"errors"
"log"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRetries_Empty(t *testing.T) {
a := apiServer{}
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/retries", nil)
a.Retries(recorder, request)
assert.Equal(t, "[]\n", recorder.Body.String())
}
func TestRetries_NotEmpty(t *testing.T) {
a := &apiServer{
logger: log.New(os.Stdout, "go-workers2: ", log.Ldate|log.Lmicroseconds),
}
// test API replies without registered workers
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/retries", nil)
a.Retries(recorder, request)
assert.Equal(t, "[]\n", recorder.Body.String())
// test API replies with registered workers
opts, err := SetupDefaultTestOptionsWithNamespace("prod")
assert.NoError(t, err)
mgr := &Manager{opts: opts}
a.registerManager(mgr)
recorder = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/retries", nil)
a.Retries(recorder, request)
actualWithManagerBytes := recorder.Body.Bytes()
actualReplyParsed := []*Retries{}
err = json.Unmarshal(actualWithManagerBytes, &actualReplyParsed)
assert.NoError(t, err)
assert.Equal(t, []*Retries{{}}, actualReplyParsed)
//puts messages in retry queue when they fail
message, _ := NewMsg("{\"jid\":\"2\",\"retry\":true}")
tests := []struct {
name string
f JobFunc
}{
{
name: "retry on panic",
f: panickingFunc,
},
{
name: "retry on error",
f: func(m *Msg) error {
return errors.New("ERROR")
},
},
}
var messages []string
for index, test := range tests {
// Test panic
wares.build("myqueue", mgr, test.f)(message)
// retries order is not guaranteed
retries, err := opts.client.ZRange(context.Background(), retryQueue(opts.Namespace), 0, -1).Result()
assert.NoError(t, err)
assert.Len(t, retries, index+1)
messages = append(messages, message.ToJson())
assert.ElementsMatch(t, messages, retries)
}
recorder = httptest.NewRecorder()
request = httptest.NewRequest("GET", "/retries", nil)
a.Retries(recorder, request)
assert.NoError(t, err)
assert.NotEqual(t, "[]\n", recorder.Body.String())
assert.NotEqual(t, string(actualWithManagerBytes), recorder.Body.String())
}