-
Notifications
You must be signed in to change notification settings - Fork 17
/
connector_test.go
142 lines (110 loc) · 3.08 KB
/
connector_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
141
142
package rabbitroutine
import (
"context"
"testing"
"time"
"github.com/pkg/errors"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/stretchr/testify/assert"
)
func TestContextDoneIsCorrectAndNotBlocking(t *testing.T) {
defer time.AfterFunc(1*time.Second, func() { panic("contextDone deadlock") }).Stop()
tests := []struct {
ctx func() context.Context
expected bool
}{
{
func() context.Context {
return context.Background()
},
false,
},
{
func() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
},
true,
},
}
for _, test := range tests {
actual := contextDone(test.ctx())
assert.Equal(t, test.expected, actual)
}
}
func TestDialIsBlocking(t *testing.T) {
conn := NewConnector(Config{
ReconnectAttempts: 10,
Wait: 10 * time.Second,
})
go func() {
_ = conn.Dial(context.Background(), "")
panic("Dial is not blocking")
}()
<-time.After(5 * time.Millisecond)
}
func TestDialReturnErrorOnFailedReconnect(t *testing.T) {
conn := NewConnector(Config{
ReconnectAttempts: 1,
Wait: time.Millisecond,
})
err := conn.Dial(context.Background(), "")
assert.Error(t, err)
}
func TestDialRespectContext(t *testing.T) {
conn := NewConnector(Config{
ReconnectAttempts: 100,
Wait: 5 * time.Minute,
})
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := conn.Dial(ctx, "")
assert.Error(t, err)
assert.Equal(t, errors.Cause(err), ctx.Err())
}
func TestConnBroadcastRespectContext(t *testing.T) {
defer time.AfterFunc(1*time.Second, func() { panic("connBroadcast don't respect context") }).Stop()
conn := NewConnector(Config{})
ctx, cancel := context.WithCancel(context.Background())
cancel()
conn.connBroadcast(ctx)
}
func TestDialWithItRespectContext(t *testing.T) {
defer time.AfterFunc(1*time.Second, func() { panic("dialWithIt don't respect context") }).Stop()
conn := NewConnector(Config{
ReconnectAttempts: 100,
Wait: 5 * time.Minute,
})
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := conn.dialWithIt(ctx, "", amqp.Config{})
assert.Error(t, err)
assert.Equal(t, err, ctx.Err())
}
func TestDialWithItRetryFailed(t *testing.T) {
defer time.AfterFunc(1*time.Second, func() { panic("dialWithIt don't respect context") }).Stop()
conn := NewConnector(Config{
ReconnectAttempts: 3,
Wait: 1 * time.Millisecond,
})
err := conn.dialWithIt(context.Background(), "", amqp.Config{})
assert.Error(t, err)
assert.Nil(t, conn.conn)
}
func TestChannelRespectContext(t *testing.T) {
defer time.AfterFunc(1*time.Second, func() { panic("Channel don't respect context") }).Stop()
conn := NewConnector(Config{
ReconnectAttempts: 100,
Wait: 5 * time.Minute,
})
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := conn.Channel(ctx)
assert.Error(t, err)
assert.Equal(t, err, ctx.Err())
}
func TestConnectorCreationWithEmptyConfig(t *testing.T) {
conn := NewConnector(Config{})
assert.NotEqual(t, 0, conn.cfg.ReconnectAttempts)
}