@@ -66,11 +66,13 @@ func TestPublisher_Cancel_willNotBlock(t *testing.T) {
6666
6767func TestPublisher_serve (t * testing.T ) {
6868 var (
69- runSync = make (chan bool )
70- deleted bool
71- closed bool
72- notifyClose bool
73- testMsg * amqp.Publishing
69+ runSync = make (chan bool )
70+ deleted bool
71+ closed bool
72+ notifyClose bool
73+ exchangeName string
74+ routingKey string
75+ testMsg * amqp.Publishing
7476 )
7577
7678 p := newTestPublisher ()
@@ -90,6 +92,8 @@ func TestPublisher_serve(t *testing.T) {
9092 return errChan
9193 },
9294 _Publish : func (ex string , key string , mandatory bool , immediate bool , msg amqp.Publishing ) error {
95+ exchangeName = ex
96+ routingKey = key
9397 testMsg = & msg
9498 return nil
9599 },
@@ -118,6 +122,85 @@ func TestPublisher_serve(t *testing.T) {
118122 t .Error ("should close channel" )
119123 }
120124
125+ if exchangeName != "exchange.name" {
126+ t .Error ("should set correct routing key" )
127+ }
128+
129+ if routingKey != "routing.key" {
130+ t .Error ("should set correct routing key" )
131+ }
132+
133+ if bytes .Compare (testMsg .Body , []byte ("test1" )) != 0 {
134+ t .Error ("should publish correct messaged" )
135+ }
136+ }
137+
138+ func TestPublisher_serve_customRoutingKey (t * testing.T ) {
139+ var (
140+ runSync = make (chan bool )
141+ deleted bool
142+ closed bool
143+ notifyClose bool
144+ exchangeName string
145+ routingKey string
146+ testMsg * amqp.Publishing
147+ )
148+
149+ p := newTestPublisher ()
150+ cli := & mqDeleterTest {
151+ _deletePublisher : func (* Publisher ) {
152+ deleted = true
153+ },
154+ }
155+
156+ ch1 := & mqChannelTest {
157+ _Close : func () error {
158+ closed = true
159+ return nil
160+ },
161+ _NotifyClose : func (errChan chan * amqp.Error ) chan * amqp.Error {
162+ notifyClose = true
163+ return errChan
164+ },
165+ _Publish : func (ex string , key string , mandatory bool , immediate bool , msg amqp.Publishing ) error {
166+ exchangeName = ex
167+ routingKey = key
168+ testMsg = & msg
169+ return nil
170+ },
171+ }
172+
173+ go func () {
174+ <- runSync
175+ p .serve (cli , ch1 )
176+ runSync <- true
177+ }()
178+
179+ runSync <- true
180+ p .PublishWithRoutingKey (amqp.Publishing {Body : []byte ("test1" )}, "my.routing.key" )
181+ p .Cancel ()
182+ <- runSync
183+
184+ if ! notifyClose {
185+ t .Error ("should register notifyClose" )
186+ }
187+
188+ if ! deleted {
189+ t .Error ("should delete publisher" )
190+ }
191+
192+ if ! closed {
193+ t .Error ("should close channel" )
194+ }
195+
196+ if exchangeName != "exchange.name" {
197+ t .Error ("should set correct routing key" )
198+ }
199+
200+ if routingKey != "my.routing.key" {
201+ t .Error ("should set correct routing key" )
202+ }
203+
121204 if bytes .Compare (testMsg .Body , []byte ("test1" )) != 0 {
122205 t .Error ("should publish correct messaged" )
123206 }
@@ -270,5 +353,5 @@ func TestPublishingTemplate(t *testing.T) {
270353}
271354
272355func newTestPublisher (opts ... PublisherOpt ) * Publisher {
273- return NewPublisher ("" , "" , opts ... )
356+ return NewPublisher ("exchange.name " , "routing.key " , opts ... )
274357}
0 commit comments