11// Qa Lab tests cover bus server plugin behavior.
22import { Agent , createServer , request } from "node:http" ;
3+ import { setTimeout as sleep } from "node:timers/promises" ;
34import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
45import { closeQaHttpServer , handleQaBusRequest , startQaBusServer } from "./bus-server.js" ;
56import { createQaBusState } from "./bus-state.js" ;
@@ -111,10 +112,15 @@ describe("qa-bus server", () => {
111112 await Promise . all ( stops . splice ( 0 ) . map ( ( stop ) => stop ( ) ) ) ;
112113 } ) ;
113114
114- it ( "wakes stale-cursor long polls as soon as matching account traffic arrives " , async ( ) => {
115+ it ( "wakes matching polls and fences late polls and writes during shutdown " , async ( ) => {
115116 const state = createQaBusState ( ) ;
116117 const bus = await startQaBusServer ( { state } ) ;
117- stops . push ( bus [ "stop" ] ) ;
118+ let stopped = false ;
119+ stops . push ( async ( ) => {
120+ if ( ! stopped ) {
121+ await bus . stop ( ) ;
122+ }
123+ } ) ;
118124
119125 const pending = pollQaBus ( {
120126 baseUrl : bus . baseUrl ,
@@ -137,6 +143,69 @@ describe("qa-bus server", () => {
137143 cursor : 1 ,
138144 kind : "inbound-message" ,
139145 } ) ;
146+
147+ const waitForCursorAdvance = state . waitForCursorAdvance . bind ( state ) ;
148+ let pollWaitersStarted = 0 ;
149+ let pollWaitersSettled = 0 ;
150+ state . waitForCursorAdvance = async ( ...args ) => {
151+ pollWaitersStarted += 1 ;
152+ try {
153+ return await waitForCursorAdvance ( ...args ) ;
154+ } finally {
155+ pollWaitersSettled += 1 ;
156+ }
157+ } ;
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 ) ) ;
164+ const body = JSON . stringify ( { accountId : "late-body" , cursor : 0 , timeoutMs : 30_000 } ) ;
165+ const slowPoll = request ( {
166+ host : "127.0.0.1" ,
167+ port : bus . port ,
168+ path : "/v1/poll" ,
169+ method : "POST" ,
170+ headers : { "content-type" : "application/json" , "content-length" : Buffer . byteLength ( body ) } ,
171+ } ) ;
172+ slowPoll . on ( "response" , ( response ) => response . resume ( ) ) ;
173+ slowPoll . on ( "error" , ( ) => undefined ) ;
174+ 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 ) ) ;
193+ await sleep ( 10 ) ;
194+
195+ stopped = true ;
196+ const startedAt = Date . now ( ) ;
197+ const stopping = bus . stop ( ) ;
198+ setTimeout ( ( ) => {
199+ slowPoll . end ( body . slice ( 1 ) ) ;
200+ slowInbound . end ( inboundBody . slice ( 1 ) ) ;
201+ } , 50 ) ;
202+ await stopping ;
203+ await activePoll ;
204+
205+ expect ( Date . now ( ) - startedAt ) . toBeLessThan ( 1_000 ) ;
206+ expect ( pollWaitersStarted ) . toBe ( 1 ) ;
207+ expect ( pollWaitersSettled ) . toBe ( 1 ) ;
208+ expect ( state . getSnapshot ( ) ) . toMatchObject ( { events : [ ] , messages : [ ] } ) ;
140209 } ) ;
141210
142211 it ( "resumes an account after its last acknowledged cursor when the client restarts" , async ( ) => {
0 commit comments