-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_test.go
85 lines (80 loc) · 2.73 KB
/
middleware_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
package opinionatedevents
import (
"context"
"errors"
"math"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestLimitMiddleware(t *testing.T) {
queue := "test"
handler := WithLimit(3)(
func(ctx context.Context, delivery Delivery) error {
return errors.New("just a test")
},
)
msg, err := NewMessage("test.test", &testMessagePayload{"test"})
assert.NoError(t, err)
r1 := handler(context.Background(), &testDelivery{1, queue, msg})
assert.NotNil(t, r1)
assert.False(t, IsFatal(r1))
r2 := handler(context.Background(), &testDelivery{2, queue, msg})
assert.NotNil(t, r2)
assert.False(t, IsFatal(r2))
r3 := handler(context.Background(), &testDelivery{3, queue, msg})
assert.NotNil(t, r3)
assert.True(t, IsFatal(r3))
}
func TestBackoffMiddleware(t *testing.T) {
queue := "test"
handler := WithBackoff(LinearBackoff(2, 1, 10*time.Second))(
func(ctx context.Context, delivery Delivery) error {
return errors.New("just a test")
},
)
msg, err := NewMessage("test.test", &testMessagePayload{"test"})
assert.NoError(t, err)
r1 := handler(context.Background(), &testDelivery{1, queue, msg})
assert.NotNil(t, r1)
var r1r *retryError
assert.True(t, errors.As(r1, &r1r))
assert.LessOrEqual(t, math.Abs(float64(time.Until(r1r.retryAt).Milliseconds()-2000)), 10.0)
r2 := handler(context.Background(), &testDelivery{2, queue, msg})
assert.NotNil(t, r2)
var r2r *retryError
assert.True(t, errors.As(r2, &r2r))
assert.LessOrEqual(t, math.Abs(float64(time.Until(r2r.retryAt).Milliseconds()-3000)), 10.0)
r3 := handler(context.Background(), &testDelivery{3, queue, msg})
assert.NotNil(t, r3)
var r3r *retryError
assert.True(t, errors.As(r3, &r3r))
assert.LessOrEqual(t, math.Abs(float64(time.Until(r3r.retryAt).Milliseconds()-4000)), 10.0)
}
func TestLimitAndBackoffMiddlewares(t *testing.T) {
queue := "test"
handler := WithLimit(3)(
WithBackoff(LinearBackoff(2, 1, 10*time.Second))(
func(ctx context.Context, delivery Delivery) error {
return errors.New("just a test")
},
),
)
msg, err := NewMessage("test.test", &testMessagePayload{"test"})
assert.NoError(t, err)
r1 := handler(context.Background(), &testDelivery{1, queue, msg})
assert.NotNil(t, r1)
assert.False(t, IsFatal(r1))
var r1r *retryError
assert.True(t, errors.As(r1, &r1r))
assert.LessOrEqual(t, math.Abs(float64(time.Until(r1r.retryAt).Milliseconds()-2000)), 10.0)
r2 := handler(context.Background(), &testDelivery{2, queue, msg})
assert.NotNil(t, r2)
assert.False(t, IsFatal(r2))
var r2r *retryError
assert.True(t, errors.As(r2, &r2r))
assert.LessOrEqual(t, math.Abs(float64(time.Until(r2r.retryAt).Milliseconds()-3000)), 10.0)
r3 := handler(context.Background(), &testDelivery{3, queue, msg})
assert.NotNil(t, r3)
assert.True(t, IsFatal(r3))
}