forked from vgarvardt/gue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff_test.go
102 lines (78 loc) · 2.75 KB
/
backoff_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
package gue
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"github.com/vgarvardt/gue/v5/adapter"
adapterTesting "github.com/vgarvardt/gue/v5/adapter/testing"
adapterZap "github.com/vgarvardt/gue/v5/adapter/zap"
)
func TestBackoff(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testBackoff(t, openFunc(t))
})
}
}
func testBackoff(t *testing.T, connPool adapter.ConnPool) {
ctx := context.Background()
logger := adapterZap.New(zaptest.NewLogger(t))
now := time.Now()
t.Run("default exponential backoff", func(t *testing.T) {
c, err := NewClient(connPool, WithClientLogger(logger))
require.NoError(t, err)
j := Job{RunAt: now, Type: "foo"}
err = c.Enqueue(ctx, &j)
require.NoError(t, err)
jLocked1, err := c.LockJobByID(ctx, j.ID)
require.NoError(t, err)
err = jLocked1.Error(ctx, errors.New("return with the error"))
require.NoError(t, err)
jLocked2, err := c.LockJobByID(ctx, j.ID)
require.NoError(t, err)
assert.Equal(t, int32(1), jLocked2.ErrorCount)
assert.True(t, jLocked2.LastError.Valid)
assert.Equal(t, "return with the error", jLocked2.LastError.String)
assert.Greater(t, jLocked2.RunAt.Unix(), jLocked1.RunAt.Unix())
err = jLocked2.Done(ctx)
require.NoError(t, err)
})
t.Run("never backoff", func(t *testing.T) {
c, err := NewClient(connPool, WithClientLogger(logger), WithClientBackoff(BackoffNever))
require.NoError(t, err)
j := Job{RunAt: now, Type: "bar"}
err = c.Enqueue(ctx, &j)
require.NoError(t, err)
jLocked1, err := c.LockJobByID(ctx, j.ID)
require.NoError(t, err)
err = jLocked1.Error(ctx, errors.New("return with the error"))
require.NoError(t, err)
jLocked2, err := c.LockJobByID(ctx, j.ID)
require.Error(t, err)
assert.Nil(t, jLocked2)
})
t.Run("const backoff", func(t *testing.T) {
c, err := NewClient(connPool, WithClientLogger(logger), WithClientBackoff(NewConstantBackoff(time.Minute)))
require.NoError(t, err)
j := Job{RunAt: now, Type: "foo"}
err = c.Enqueue(ctx, &j)
require.NoError(t, err)
jLocked1, err := c.LockJobByID(ctx, j.ID)
require.NoError(t, err)
err = jLocked1.Error(ctx, errors.New("return with the error"))
require.NoError(t, err)
jLocked2, err := c.LockJobByID(ctx, j.ID)
require.NoError(t, err)
assert.Equal(t, int32(1), jLocked2.ErrorCount)
assert.True(t, jLocked2.LastError.Valid)
assert.Equal(t, "return with the error", jLocked2.LastError.String)
assert.Greater(t, jLocked2.RunAt.Unix(), jLocked1.RunAt.Unix())
assert.WithinDuration(t, jLocked1.RunAt.Add(time.Minute), jLocked2.RunAt, time.Second)
err = jLocked2.Done(ctx)
require.NoError(t, err)
})
}