-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpublisher_test.go
More file actions
183 lines (147 loc) · 4.63 KB
/
Copy pathpublisher_test.go
File metadata and controls
183 lines (147 loc) · 4.63 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package steadyrabbit_test
import (
"context"
"time"
"github.com/BackAged/steadyrabbit"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/streadway/amqp"
)
var _ = Describe("Publisher", func() {
var (
cnf *steadyrabbit.Config
p *steadyrabbit.Publisher
err error
)
JustBeforeEach(func() {
cnf = formPublisherConfig()
// p, err = steadyrabbit.NewPublisher(cnf)
// Expect(err).ToNot(HaveOccurred())
// Expect(p).ToNot(BeNil())
})
Describe("NewPublisher", func() {
Context("instantiating", func() {
It("should success with valid config", func() {
p, err := steadyrabbit.NewPublisher(cnf)
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
})
It("should success with exchange config", func() {
cnf := formPublisherConfig()
cnf.Publisher = &steadyrabbit.PublisherConfig{
PublishConfirmation: true,
Exchange: &steadyrabbit.ExchangeConfig{
ExchangeDeclare: true,
ExchangeType: amqp.ExchangeDirect,
ExchangeName: "shahin",
ExchangeDurable: false,
ExchangeAutoDelete: false,
},
}
p, err := steadyrabbit.NewPublisher(cnf)
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
})
It("should error with missing config", func() {
p, err := steadyrabbit.NewPublisher(nil)
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring("Config can not be nil"))
Expect(p).To(BeNil())
})
It("should error with unreachable rabbit server", func() {
cnf := formPublisherConfig()
cnf.URL = "amqp://bad-url"
r, err := steadyrabbit.NewPublisher(cnf)
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring("unable to dial server"))
Expect(r).To(BeNil())
})
It("should start PublisherChannel watcher if publisher confirm is on", func() {
cnf := formPublisherConfig()
cnf.Publisher = &steadyrabbit.PublisherConfig{
PublishConfirmation: true,
}
p, err := steadyrabbit.NewPublisher(cnf)
Expect(err).To(BeNil())
Expect(p).ToNot(BeNil())
})
})
})
Describe("Publish", func() {
Context("with good connection", func() {
It("should success", func() {
cnf := formPublisherConfig()
p, err := steadyrabbit.NewPublisher(cnf)
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
err = p.Publish(context.Background(), "shahin", []byte("shahin"))
Expect(err).ToNot(HaveOccurred())
})
It("should success with exchange config", func() {
cnf := formPublisherConfig()
cnf.Publisher = &steadyrabbit.PublisherConfig{
PublishConfirmation: true,
Exchange: &steadyrabbit.ExchangeConfig{
ExchangeDeclare: true,
ExchangeType: amqp.ExchangeDirect,
ExchangeName: "shahin",
ExchangeDurable: false,
ExchangeAutoDelete: false,
},
}
p, err := steadyrabbit.NewPublisher(cnf)
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
})
})
Context("with unreliable connection", func() {
It("should reconnect & publish", func() {
cnf := formPublisherConfig()
p, err := steadyrabbit.NewPublisher(cnf)
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
// Write an error to the NotifyCloseChan
p.GetNotifyCloseChannel() <- &amqp.Error{
Code: 0,
Reason: "Test failure",
Server: false,
Recover: false,
}
err = p.Publish(context.Background(), "shahin", []byte("shahin"))
Expect(err).ToNot(HaveOccurred())
})
})
})
Describe("Close", func() {
Context("when publisher is closed", func() {
It("should error when try to publish", func() {
p, err := steadyrabbit.NewPublisher(cnf)
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
p.Close()
err = p.Publish(context.Background(), "shahin", []byte("shahin"))
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(steadyrabbit.ErrConnectionClosed))
})
})
})
Describe("Performance", func() {
Context("Performance Benchmark: ", func() {
cnf = formPublisherConfig()
p, err = steadyrabbit.NewPublisher(cnf)
Expect(err).ToNot(HaveOccurred())
Expect(p).ToNot(BeNil())
Measure("it should be able to publish 5000 message per sec", func(b Benchmarker) {
runtime := b.Time("runtime", func() {
for i := 0; i < 5000; i++ {
err = p.Publish(context.Background(), "shahin", []byte("shahin"))
Expect(err).ToNot(HaveOccurred())
}
})
Expect(runtime.Seconds()).Should(BeNumerically("<", 1), "5000 message publish shouldn't take too long.")
//b.RecordValue("disk usage (in MB)", HowMuchDiskSpaceDidYouUse())
time.Sleep(1 * time.Second)
}, 10)
})
})
})