-
Notifications
You must be signed in to change notification settings - Fork 2
/
connector_test.go
87 lines (72 loc) · 1.28 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
package rmqconn
import (
"fmt"
"github.com/streadway/amqp"
"sync/atomic"
"testing"
"time"
)
var (
attempts int32
)
type connMock struct {
с chan *amqp.Error
}
func (c *connMock) Close() error {
close(c.с)
return nil
}
func (c *connMock) NotifyClose(ch chan *amqp.Error) chan *amqp.Error {
c.с = ch
return nil
}
func (c *connMock) GetChannel() (*amqp.Channel, error) {
return new(amqp.Channel), nil
}
func (c *connMock) Disconnect() {
close(c.с)
}
func DialMock(url string) (Conner, error) {
defer atomic.AddInt32(&attempts, 1)
if atomic.LoadInt32(&attempts) == 0 {
conn := &connMock{make(chan *amqp.Error)}
go func() {
time.Sleep(time.Millisecond * 100)
conn.Disconnect()
}()
return conn, nil
}
if atomic.LoadInt32(&attempts) <= 1 {
return nil, fmt.Errorf("dial err")
} else {
return &connMock{make(chan *amqp.Error)}, nil
}
}
func TestReconn(t *testing.T) {
c, err := Open("amqp://host", DialMock)
if err != nil {
t.Fail()
}
if !c.IsConnected() {
t.Fail()
} else {
_, err := c.GetChannel()
if err != nil {
t.Fail()
}
}
time.Sleep(time.Second * 3)
c.Close()
c.Close()
_, err = c.GetChannel()
if err == nil {
t.Fail()
}
}
func TestDial(t *testing.T) {
c, err := Open("amqp://host", Dial)
if err == nil {
t.Fail()
}
c.Close()
}