@@ -5,16 +5,27 @@ import (
55 "github.com/Azure/go-amqp"
66 . "github.com/onsi/ginkgo/v2"
77 . "github.com/onsi/gomega"
8- test_helper "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/test-helper"
8+ testhelper "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/test-helper"
99 "time"
1010)
1111
1212var _ = Describe ("Recovery connection test" , func () {
13- It ("connection should reconnect if dropped by via REST API" , func () {
14- containerID := "connection should reconnect if dropped by via REST API"
13+ It ("connection should reconnect producers and consumers if dropped by via REST API" , func () {
14+ /*
15+ The test is a bit complex since it requires to drop the connection by REST API
16+ Then wait for the connection to be reconnected.
17+ The scope of the test is to verify that the connection is reconnected and the
18+ producers and consumers are able to send and receive messages.
19+ It is more like an integration test.
20+ This kind of the tests requires time in terms of execution it has to wait for the
21+ connection to be reconnected, so to speed up the test I aggregated the tests in one.
22+ */
23+
24+ name := "connection should reconnect producers and consumers if dropped by via REST API"
1525 connection , err := Dial (context .Background (), []string {"amqp://" }, & AmqpConnOptions {
1626 SASLType : amqp .SASLTypeAnonymous (),
17- ContainerID : containerID ,
27+ ContainerID : name ,
28+ // reduced the reconnect interval to speed up the test
1829 RecoveryConfiguration : & RecoveryConfiguration {
1930 ActiveRecovery : true ,
2031 BackOffReconnectInterval : 2 * time .Second ,
@@ -25,19 +36,77 @@ var _ = Describe("Recovery connection test", func() {
2536 ch := make (chan * StateChanged , 1 )
2637 connection .NotifyStatusChange (ch )
2738
39+ qName := generateName (name )
40+ queueInfo , err := connection .Management ().DeclareQueue (context .Background (), & QuorumQueueSpecification {
41+ Name : qName ,
42+ })
43+ Expect (err ).To (BeNil ())
44+ Expect (queueInfo ).NotTo (BeNil ())
45+
46+ publisher , err := connection .NewPublisher (context .Background (), & QueueAddress {
47+ Queue : qName ,
48+ }, "test" )
49+
50+ Expect (err ).To (BeNil ())
51+ Expect (publisher ).NotTo (BeNil ())
52+ for i := 0 ; i < 5 ; i ++ {
53+ publishResult , err := publisher .Publish (context .Background (), amqp .NewMessage ([]byte ("Hello" )))
54+ Expect (err ).To (BeNil ())
55+ Expect (publishResult ).NotTo (BeNil ())
56+ Expect (publishResult .Outcome ).To (Equal (& amqp.StateAccepted {}))
57+ }
58+
2859 Eventually (func () bool {
29- err := test_helper .DropConnectionContainerID (containerID )
60+ err := testhelper .DropConnectionContainerID (name )
3061 return err == nil
3162 }).WithTimeout (5 * time .Second ).WithPolling (400 * time .Millisecond ).Should (BeTrue ())
32- <- ch
33- time .Sleep (2 * time .Second )
63+ st1 := <- ch
64+ Expect (st1 .From ).To (Equal (& StateOpen {}))
65+ Expect (st1 .To ).To (BeAssignableToTypeOf (& StateClosed {}))
66+ /// Closed state should have an error
67+ // Since it is forced closed by the REST API
68+ err = st1 .To .(* StateClosed ).GetError ()
69+ Expect (err ).NotTo (BeNil ())
70+ Expect (err .Error ()).To (ContainSubstring ("Connection forced" ))
71+
72+ time .Sleep (1 * time .Second )
3473 Eventually (func () bool {
35- conn , err := test_helper .GetConnectionByContainerID (containerID )
74+ conn , err := testhelper .GetConnectionByContainerID (name )
3675 return err == nil && conn != nil
3776 }).WithTimeout (5 * time .Second ).WithPolling (400 * time .Millisecond ).Should (BeTrue ())
38- <- ch
77+ st2 := <- ch
78+ Expect (st2 .From ).To (BeAssignableToTypeOf (& StateClosed {}))
79+ Expect (st2 .To ).To (Equal (& StateReconnecting {}))
80+
81+ st3 := <- ch
82+ Expect (st3 .From ).To (BeAssignableToTypeOf (& StateReconnecting {}))
83+ Expect (st3 .To ).To (Equal (& StateOpen {}))
84+
85+ for i := 0 ; i < 5 ; i ++ {
86+ publishResult , err := publisher .Publish (context .Background (), amqp .NewMessage ([]byte ("Hello" )))
87+ Expect (err ).To (BeNil ())
88+ Expect (publishResult ).NotTo (BeNil ())
89+ Expect (publishResult .Outcome ).To (Equal (& amqp.StateAccepted {}))
90+ }
91+
92+ time .Sleep (500 * time .Millisecond )
93+ purged , err := connection .Management ().PurgeQueue (context .Background (), qName )
94+ Expect (err ).To (BeNil ())
95+ Expect (purged ).To (Equal (5 + 5 ))
96+
97+ Expect (connection .Management ().DeleteQueue (context .Background (), qName )).To (BeNil ())
98+
3999 err = connection .Close (context .Background ())
40- <- ch
100+ Expect (err ).To (BeNil ())
101+ st4 := <- ch
102+ Expect (st4 .From ).To (Equal (& StateOpen {}))
103+ Expect (st4 .To ).To (BeAssignableToTypeOf (& StateClosed {}))
104+ err = st4 .To .(* StateClosed ).GetError ()
105+ // the flow status should be:
106+ // from open to closed (with error)
107+ // from closed to reconnecting
108+ // from reconnecting to open
109+ // from open to closed (without error)
41110 Expect (err ).To (BeNil ())
42111 })
43112})
0 commit comments