-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy patherror_test.go
140 lines (106 loc) · 3.63 KB
/
error_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package gue
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vgarvardt/gue/v5/adapter"
adapterTesting "github.com/vgarvardt/gue/v5/adapter/testing"
)
func TestErrRescheduleJobIn(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testErrRescheduleJobIn(t, openFunc(t))
})
}
}
func testErrRescheduleJobIn(t *testing.T, connPool adapter.ConnPool) {
t.Helper()
ctx := context.Background()
now := time.Now()
c, err := NewClient(connPool)
require.NoError(t, err)
j := Job{RunAt: now, Type: "foo"}
err = c.Enqueue(ctx, &j)
require.NoError(t, err)
require.NotEmpty(t, j.ID)
jLocked1, err := c.LockJobByID(ctx, j.ID)
require.NoError(t, err)
errReschedule := ErrRescheduleJobIn(10*time.Second, "reschedule me for later time")
errRescheduleStr := `rescheduling job in "10s" because "reschedule me for later time"`
assert.Equal(t, errRescheduleStr, errReschedule.Error())
err = jLocked1.Error(ctx, errReschedule)
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, errRescheduleStr, jLocked2.LastError.String)
assert.GreaterOrEqual(t, jLocked2.RunAt.Sub(jLocked1.RunAt), 10*time.Second)
err = jLocked2.Done(ctx)
require.NoError(t, err)
}
func TestErrRescheduleJobAt(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testErrRescheduleJobAt(t, openFunc(t))
})
}
}
func testErrRescheduleJobAt(t *testing.T, connPool adapter.ConnPool) {
t.Helper()
ctx := context.Background()
now := time.Now()
rescheduleAt := now.Add(3 * time.Hour)
c, err := NewClient(connPool)
require.NoError(t, err)
j := Job{RunAt: now, Type: "foo"}
err = c.Enqueue(ctx, &j)
require.NoError(t, err)
require.NotEmpty(t, j.ID)
jLocked1, err := c.LockJobByID(ctx, j.ID)
require.NoError(t, err)
errReschedule := ErrRescheduleJobAt(rescheduleAt, "reschedule me for later time")
errRescheduleStr := fmt.Sprintf(`rescheduling job at "%s" because "reschedule me for later time"`, rescheduleAt.String())
assert.Equal(t, errRescheduleStr, errReschedule.Error())
err = jLocked1.Error(ctx, errReschedule)
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, errRescheduleStr, jLocked2.LastError.String)
assert.True(t, jLocked2.RunAt.Round(time.Second).Equal(rescheduleAt.Round(time.Second)))
err = jLocked2.Done(ctx)
require.NoError(t, err)
}
func TestErrDiscardJob(t *testing.T) {
for name, openFunc := range adapterTesting.AllAdaptersOpenTestPool {
t.Run(name, func(t *testing.T) {
testErrDiscardJob(t, openFunc(t))
})
}
}
func testErrDiscardJob(t *testing.T, connPool adapter.ConnPool) {
t.Helper()
ctx := context.Background()
now := time.Now()
c, err := NewClient(connPool)
require.NoError(t, err)
j := Job{RunAt: now, Type: "foo"}
err = c.Enqueue(ctx, &j)
require.NoError(t, err)
require.NotEmpty(t, j.ID)
jLocked1, err := c.LockJobByID(ctx, j.ID)
require.NoError(t, err)
errReschedule := ErrDiscardJob("no job - no fear of being fired")
errRescheduleStr := `discarding job because "no job - no fear of being fired"`
assert.Equal(t, errRescheduleStr, errReschedule.Error())
err = jLocked1.Error(ctx, errReschedule)
require.NoError(t, err)
jLocked2, err := c.LockJobByID(ctx, j.ID)
require.Error(t, err)
assert.Nil(t, jLocked2)
}