11import 'dart:async' ;
2- import 'dart:math' ;
3- import 'package:collection/collection.dart' ;
42import 'package:sqlite_async/sqlite3_common.dart' ;
53import 'package:sqlite_async/sqlite_async.dart' ;
64import 'package:test/test.dart' ;
@@ -9,6 +7,7 @@ import 'utils/test_utils_impl.dart';
97
108final testUtils = TestUtils ();
119const _isDart2Wasm = bool .fromEnvironment ('dart.tool.dart2wasm' );
10+ const _isWeb = identical (0 , 0.0 ) || _isDart2Wasm;
1211
1312void main () {
1413 group ('Shared Basic Tests' , () {
@@ -305,8 +304,7 @@ void main() {
305304 });
306305
307306 test ('with all connections' , () async {
308- // TODO: Is this right?
309- final maxReaders = _isDart2Wasm ? 0 : 3 ;
307+ final maxReaders = _isWeb ? 0 : 3 ;
310308
311309 final db = SqliteDatabase .withFactory (
312310 await testUtils.testFactory (path: path),
@@ -315,53 +313,35 @@ void main() {
315313 await db.initialize ();
316314 await createTables (db);
317315
318- Future <Row > readWithRandomDelay (SqliteReadContext ctx, int id) async {
319- return await ctx.get (
320- 'SELECT ? as i, test_sleep(?) as sleep, test_connection_name() as connection' ,
321- [id, 5 + Random ().nextInt (10 )]);
322- }
323-
324316 // Warm up to spawn the max readers
325- await Future .wait (
326- [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ].map ((i) => readWithRandomDelay (db, i)),
327- );
317+ await Future .wait ([for (var i = 0 ; i < 10 ; i++ ) db.get ('SELECT $i ' )]);
328318
329319 bool finishedWithAllConns = false ;
330320
331321 late Future <void > readsCalledWhileWithAllConnsRunning;
332322
333- print ( "${ DateTime . now ()} start" ) ;
323+ final parentZone = Zone .current ;
334324 await db.withAllConnections ((writer, readers) async {
335325 expect (readers.length, maxReaders);
336326
337327 // Run some reads during the block that they should run after the block finishes and releases
338328 // all locks
339- readsCalledWhileWithAllConnsRunning = Future .wait (
340- [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ].map ((i) async {
341- final r = await db.readLock ((c) async {
342- expect (finishedWithAllConns, isTrue);
343- return await readWithRandomDelay (c, i);
344- });
345- print (
346- "${DateTime .now ()} After withAllConnections, started while running $r " );
347- }),
348- );
349-
350- await Future .wait ([
351- writer.execute (
352- "INSERT OR REPLACE INTO test_data(id, description) SELECT ? as i, test_sleep(?) || ' ' || test_connection_name() || ' 1 ' || datetime() as connection RETURNING *" ,
353- [
354- 123 ,
355- 5 + Random ().nextInt (20 )
356- ]).then ((value) =>
357- print ("${DateTime .now ()} withAllConnections writer done $value " )),
358- ...readers
359- .mapIndexed ((i, r) => readWithRandomDelay (r, i).then ((results) {
360- print (
361- "${DateTime .now ()} withAllConnections readers done $results " );
362- }))
363- ]);
364- }).then ((_) => finishedWithAllConns = true );
329+ // Need a root zone here to avoid recursive lock errors.
330+ readsCalledWhileWithAllConnsRunning =
331+ Future (parentZone.bindCallback (() async {
332+ await Future .wait (
333+ [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ].map ((i) async {
334+ await db.readLock ((c) async {
335+ expect (finishedWithAllConns, isTrue);
336+ await Future .delayed (const Duration (milliseconds: 100 ));
337+ });
338+ }),
339+ );
340+ }));
341+
342+ await Future .delayed (const Duration (milliseconds: 200 ));
343+ finishedWithAllConns = true ;
344+ });
365345
366346 await readsCalledWhileWithAllConnsRunning;
367347 });
0 commit comments