@@ -5,6 +5,7 @@ import { Rabbit } from "../support/rabbit"
55import { username , password , eventually , always } from "../support/util"
66import { randomUUID } from "crypto"
77import { Offset } from "../../src/requests/subscribe_request"
8+
89describe ( "connection closed callback" , ( ) => {
910 let client : Client | undefined = undefined
1011 const rabbit = new Rabbit ( username , password )
@@ -22,10 +23,10 @@ describe("connection closed callback", () => {
2223 afterEach ( async ( ) => {
2324 spySandbox ?. restore ( )
2425 try {
25- await client ?. close ( )
2626 await rabbit . deleteStream ( streamName )
27- await rabbit . closeAllConnections ( )
2827 await rabbit . deleteAllQueues ( { match : / m y - s t r e a m - / } )
28+ await client ?. close ( )
29+ await rabbit . closeAllConnections ( )
2930 } catch ( e ) { }
3031
3132 try {
@@ -65,55 +66,137 @@ describe("connection closed callback", () => {
6566 } , 1000 )
6667 } ) . timeout ( 5000 )
6768
68- it ( "if specified, is called also on publisher and consumer socket events " , async ( ) => {
69+ it ( "closed_connection listener is not invoked by the client " , async ( ) => {
6970 const listener = ( _hasError : boolean ) => {
7071 return
7172 }
72- const listenerSpy = spy ( listener )
73- client = await createClient ( username , password , { connection_closed : listenerSpy } )
74- await client . declarePublisher ( { stream : streamName , publisherRef, connectionClosedListener : listenerSpy } )
75- await client . declareConsumer (
76- { stream : streamName , consumerRef, offset : Offset . first ( ) , connectionClosedListener : listenerSpy } ,
73+ const clientListenerSpy = spy ( listener )
74+ client = await createClient ( username , password , { connection_closed : clientListenerSpy } )
75+
76+ await client . close ( )
77+
78+ await always ( ( ) => {
79+ expect ( clientListenerSpy ) . to . have . been . called . exactly ( 0 )
80+ } )
81+ } ) . timeout ( 5000 )
82+
83+ it ( "closed_connection listener is not invoked by the deletePublisher" , async ( ) => {
84+ const listener = ( _hasError : boolean ) => {
85+ return
86+ }
87+ const publisherListenerSpy = spy ( listener )
88+ client = await createClient ( username , password , { connection_closed : publisherListenerSpy } )
89+ const publisher = await client . declarePublisher ( {
90+ stream : streamName ,
91+ publisherRef,
92+ connectionClosedListener : publisherListenerSpy ,
93+ } )
94+
95+ await client . deletePublisher ( publisher . publisherId )
96+
97+ await always ( ( ) => {
98+ expect ( publisherListenerSpy ) . to . have . been . called . exactly ( 0 )
99+ } )
100+ } ) . timeout ( 5000 )
101+
102+ it ( "closed_connection listener is not invoked by the deleteConsumer" , async ( ) => {
103+ const listener = ( _hasError : boolean ) => {
104+ return
105+ }
106+ const consumerListenerSpy = spy ( listener )
107+ client = await createClient ( username , password , { connection_closed : consumerListenerSpy } )
108+ const consumer = await client . declareConsumer (
109+ { stream : streamName , consumerRef, offset : Offset . first ( ) , connectionClosedListener : consumerListenerSpy } ,
77110 ( _msg ) => {
78111 return
79112 }
80113 )
81114
82- await client . close ( )
115+ await client . closeConsumer ( consumer . consumerId )
83116
84- await eventually ( ( ) => {
85- expect ( listenerSpy ) . to . have . been . called . exactly ( 3 )
86- } , 1000 )
117+ await always ( ( ) => {
118+ expect ( consumerListenerSpy ) . to . have . been . called . exactly ( 0 )
119+ } )
87120 } ) . timeout ( 5000 )
88121
89- it ( "different callbacks for client, consumer and publisher are all called when connections close" , async ( ) => {
90- const instListener = ( ) => {
91- return ( _hasError : boolean ) => {
122+ it ( "closed_connection listener is not invoked by the client even with multiple publishers and consumers" , async ( ) => {
123+ const listener = ( _hasError : boolean ) => {
124+ return
125+ }
126+ const clientListenerSpy = spy ( listener )
127+ const publisherListenerSpy = spy ( listener )
128+ const consumerListenerSpy = spy ( listener )
129+ client = await createClient ( username , password , { connection_closed : clientListenerSpy } )
130+ await client . declarePublisher ( {
131+ stream : streamName ,
132+ publisherRef,
133+ connectionClosedListener : publisherListenerSpy ,
134+ } )
135+ await client . declarePublisher ( {
136+ stream : streamName ,
137+ publisherRef : `${ publisherRef } -1` ,
138+ connectionClosedListener : publisherListenerSpy ,
139+ } )
140+ await client . declareConsumer (
141+ { stream : streamName , consumerRef, offset : Offset . first ( ) , connectionClosedListener : consumerListenerSpy } ,
142+ ( _msg ) => {
92143 return
93144 }
94- }
95- const listenerClientSpy = spy ( instListener ( ) )
96- const listenerConsumerSpy = spy ( instListener ( ) )
97- const listenerPublisherSpy = spy ( instListener ( ) )
98- client = await createClient ( username , password , { connection_closed : listenerClientSpy } )
99- await client . declarePublisher ( { stream : streamName , publisherRef, connectionClosedListener : listenerConsumerSpy } )
145+ )
100146 await client . declareConsumer (
101- { stream : streamName , consumerRef, offset : Offset . first ( ) , connectionClosedListener : listenerPublisherSpy } ,
147+ {
148+ stream : streamName ,
149+ consumerRef : `${ consumerRef } -1` ,
150+ offset : Offset . first ( ) ,
151+ connectionClosedListener : consumerListenerSpy ,
152+ } ,
102153 ( _msg ) => {
103154 return
104155 }
105156 )
106157
107158 await client . close ( )
108159
109- await eventually ( ( ) => {
110- expect ( listenerClientSpy ) . to . have . been . called . exactly ( 1 )
111- } , 1000 )
112- await eventually ( ( ) => {
113- expect ( listenerConsumerSpy ) . to . have . been . called . exactly ( 1 )
114- } , 1000 )
115- await eventually ( ( ) => {
116- expect ( listenerPublisherSpy ) . to . have . been . called . exactly ( 1 )
117- } , 1000 )
160+ await always ( ( ) => {
161+ expect ( clientListenerSpy ) . to . have . been . called . exactly ( 0 )
162+ } )
163+ await always ( ( ) => {
164+ expect ( publisherListenerSpy ) . to . have . been . called . exactly ( 0 )
165+ } )
166+ await always ( ( ) => {
167+ expect ( consumerListenerSpy ) . to . have . been . called . exactly ( 0 )
168+ } )
118169 } ) . timeout ( 5000 )
170+
171+ it ( "closed_connection listener is invoked by the server if it closes the connection" , async ( ) => {
172+ const listener = ( _hasError : boolean ) => {
173+ return
174+ }
175+ const clientListenerSpy = spy ( listener )
176+ const publisherListenerSpy = spy ( listener )
177+ const consumerListenerSpy = spy ( listener )
178+ client = await createClient ( username , password , { connection_closed : clientListenerSpy } )
179+ await client . declarePublisher ( {
180+ stream : streamName ,
181+ publisherRef,
182+ connectionClosedListener : publisherListenerSpy ,
183+ } )
184+ await client . declareConsumer (
185+ { stream : streamName , consumerRef, offset : Offset . first ( ) , connectionClosedListener : consumerListenerSpy } ,
186+ ( _msg ) => {
187+ return
188+ }
189+ )
190+ await sleep ( 5000 )
191+
192+ await rabbit . closeAllConnections ( )
193+
194+ await eventually ( ( ) => {
195+ expect ( clientListenerSpy ) . to . have . been . called . exactly ( 1 )
196+ expect ( publisherListenerSpy ) . to . have . been . called . exactly ( 1 )
197+ expect ( consumerListenerSpy ) . to . have . been . called . exactly ( 1 )
198+ } , 5000 )
199+ } ) . timeout ( 10000 )
119200} )
201+
202+ const sleep = ( ms : number ) => new Promise ( ( r ) => setTimeout ( r , ms ) )
0 commit comments