@@ -79,6 +79,22 @@ describe('RPC request coalescer', () => {
7979 const expectationB = expect ( responsePromiseB ) . rejects . toBe ( mockErrorB ) ;
8080 await Promise . all ( [ expectationA , expectationB ] ) ;
8181 } ) ;
82+ it ( 'does not abort the transport when the number of consumers increases, falls to zero, then increases again in the same runloop' , async ( ) => {
83+ expect . assertions ( 2 ) ;
84+ const abortControllerA = new AbortController ( ) ;
85+ const abortControllerB = new AbortController ( ) ;
86+ coalescedTransport ( { payload : null , signal : abortControllerA . signal } ) . catch ( ( ) => { } ) ;
87+ coalescedTransport ( { payload : null , signal : abortControllerB . signal } ) . catch ( ( ) => { } ) ;
88+ // Both abort, bringing the consumer count to zero.
89+ abortControllerA . abort ( 'o no A' ) ;
90+ abortControllerB . abort ( 'o no B' ) ;
91+ // New request comes in at the last moment before the end of the runloop.
92+ coalescedTransport ( { payload : null } ) ;
93+ await jest . runOnlyPendingTimersAsync ( ) ;
94+ expect ( mockTransport ) . toHaveBeenCalledTimes ( 1 ) ;
95+ const transportAbortSignal = mockTransport . mock . lastCall ! [ 0 ] . signal ! ;
96+ expect ( transportAbortSignal . aborted ) . toBe ( false ) ;
97+ } ) ;
8298 describe ( 'multiple coalesced requests each with an abort signal' , ( ) => {
8399 let abortControllerA : AbortController ;
84100 let abortControllerB : AbortController ;
@@ -120,9 +136,13 @@ describe('RPC request coalescer', () => {
120136 abortControllerA . abort ( 'o no' ) ;
121137 await expect ( responsePromiseA ) . rejects . toBe ( 'o no' ) ;
122138 } ) ;
123- it ( 'aborts the transport when all of the requests abort' , ( ) => {
139+ it ( 'aborts the transport at the end of the runloop when all of the requests abort' , async ( ) => {
140+ expect . assertions ( 1 ) ;
141+ responsePromiseA . catch ( ( ) => { } ) ;
142+ responsePromiseB . catch ( ( ) => { } ) ;
124143 abortControllerA . abort ( 'o no A' ) ;
125144 abortControllerB . abort ( 'o no B' ) ;
145+ await jest . runOnlyPendingTimersAsync ( ) ;
126146 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
127147 const transportAbortSignal = mockTransport . mock . lastCall ! [ 0 ] . signal ! ;
128148 expect ( transportAbortSignal . aborted ) . toBe ( true ) ;
0 commit comments