This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
forked from assembla/cony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
162 lines (123 loc) · 2.65 KB
/
client_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package cony
import (
"errors"
"testing"
"github.com/streadway/amqp"
)
func TestNewClient(t *testing.T) {
c := NewClient(func(c *Client) {
c.addr = "test1"
})
if c.addr != "test1" {
t.Error("should call functional opts")
}
}
func TestClient_Declare(t *testing.T) {
c := &Client{}
if len(c.declarations) != 0 {
t.Error("declarations should be empty")
}
q := &Queue{}
c.Declare([]Declaration{DeclareQueue(q)})
if len(c.declarations) != 1 {
t.Error("declarations should have 1 declaration")
}
}
func TestClient_Consume(t *testing.T) {
c := NewClient()
cons := &Consumer{}
c.Consume(cons)
if _, ok := c.consumers[cons]; !ok {
t.Error("should save consumer")
}
c.deleteConsumer(cons)
if _, ok := c.consumers[cons]; ok {
t.Error("should remove consumer")
}
}
func TestClient_Publish(t *testing.T) {
c := NewClient()
pub := &Publisher{}
c.Publish(pub)
if _, ok := c.publishers[pub]; !ok {
t.Error("should save publisher")
}
c.deletePublisher(pub)
if _, ok := c.publishers[pub]; ok {
t.Error("should remove publisher")
}
}
func TestClient_Errors(t *testing.T) {
c := NewClient()
errs := c.Errors()
if errs == nil {
t.Error("should initialize Errors channel")
}
}
func TestClient_Close(t *testing.T) {
c := NewClient()
c.Close()
if c.run != noRun {
t.Error("should stop running")
}
}
func TestClient_Loop(t *testing.T) {
c := NewClient()
c.run = noRun
if c.Loop() {
t.Error("should not run if noRun")
}
// immitate connection
c.conn.Store(&amqp.Connection{})
c.run = run
if !c.Loop() {
t.Error("should tell loop to run")
}
}
func TestClient_reportErr(t *testing.T) {
c := NewClient()
if c.reportErr(nil) {
t.Error("should return false on no error")
}
// fill in errs buffer
for i := 0; i < 100; i++ {
c.reportErr(errors.New("test err"))
}
// should not block, error will be discarded
if !c.reportErr(errors.New("test err")) {
t.Error("should return true")
}
}
func TestClient_channel(t *testing.T) {}
func TestClient_connection(t *testing.T) {
c := NewClient()
if _, err := c.connection(); err != ErrNoConnection {
t.Error("error should be", ErrNoConnection)
}
c.conn.Store(&amqp.Connection{})
con, err := c.connection()
if con == nil {
t.Error("should return existing connection")
}
if err != nil {
t.Error("should be no errors")
}
}
func TestURL(t *testing.T) {
c := &Client{}
URL("test1")(c)
if c.addr != "test1" {
t.Error("should set URL")
}
URL("")(c)
if c.addr != "amqp://guest:guest@localhost/" {
t.Error("should use default URL")
}
}
func TestBackoff(t *testing.T) {
c := &Client{}
Backoff(DefaultBackoff)(c)
if c.bo == nil {
t.Error("should set backoff")
}
}