@@ -112,7 +112,7 @@ describe("qa-bus server", () => {
112112 await Promise . all ( stops . splice ( 0 ) . map ( ( stop ) => stop ( ) ) ) ;
113113 } ) ;
114114
115- it ( "wakes matching long polls and settles late polls during shutdown" , async ( ) => {
115+ it ( "wakes matching polls and fences late polls and writes during shutdown" , async ( ) => {
116116 const state = createQaBusState ( ) ;
117117 const bus = await startQaBusServer ( { state } ) ;
118118 let stopped = false ;
@@ -145,16 +145,22 @@ describe("qa-bus server", () => {
145145 } ) ;
146146
147147 const waitForCursorAdvance = state . waitForCursorAdvance . bind ( state ) ;
148- let lateWaiterStarted = false ;
149- let lateWaiterSettled = false ;
148+ let pollWaitersStarted = 0 ;
149+ let pollWaitersSettled = 0 ;
150150 state . waitForCursorAdvance = async ( ...args ) => {
151- lateWaiterStarted = true ;
151+ pollWaitersStarted += 1 ;
152152 try {
153153 return await waitForCursorAdvance ( ...args ) ;
154154 } finally {
155- lateWaiterSettled = true ;
155+ pollWaitersSettled += 1 ;
156156 }
157157 } ;
158+ const activePoll = fetch ( `${ bus . baseUrl } /v1/poll` , {
159+ method : "POST" ,
160+ headers : { "content-type" : "application/json" } ,
161+ body : JSON . stringify ( { accountId : "active" , cursor : 1 , timeoutMs : 30_000 } ) ,
162+ } ) . catch ( ( ) => undefined ) ;
163+ await vi . waitFor ( ( ) => expect ( pollWaitersStarted ) . toBe ( 1 ) ) ;
158164 const body = JSON . stringify ( { accountId : "late-body" , cursor : 0 , timeoutMs : 30_000 } ) ;
159165 const slowPoll = request ( {
160166 host : "127.0.0.1" ,
@@ -166,17 +172,40 @@ describe("qa-bus server", () => {
166172 slowPoll . on ( "response" , ( response ) => response . resume ( ) ) ;
167173 slowPoll . on ( "error" , ( ) => undefined ) ;
168174 slowPoll . write ( body . slice ( 0 , 1 ) ) ;
175+ const inboundBody = JSON . stringify ( {
176+ conversation : { id : "late-room" , kind : "direct" } ,
177+ senderId : "late-sender" ,
178+ text : "must not survive shutdown" ,
179+ } ) ;
180+ const slowInbound = request ( {
181+ host : "127.0.0.1" ,
182+ port : bus . port ,
183+ path : "/v1/inbound/message" ,
184+ method : "POST" ,
185+ headers : {
186+ "content-type" : "application/json" ,
187+ "content-length" : Buffer . byteLength ( inboundBody ) ,
188+ } ,
189+ } ) ;
190+ slowInbound . on ( "response" , ( response ) => response . resume ( ) ) ;
191+ slowInbound . on ( "error" , ( ) => undefined ) ;
192+ slowInbound . write ( inboundBody . slice ( 0 , 1 ) ) ;
169193 await sleep ( 10 ) ;
170194
171195 stopped = true ;
172196 const startedAt = Date . now ( ) ;
173197 const stopping = bus . stop ( ) ;
174- setTimeout ( ( ) => slowPoll . end ( body . slice ( 1 ) ) , 50 ) ;
198+ setTimeout ( ( ) => {
199+ slowPoll . end ( body . slice ( 1 ) ) ;
200+ slowInbound . end ( inboundBody . slice ( 1 ) ) ;
201+ } , 50 ) ;
175202 await stopping ;
203+ await activePoll ;
176204
177205 expect ( Date . now ( ) - startedAt ) . toBeLessThan ( 1_000 ) ;
178- expect ( lateWaiterStarted ) . toBe ( true ) ;
179- expect ( lateWaiterSettled ) . toBe ( true ) ;
206+ expect ( pollWaitersStarted ) . toBe ( 1 ) ;
207+ expect ( pollWaitersSettled ) . toBe ( 1 ) ;
208+ expect ( state . getSnapshot ( ) ) . toMatchObject ( { events : [ ] , messages : [ ] } ) ;
180209 } ) ;
181210
182211 it ( "resumes an account after its last acknowledged cursor when the client restarts" , async ( ) => {
0 commit comments