forked from rabbitmq/amqp091-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
1910 lines (1556 loc) · 46.1 KB
/
integration_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2021 VMware, Inc. or its affiliates. All Rights Reserved.
// Copyright (c) 2012-2021, Sean Treadway, SoundCloud Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build integration
package amqp091
import (
"bytes"
devrand "crypto/rand"
"encoding/binary"
"fmt"
"hash/crc32"
"io"
"math/rand"
"net"
"os"
"reflect"
"strconv"
"sync"
"testing"
"testing/quick"
"time"
)
func TestIntegrationOpenClose(t *testing.T) {
if c := integrationConnection(t, "open-close"); c != nil {
t.Logf("have connection, calling connection close")
if err := c.Close(); err != nil {
t.Fatalf("connection close: %s", err)
}
t.Logf("connection close OK")
}
}
func TestIntegrationOpenCloseChannel(t *testing.T) {
if c := integrationConnection(t, "channel"); c != nil {
defer c.Close()
ch, err := c.Channel()
if err != nil {
t.Fatalf("create channel 1: %s", err)
}
ch.Close()
}
}
func TestIntegrationHighChannelChurnInTightLoop(t *testing.T) {
if c := integrationConnection(t, "channel churn"); c != nil {
defer c.Close()
for i := 0; i < 1000; i++ {
ch, err := c.Channel()
if err != nil {
t.Fatalf("create channel 1: %s", err)
}
ch.Close()
}
}
}
func TestIntegrationOpenConfig(t *testing.T) {
config := Config{}
c, err := DialConfig(integrationURLFromEnv(), config)
if err != nil {
t.Fatalf("expected to dial with config %+v integration server: %s", config, err)
}
if _, err := c.Channel(); err != nil {
t.Errorf("expected to open channel: %s", err)
}
if err := c.Close(); err != nil {
t.Errorf("expected to close the connection: %s", err)
}
}
func TestIntegrationOpenConfigWithNetDial(t *testing.T) {
config := Config{Dial: net.Dial}
c, err := DialConfig(integrationURLFromEnv(), config)
if err != nil {
t.Fatalf("expected to dial with config %+v integration server: %s", config, err)
}
if _, err := c.Channel(); err != nil {
t.Errorf("expected to open channel: %s", err)
}
if err := c.Close(); err != nil {
t.Errorf("expected to close the connection: %s", err)
}
}
func TestIntegrationLocalAddr(t *testing.T) {
config := Config{}
c, err := DialConfig(integrationURLFromEnv(), config)
if err != nil {
t.Fatalf("expected to dial with config %+v integration server: %s", config, err)
}
defer c.Close()
a := c.LocalAddr()
_, portString, err := net.SplitHostPort(a.String())
if err != nil {
t.Errorf("expected to get a local network address with config %+v integration server: %s", config, a.String())
}
port, err := strconv.Atoi(portString)
if err != nil {
t.Errorf("expected to get a TCP port number with config %+v integration server: %s", config, err)
}
t.Logf("Connected to port %d\n", port)
}
// https://github.com/streadway/amqp/issues/94
func TestExchangePassiveOnMissingExchangeShouldError(t *testing.T) {
c := integrationConnection(t, "exch")
if c != nil {
defer c.Close()
ch, err := c.Channel()
if err != nil {
t.Fatalf("create channel 1: %s", err)
}
defer ch.Close()
if err := ch.ExchangeDeclarePassive(
"test-integration-missing-passive-exchange",
"direct", // type
false, // duration (note: is durable)
true, // auto-delete
false, // internal
false, // nowait
nil, // args
); err == nil {
t.Fatal("ExchangeDeclarePassive of a missing exchange should return error")
}
}
}
// https://github.com/streadway/amqp/issues/94
func TestIntegrationExchangeDeclarePassiveOnDeclaredShouldNotError(t *testing.T) {
c := integrationConnection(t, "exch")
if c != nil {
defer c.Close()
exchange := "test-integration-declared-passive-exchange"
ch, err := c.Channel()
if err != nil {
t.Fatalf("create channel: %s", err)
}
defer ch.Close()
if err := ch.ExchangeDeclare(
exchange, // name
"direct", // type
false, // durable
true, // auto-delete
false, // internal
false, // nowait
nil, // args
); err != nil {
t.Fatalf("declare exchange: %s", err)
}
if err := ch.ExchangeDeclarePassive(
exchange, // name
"direct", // type
false, // durable
true, // auto-delete
false, // internal
false, // nowait
nil, // args
); err != nil {
t.Fatalf("ExchangeDeclarePassive on a declared exchange should not error, got: %q", err)
}
}
}
func TestIntegrationExchange(t *testing.T) {
c := integrationConnection(t, "exch")
if c != nil {
defer c.Close()
channel, err := c.Channel()
if err != nil {
t.Fatalf("create channel: %s", err)
}
t.Logf("create channel OK")
exchange := "test-integration-exchange"
if err := channel.ExchangeDeclare(
exchange, // name
"direct", // type
false, // duration
true, // auto-delete
false, // internal
false, // nowait
nil, // args
); err != nil {
t.Fatalf("declare exchange: %s", err)
}
t.Logf("declare exchange OK")
if err := channel.ExchangeDelete(exchange, false, false); err != nil {
t.Fatalf("delete exchange: %s", err)
}
t.Logf("delete exchange OK")
if err := channel.Close(); err != nil {
t.Fatalf("close channel: %s", err)
}
t.Logf("close channel OK")
}
}
// https://github.com/streadway/amqp/issues/94
func TestIntegrationQueueDeclarePassiveOnMissingExchangeShouldError(t *testing.T) {
c := integrationConnection(t, "queue")
if c != nil {
defer c.Close()
ch, err := c.Channel()
if err != nil {
t.Fatalf("create channel1: %s", err)
}
defer ch.Close()
if _, err := ch.QueueDeclarePassive(
"test-integration-missing-passive-queue", // name
false, // duration (note: not durable)
true, // auto-delete
false, // exclusive
false, // noWait
nil, // arguments
); err == nil {
t.Fatal("QueueDeclarePassive of a missing queue should error")
}
}
}
// https://github.com/streadway/amqp/issues/94
func TestIntegrationPassiveQueue(t *testing.T) {
c := integrationConnection(t, "queue")
if c != nil {
defer c.Close()
name := "test-integration-declared-passive-queue"
ch, err := c.Channel()
if err != nil {
t.Fatalf("create channel1: %s", err)
}
defer ch.Close()
if _, err := ch.QueueDeclare(
name, // name
false, // durable
true, // auto-delete
false, // exclusive
false, // noWait
nil, // arguments
); err != nil {
t.Fatalf("queue declare: %s", err)
}
if _, err := ch.QueueDeclarePassive(
name, // name
false, // durable
true, // auto-delete
false, // exclusive
false, // noWait
nil, // arguments
); err != nil {
t.Fatalf("QueueDeclarePassive on declared queue should not error, got: %q", err)
}
if _, err := ch.QueueDeclarePassive(
name, // name
true, // durable (note: differs)
true, // auto-delete
false, // exclusive
false, // noWait
nil, // arguments
); err != nil {
t.Fatalf("QueueDeclarePassive on declared queue with different flags should error")
}
}
}
func TestIntegrationBasicQueueOperations(t *testing.T) {
c := integrationConnection(t, "queue")
if c != nil {
defer c.Close()
channel, err := c.Channel()
if err != nil {
t.Fatalf("create channel: %s", err)
}
t.Logf("create channel OK")
exchangeName := "test-basic-ops-exchange"
queueName := "test-basic-ops-queue"
deleteQueueFirstOptions := []bool{true, false}
for _, deleteQueueFirst := range deleteQueueFirstOptions {
if err := channel.ExchangeDeclare(
exchangeName, // name
"direct", // type
true, // duration (note: is durable)
false, // auto-delete
false, // internal
false, // nowait
nil, // args
); err != nil {
t.Fatalf("declare exchange: %s", err)
}
t.Logf("declare exchange OK")
if _, err := channel.QueueDeclare(
queueName, // name
true, // duration (note: durable)
false, // auto-delete
false, // exclusive
false, // noWait
nil, // arguments
); err != nil {
t.Fatalf("queue declare: %s", err)
}
t.Logf("declare queue OK")
if err := channel.QueueBind(
queueName, // name
"", // routingKey
exchangeName, // sourceExchange
false, // noWait
nil, // arguments
); err != nil {
t.Fatalf("queue bind: %s", err)
}
t.Logf("queue bind OK")
if deleteQueueFirst {
if _, err := channel.QueueDelete(
queueName, // name
false, // ifUnused (false=be aggressive)
false, // ifEmpty (false=be aggressive)
false, // noWait
); err != nil {
t.Fatalf("delete queue (first): %s", err)
}
t.Logf("delete queue (first) OK")
if err := channel.ExchangeDelete(exchangeName, false, false); err != nil {
t.Fatalf("delete exchange (after delete queue): %s", err)
}
t.Logf("delete exchange (after delete queue) OK")
} else { // deleteExchangeFirst
if err := channel.ExchangeDelete(exchangeName, false, false); err != nil {
t.Fatalf("delete exchange (first): %s", err)
}
t.Logf("delete exchange (first) OK")
if _, err := channel.QueueInspect(queueName); err != nil {
t.Fatalf("inspect queue state after deleting exchange: %s", err)
}
t.Logf("queue properly remains after exchange is deleted")
if _, err := channel.QueueDelete(
queueName,
false, // ifUnused
false, // ifEmpty
false, // noWait
); err != nil {
t.Fatalf("delete queue (after delete exchange): %s", err)
}
t.Logf("delete queue (after delete exchange) OK")
}
}
if err := channel.Close(); err != nil {
t.Fatalf("close channel: %s", err)
}
t.Logf("close channel OK")
}
}
func TestIntegrationConnectionNegotiatesMaxChannels(t *testing.T) {
config := Config{ChannelMax: 0}
c, err := DialConfig(integrationURLFromEnv(), config)
if err != nil {
t.Fatalf("expected to dial with config %+v integration server: %s", config, err)
}
defer c.Close()
if want, got := defaultChannelMax, c.Config.ChannelMax; want != got {
t.Errorf("expected connection to negotiate uint16 (%d) channels, got: %d", want, got)
}
}
func TestIntegrationConnectionNegotiatesClientMaxChannels(t *testing.T) {
config := Config{ChannelMax: 16}
c, err := DialConfig(integrationURLFromEnv(), config)
if err != nil {
t.Fatalf("expected to dial with config %+v integration server: %s", config, err)
}
defer c.Close()
if want, got := config.ChannelMax, c.Config.ChannelMax; want != got {
t.Errorf("expected client specified channel limit after handshake %d, got: %d", want, got)
}
}
func TestIntegrationChannelIDsExhausted(t *testing.T) {
config := Config{ChannelMax: 16}
c, err := DialConfig(integrationURLFromEnv(), config)
if err != nil {
t.Fatalf("expected to dial with config %+v integration server: %s", config, err)
}
defer c.Close()
for i := 1; i <= c.Config.ChannelMax; i++ {
if _, err := c.Channel(); err != nil {
t.Fatalf("expected allocating all channel ids to succed, failed on %d with %v", i, err)
}
}
if _, err := c.Channel(); err != ErrChannelMax {
t.Fatalf("expected allocating all channels to produce the client side error %#v, got: %#v", ErrChannelMax, err)
}
}
func TestIntegrationChannelClosing(t *testing.T) {
c := integrationConnection(t, "closings")
if c != nil {
defer c.Close()
// This function is run on every channel after it is successfully
// opened. It can do something to verify something. It should be
// quick; many channels may be opened!
f := func(t *testing.T, c *Channel) {
return
}
// open and close
channel, err := c.Channel()
if err != nil {
t.Fatalf("basic create channel: %s", err)
}
t.Logf("basic create channel OK")
if err := channel.Close(); err != nil {
t.Fatalf("basic close channel: %s", err)
}
t.Logf("basic close channel OK")
// deferred close
signal := make(chan bool)
go func() {
channel, err := c.Channel()
if err != nil {
t.Fatalf("second create channel: %s", err)
}
t.Logf("second create channel OK")
<-signal // a bit of synchronization
f(t, channel)
defer func() {
if err := channel.Close(); err != nil {
t.Fatalf("deferred close channel: %s", err)
}
t.Logf("deferred close channel OK")
signal <- true
}()
}()
signal <- true
select {
case <-signal:
t.Logf("(got close signal OK)")
break
case <-time.After(250 * time.Millisecond):
t.Fatalf("deferred close: timeout")
}
// multiple channels
for _, n := range []int{2, 4, 8, 16, 32, 64, 128, 256} {
channels := make([]*Channel, n)
for i := 0; i < n; i++ {
var err error
if channels[i], err = c.Channel(); err != nil {
t.Fatalf("create channel %d/%d: %s", i+1, n, err)
}
}
f(t, channel)
for i, channel := range channels {
if err := channel.Close(); err != nil {
t.Fatalf("close channel %d/%d: %s", i+1, n, err)
}
}
t.Logf("created/closed %d channels OK", n)
}
}
}
func TestIntegrationMeaningfulChannelErrors(t *testing.T) {
c := integrationConnection(t, "pub")
if c != nil {
defer c.Close()
ch, err := c.Channel()
if err != nil {
t.Fatalf("Could not create channel")
}
queue := "test.integration.channel.error"
_, err = ch.QueueDeclare(queue, false, true, false, false, nil)
if err != nil {
t.Fatalf("Could not declare")
}
_, err = ch.QueueDeclare(queue, true, false, false, false, nil)
if err == nil {
t.Fatalf("Expected error, got nil")
}
e, ok := err.(*Error)
if !ok {
t.Fatalf("Expected type Error response, got %T", err)
}
if e.Code != PreconditionFailed {
t.Fatalf("Expected PreconditionFailed, got: %+v", e)
}
_, err = ch.QueueDeclare(queue, false, true, false, false, nil)
if err != ErrClosed {
t.Fatalf("Expected channel to be closed, got: %T", err)
}
}
}
// https://github.com/streadway/amqp/issues/6
func TestIntegrationNonBlockingClose(t *testing.T) {
c := integrationConnection(t, "#6")
if c != nil {
defer c.Close()
ch, err := c.Channel()
if err != nil {
t.Fatalf("Could not create channel")
}
queue := "test.integration.blocking.close"
_, err = ch.QueueDeclare(queue, false, true, false, false, nil)
if err != nil {
t.Fatalf("Could not declare")
}
msgs, err := ch.Consume(queue, "", false, false, false, false, nil)
if err != nil {
t.Fatalf("Could not consume")
}
// Simulate a consumer
go func() {
for range msgs {
t.Logf("Oh my, received message on an empty queue")
}
}()
succeed := make(chan bool)
go func() {
if err = ch.Close(); err != nil {
t.Fatalf("Close produced an error when it shouldn't")
}
succeed <- true
}()
select {
case <-succeed:
break
case <-time.After(1 * time.Second):
t.Fatalf("Close timed out after 1s")
}
}
}
func TestIntegrationPublishConsume(t *testing.T) {
queue := "test.integration.publish.consume"
c1 := integrationConnection(t, "pub")
c2 := integrationConnection(t, "sub")
if c1 != nil && c2 != nil {
defer c1.Close()
defer c2.Close()
pub, _ := c1.Channel()
sub, _ := c2.Channel()
pub.QueueDeclare(queue, false, true, false, false, nil)
sub.QueueDeclare(queue, false, true, false, false, nil)
defer pub.QueueDelete(queue, false, false, false)
messages, _ := sub.Consume(queue, "", false, false, false, false, nil)
pub.Publish("", queue, false, false, Publishing{Body: []byte("pub 1")})
pub.Publish("", queue, false, false, Publishing{Body: []byte("pub 2")})
pub.Publish("", queue, false, false, Publishing{Body: []byte("pub 3")})
assertConsumeBody(t, messages, []byte("pub 1"))
assertConsumeBody(t, messages, []byte("pub 2"))
assertConsumeBody(t, messages, []byte("pub 3"))
}
}
func TestIntegrationConsumeFlow(t *testing.T) {
queue := "test.integration.consumer-flow"
c1 := integrationConnection(t, "pub-flow")
c2 := integrationConnection(t, "sub-flow")
if c1 != nil && c2 != nil {
defer c1.Close()
defer c2.Close()
pub, _ := c1.Channel()
sub, _ := c2.Channel()
pub.QueueDeclare(queue, false, true, false, false, nil)
sub.QueueDeclare(queue, false, true, false, false, nil)
defer pub.QueueDelete(queue, false, false, false)
sub.Qos(1, 0, false)
messages, _ := sub.Consume(queue, "", false, false, false, false, nil)
pub.Publish("", queue, false, false, Publishing{Body: []byte("pub 1")})
pub.Publish("", queue, false, false, Publishing{Body: []byte("pub 2")})
msg := assertConsumeBody(t, messages, []byte("pub 1"))
if err := sub.Flow(false); err.(*Error).Code == NotImplemented {
t.Log("flow control is not supported on this version of rabbitmq")
return
}
msg.Ack(false)
select {
case <-messages:
t.Fatalf("message was delivered when flow was not active")
default:
}
sub.Flow(true)
msg = assertConsumeBody(t, messages, []byte("pub 2"))
msg.Ack(false)
}
}
func TestIntegrationRecoverNotImplemented(t *testing.T) {
queue := "test.recover"
if c, ch := integrationQueue(t, queue); c != nil {
if product, ok := c.Properties["product"]; ok && product.(string) == "RabbitMQ" {
defer c.Close()
err := ch.Recover(false)
if ex, ok := err.(*Error); !ok || ex.Code != 540 {
t.Fatalf("Expected NOT IMPLEMENTED got: %v", ex)
}
}
}
}
// This test is driven by a private API to simulate the server sending a channelFlow message
func TestIntegrationPublishFlow(t *testing.T) {
// TODO - no idea how to test without affecting the server or mucking internal APIs
// i'd like to make sure the RW lock can be held by multiple publisher threads
// and that multiple channelFlow messages do not block the dispatch thread
}
func TestIntegrationConsumeCancel(t *testing.T) {
queue := "test.integration.consume-cancel"
c := integrationConnection(t, "pub")
if c != nil {
defer c.Close()
ch, _ := c.Channel()
ch.QueueDeclare(queue, false, true, false, false, nil)
defer ch.QueueDelete(queue, false, false, false)
messages, _ := ch.Consume(queue, "integration-tag", false, false, false, false, nil)
ch.Publish("", queue, false, false, Publishing{Body: []byte("1")})
assertConsumeBody(t, messages, []byte("1"))
err := ch.Cancel("integration-tag", false)
if err != nil {
t.Fatalf("error cancelling the consumer: %v", err)
}
ch.Publish("", queue, false, false, Publishing{Body: []byte("2")})
select {
case <-time.After(100 * time.Millisecond):
t.Fatalf("Timeout on Close")
case _, ok := <-messages:
if ok {
t.Fatalf("Extra message on consumer when consumer should have been closed")
}
}
}
}
func (c *Connection) Generate(r *rand.Rand, _ int) reflect.Value {
urlStr := os.Getenv("AMQP_URL")
if urlStr == "" {
return reflect.ValueOf(nil)
}
conn, err := Dial(urlStr)
if err != nil {
return reflect.ValueOf(nil)
}
return reflect.ValueOf(conn)
}
func (c Publishing) Generate(r *rand.Rand, _ int) reflect.Value {
var ok bool
var t reflect.Value
p := Publishing{}
//p.DeliveryMode = uint8(r.Intn(3))
//p.Priority = uint8(r.Intn(8))
if r.Intn(2) > 0 {
p.ContentType = "application/octet-stream"
}
if r.Intn(2) > 0 {
p.ContentEncoding = "gzip"
}
if r.Intn(2) > 0 {
p.CorrelationId = fmt.Sprintf("%d", r.Int())
}
if r.Intn(2) > 0 {
p.ReplyTo = fmt.Sprintf("%d", r.Int())
}
if r.Intn(2) > 0 {
p.MessageId = fmt.Sprintf("%d", r.Int())
}
if r.Intn(2) > 0 {
p.Type = fmt.Sprintf("%d", r.Int())
}
if r.Intn(2) > 0 {
p.AppId = fmt.Sprintf("%d", r.Int())
}
if r.Intn(2) > 0 {
p.Timestamp = time.Unix(r.Int63(), r.Int63())
}
if t, ok = quick.Value(reflect.TypeOf(p.Body), r); ok {
p.Body = t.Bytes()
}
return reflect.ValueOf(p)
}
func TestQuickPublishOnly(t *testing.T) {
if c := integrationConnection(t, "quick"); c != nil {
defer c.Close()
pub, err := c.Channel()
queue := "test-publish"
if _, err = pub.QueueDeclare(queue, false, true, false, false, nil); err != nil {
t.Errorf("Failed to declare: %s", err)
return
}
defer pub.QueueDelete(queue, false, false, false)
quick.Check(func(msg Publishing) bool {
return pub.Publish("", queue, false, false, msg) == nil
}, nil)
}
}
func TestPublishEmptyBody(t *testing.T) {
c := integrationConnection(t, "empty")
if c != nil {
defer c.Close()
ch, err := c.Channel()
if err != nil {
t.Errorf("Failed to create channel")
return
}
queue := "test-TestPublishEmptyBody"
if _, err := ch.QueueDeclare(queue, false, true, false, false, nil); err != nil {
t.Fatalf("Could not declare")
}
messages, err := ch.Consume(queue, "", false, false, false, false, nil)
if err != nil {
t.Fatalf("Could not consume")
}
err = ch.Publish("", queue, false, false, Publishing{})
if err != nil {
t.Fatalf("Could not publish")
}
select {
case msg := <-messages:
if len(msg.Body) != 0 {
t.Errorf("Received non empty body")
}
case <-time.After(200 * time.Millisecond):
t.Errorf("Timeout on receive")
}
}
}
func TestPublishEmptyBodyWithHeadersIssue67(t *testing.T) {
c := integrationConnection(t, "issue67")
if c != nil {
defer c.Close()
ch, err := c.Channel()
if err != nil {
t.Errorf("Failed to create channel")
return
}
queue := "test-TestPublishEmptyBodyWithHeaders"
if _, err := ch.QueueDeclare(queue, false, true, false, false, nil); err != nil {
t.Fatalf("Could not declare")
}
messages, err := ch.Consume(queue, "", false, false, false, false, nil)
if err != nil {
t.Fatalf("Could not consume")
}
headers := Table{
"ham": "spam",
}
err = ch.Publish("", queue, false, false, Publishing{Headers: headers})
if err != nil {
t.Fatalf("Could not publish")
}
select {
case msg := <-messages:
if msg.Headers["ham"] == nil {
t.Fatalf("Headers aren't sent")
}
if msg.Headers["ham"] != "spam" {
t.Fatalf("Headers are wrong")
}
case <-time.After(200 * time.Millisecond):
t.Errorf("Timeout on receive")
}
}
}
func TestQuickPublishConsumeOnly(t *testing.T) {
c1 := integrationConnection(t, "quick-pub")
c2 := integrationConnection(t, "quick-sub")
if c1 != nil && c2 != nil {
defer c1.Close()
defer c2.Close()
pub, err := c1.Channel()
sub, err := c2.Channel()
queue := "TestPublishConsumeOnly"
if _, err = pub.QueueDeclare(queue, false, true, false, false, nil); err != nil {
t.Errorf("Failed to declare: %s", err)
return
}
if _, err = sub.QueueDeclare(queue, false, true, false, false, nil); err != nil {
t.Errorf("Failed to declare: %s", err)
return
}
defer sub.QueueDelete(queue, false, false, false)
ch, err := sub.Consume(queue, "", false, false, false, false, nil)
if err != nil {
t.Errorf("Could not sub: %s", err)
}
quick.CheckEqual(
func(msg Publishing) []byte {
empty := Publishing{Body: msg.Body}
if pub.Publish("", queue, false, false, empty) != nil {
return []byte{'X'}
}
return msg.Body
},
func(msg Publishing) []byte {
out := <-ch
out.Ack(false)
return out.Body
},
nil)
}
}
func TestQuickPublishConsumeBigBody(t *testing.T) {
c1 := integrationConnection(t, "big-pub")
c2 := integrationConnection(t, "big-sub")
if c1 != nil && c2 != nil {
defer c1.Close()
defer c2.Close()
pub, err := c1.Channel()
sub, err := c2.Channel()
queue := "test-pubsub"
if _, err = sub.QueueDeclare(queue, false, true, false, false, nil); err != nil {
t.Errorf("Failed to declare: %s", err)
return
}
ch, err := sub.Consume(queue, "", false, false, false, false, nil)
if err != nil {
t.Errorf("Could not sub: %s", err)
}
fixture := Publishing{
Body: make([]byte, 1e4+1000),
}
if _, err = pub.QueueDeclare(queue, false, true, false, false, nil); err != nil {
t.Errorf("Failed to declare: %s", err)
return
}
err = pub.Publish("", queue, false, false, fixture)
if err != nil {
t.Errorf("Could not publish big body")
}
select {
case msg := <-ch:
if bytes.Compare(msg.Body, fixture.Body) != 0 {
t.Errorf("Consumed big body didn't match")
}
case <-time.After(200 * time.Millisecond):
t.Errorf("Timeout on receive")
}
}
}
func TestIntegrationGetOk(t *testing.T) {
if c := integrationConnection(t, "getok"); c != nil {
defer c.Close()