-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequeue_retry_test.go
More file actions
164 lines (138 loc) · 4.36 KB
/
Copy pathrequeue_retry_test.go
File metadata and controls
164 lines (138 loc) · 4.36 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package httpsqs
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"github.com/gomooth/httpsqs"
"github.com/gomooth/pkg/framework/retry"
"github.com/gomooth/pkg/mq/internal/logutil"
"github.com/gomooth/pkg/mq/internal/types"
"github.com/stretchr/testify/assert"
)
// mockHTTPSQSClient 模拟 httpsqs.IClient
type mockHTTPSQSClient struct {
getResult string
getPos int64
getErr error
putCalled atomic.Int32
putErr error
}
func (m *mockHTTPSQSClient) Get(_ context.Context, _ string) (string, int64, error) {
return m.getResult, m.getPos, m.getErr
}
func (m *mockHTTPSQSClient) Put(_ context.Context, _ string, _ string) (int64, error) {
m.putCalled.Add(1)
return 1, m.putErr
}
func (m *mockHTTPSQSClient) Status(_ context.Context, _ string) (*httpsqs.Status, error) {
return nil, nil
}
func (m *mockHTTPSQSClient) View(_ context.Context, _ string, _ int64) (string, error) {
return "", nil
}
func (m *mockHTTPSQSClient) Reset(_ context.Context, _ string) error { return nil }
func (m *mockHTTPSQSClient) SetMaxQueue(_ context.Context, _ string, _ int) error {
return nil
}
func (m *mockHTTPSQSClient) SetSyncTime(_ context.Context, _ string, _ time.Duration) error {
return nil
}
func TestRequeueRetryStrategy_Success(t *testing.T) {
client := &mockHTTPSQSClient{}
strategy := newRequeueRetryStrategy(
types.FuncHandler(func(ctx context.Context, msg types.Message) error {
return nil
}),
3,
&retry.FixedDelay{Wait: time.Millisecond},
client,
"test-queue",
logutil.NewSlogLogger(nilLogger()),
nil,
)
err := strategy.OnMessage(context.Background(), "test", []byte("hello"))
assert.NoError(t, err)
assert.Equal(t, int32(0), client.putCalled.Load(), "success should not requeue")
}
func TestRequeueRetryStrategy_RequeueOnFailure(t *testing.T) {
client := &mockHTTPSQSClient{}
strategy := newRequeueRetryStrategy(
types.FuncHandler(func(ctx context.Context, msg types.Message) error {
return errors.New("fail")
}),
5,
&retry.FixedDelay{Wait: time.Millisecond},
client,
"test-queue",
logutil.NewSlogLogger(nilLogger()),
nil,
)
err := strategy.OnMessage(context.Background(), "test", []byte("hello"))
assert.NoError(t, err)
assert.Equal(t, int32(1), client.putCalled.Load(), "failure should requeue")
}
func TestRequeueRetryStrategy_Exhausted(t *testing.T) {
client := &mockHTTPSQSClient{}
var failedCalled atomic.Int32
strategy := newRequeueRetryStrategy(
types.FuncHandler(func(ctx context.Context, msg types.Message) error {
return errors.New("always fail")
}),
0, // maxRetry=0 means no retries, immediate exhaustion
&retry.FixedDelay{Wait: time.Millisecond},
client,
"test-queue",
logutil.NewSlogLogger(nilLogger()),
nil,
)
strategy.SetFailedHandler(func(ctx context.Context, msg types.Message, err error) {
failedCalled.Add(1)
})
err := strategy.OnMessage(context.Background(), "test", []byte("hello"))
assert.NoError(t, err)
assert.Equal(t, int32(1), failedCalled.Load())
assert.Equal(t, int32(0), client.putCalled.Load(), "exhausted message should not be requeued")
}
func TestRequeueRetryStrategy_ContextCancel(t *testing.T) {
client := &mockHTTPSQSClient{}
strategy := newRequeueRetryStrategy(
types.FuncHandler(func(ctx context.Context, msg types.Message) error {
return errors.New("fail")
}),
100,
&retry.FixedDelay{Wait: 10 * time.Millisecond},
client,
"test-queue",
logutil.NewSlogLogger(nilLogger()),
nil,
)
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := strategy.OnMessage(ctx, "test", []byte("hello"))
// context 已取消,可能在退避等待中返回
assert.True(t, err != nil, "should return error on context cancel")
}
func TestRequeueRetryStrategy_PutFailure(t *testing.T) {
client := &mockHTTPSQSClient{putErr: errors.New("put failed")}
var failedCalled atomic.Int32
strategy := newRequeueRetryStrategy(
types.FuncHandler(func(ctx context.Context, msg types.Message) error {
return errors.New("handle failed")
}),
3,
&retry.FixedDelay{Wait: time.Millisecond},
client,
"test-queue",
logutil.NewSlogLogger(nilLogger()),
nil,
)
strategy.SetFailedHandler(func(ctx context.Context, msg types.Message, err error) {
failedCalled.Add(1)
})
err := strategy.OnMessage(context.Background(), "test", []byte("hello"))
assert.NoError(t, err)
// Put 失败后走 exhausted 逻辑
assert.Equal(t, int32(1), failedCalled.Load())
}