@@ -247,11 +247,11 @@ async function createIframeWithPowerSyncClient(
247247 */
248248function createMultipleTabsTest ( vfs ?: WASQLiteVFS ) {
249249 const vfsName = vfs || 'IndexedDB' ;
250- describe ( `Multiple Tabs with Iframes (${ vfsName } )` , { sequential : true , timeout : 120000 } , ( ) => {
250+ describe ( `Multiple Tabs with Iframes (${ vfsName } )` , { sequential : true , timeout : 20_000 } , ( ) => {
251251 const dbFilename = `test-multi-tab-${ uuid ( ) } .db` ;
252252
253253 // Configurable number of tabs to create (excluding the long-lived tab)
254- const NUM_TABS = 10 ;
254+ const NUM_TABS = 50 ;
255255
256256 it ( 'should handle simultaneous close and reopen of tabs' , async ( ) => {
257257 // Step 1: Open a long-lived reference tab that stays open throughout the test
@@ -299,13 +299,20 @@ function createMultipleTabsTest(vfs?: WASQLiteVFS) {
299299 }
300300 expect ( longLivedTab . iframe . isConnected ) . toBe ( true ) ;
301301
302- // Step 3: Simultaneously close half of the tabs (simulating abrupt closure)
303- const halfCount = Math . floor ( NUM_TABS / 2 ) ;
304- // Close the latest opened tabs since we usually use the last connected tabs for operations.
305- const tabsToClose = tabs . slice ( halfCount ) ;
306- const tabsToKeep = tabs . slice ( 0 , halfCount ) ;
302+ // Step 3: Simultaneously close the first and last quarters of the tabs (simulating abrupt closure)
303+ const quarterCount = Math . floor ( NUM_TABS / 4 ) ;
304+ const firstQuarterEnd = quarterCount ;
305+ const lastQuarterStart = NUM_TABS - quarterCount ;
307306
308- // Close half the tabs simultaneously (without proper cleanup)
307+ // Close the first quarter and last quarter of tabs
308+ const firstQuarter = tabs . slice ( 0 , firstQuarterEnd ) ;
309+ const lastQuarter = tabs . slice ( lastQuarterStart ) ;
310+ const tabsToClose = [ ...firstQuarter , ...lastQuarter ] ;
311+
312+ // Keep the middle two quarters
313+ const tabsToKeep = tabs . slice ( firstQuarterEnd , lastQuarterStart ) ;
314+
315+ // Close the first and last quarters of tabs simultaneously (without proper cleanup)
309316 // Do this in reverse order to ensure the last connected tab is closed first.
310317 const closePromises = tabsToClose . reverse ( ) . map ( ( tab ) => tab . cleanup ( ) ) ;
311318 await Promise . all ( closePromises ) ;
@@ -324,8 +331,13 @@ function createMultipleTabsTest(vfs?: WASQLiteVFS) {
324331
325332 // Step 4: Reopen the closed tabs
326333 const reopenedTabs : IframeClient [ ] = [ ] ;
327- const reopenPromises = tabsToClose . map ( async ( _ , index ) => {
328- const identifier = tabIdentifiers [ index ] ;
334+ // Get the identifiers for the closed tabs by finding their indices in the original tabs array
335+ const closedTabIdentifiers = tabsToClose . map ( ( closedTab ) => {
336+ const index = tabs . indexOf ( closedTab ) ;
337+ return tabIdentifiers [ index ] ;
338+ } ) ;
339+
340+ const reopenPromises = closedTabIdentifiers . map ( async ( identifier ) => {
329341 const result = await createIframeWithPowerSyncClient ( dbFilename , identifier , vfs ) ;
330342 reopenedTabs . push ( result ) ;
331343
@@ -367,6 +379,11 @@ function createMultipleTabsTest(vfs?: WASQLiteVFS) {
367379 expect ( queryResult . length ) . toBe ( 1 ) ;
368380 expect ( ( queryResult [ 0 ] as { value : number } ) . value ) . toBe ( 1 ) ;
369381
382+ /**
383+ * Wait a little for the state to settle.
384+ */
385+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
386+
370387 // Step 6: Create a new tab which should trigger a connect. The shared sync worker should reconnect.
371388 // This ensures the shared sync worker is not stuck and is properly handling new connections
372389 const newTabIdentifier = `new-tab-${ Date . now ( ) } ` ;
0 commit comments