Skip to content

Commit ab861ec

Browse files
committed
review comments
1 parent b119b13 commit ab861ec

File tree

3 files changed

+67
-61
lines changed

3 files changed

+67
-61
lines changed

packages/sqlite_async/lib/src/native/database/connection_pool.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
9292

9393
if (_availableReadConnections.isEmpty &&
9494
_runningWithAllConnectionsCount > 0) {
95-
// Wait until withAllConnections is done
95+
// Wait until [withAllConnections] is done. Otherwise we could spawn a new
96+
// reader while the user is configuring all the connections,
97+
// e.g. a global open factory configuration shared across all connections.
9698
return;
9799
}
98100

packages/sqlite_async/test/basic_test.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import 'dart:async';
2+
import 'dart:math';
3+
import 'package:collection/collection.dart';
24
import 'package:sqlite_async/sqlite3_common.dart';
35
import 'package:sqlite_async/sqlite_async.dart';
46
import 'package:test/test.dart';
@@ -301,6 +303,68 @@ void main() {
301303
'Web locks are managed with a shared worker, which does not support timeouts',
302304
)
303305
});
306+
307+
test('with all connections', () async {
308+
// TODO: Is this right?
309+
final maxReaders = _isDart2Wasm ? 0 : 3;
310+
311+
final db = SqliteDatabase.withFactory(
312+
await testUtils.testFactory(path: path),
313+
maxReaders: maxReaders,
314+
);
315+
await db.initialize();
316+
await createTables(db);
317+
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+
324+
// 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+
);
328+
329+
bool finishedWithAllConns = false;
330+
331+
late Future<void> readsCalledWhileWithAllConnsRunning;
332+
333+
print("${DateTime.now()} start");
334+
await db.withAllConnections((writer, readers) async {
335+
expect(readers.length, maxReaders);
336+
337+
// Run some reads during the block that they should run after the block finishes and releases
338+
// 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);
365+
366+
await readsCalledWhileWithAllConnsRunning;
367+
});
304368
});
305369
}
306370

packages/sqlite_async/test/native/basic_test.dart

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import 'dart:async';
55
import 'dart:io';
66
import 'dart:math';
77

8-
import 'package:collection/collection.dart';
98
import 'package:path/path.dart' show join;
109
import 'package:sqlite3/common.dart' as sqlite;
11-
import 'package:sqlite3/sqlite3.dart' show Row;
1210
import 'package:sqlite_async/sqlite_async.dart';
1311
import 'package:test/test.dart';
1412

@@ -105,64 +103,6 @@ void main() {
105103
print("${DateTime.now()} done");
106104
});
107105

108-
test('with all connections', () async {
109-
final db = SqliteDatabase.withFactory(
110-
await testUtils.testFactory(path: path),
111-
maxReaders: 3);
112-
await db.initialize();
113-
await createTables(db);
114-
115-
Future<Row> readWithRandomDelay(SqliteReadContext ctx, int id) async {
116-
return await ctx.get(
117-
'SELECT ? as i, test_sleep(?) as sleep, test_connection_name() as connection',
118-
[id, 5 + Random().nextInt(10)]);
119-
}
120-
121-
// Warm up to spawn the max readers
122-
await Future.wait(
123-
[1, 2, 3, 4, 5, 6, 7, 8].map((i) => readWithRandomDelay(db, i)),
124-
);
125-
126-
bool finishedWithAllConns = false;
127-
128-
late Future<void> readsCalledWhileWithAllConnsRunning;
129-
130-
print("${DateTime.now()} start");
131-
await db.withAllConnections((writer, readers) async {
132-
expect(readers.length, 3);
133-
134-
// Run some reads during the block that they should run after the block finishes and releases
135-
// all locks
136-
readsCalledWhileWithAllConnsRunning = Future.wait(
137-
[1, 2, 3, 4, 5, 6, 7, 8].map((i) async {
138-
final r = await db.readLock((c) async {
139-
expect(finishedWithAllConns, isTrue);
140-
return await readWithRandomDelay(c, i);
141-
});
142-
print(
143-
"${DateTime.now()} After withAllConnections, started while running $r");
144-
}),
145-
);
146-
147-
await Future.wait([
148-
writer.execute(
149-
"INSERT OR REPLACE INTO test_data(id, description) SELECT ? as i, test_sleep(?) || ' ' || test_connection_name() || ' 1 ' || datetime() as connection RETURNING *",
150-
[
151-
123,
152-
5 + Random().nextInt(20)
153-
]).then((value) =>
154-
print("${DateTime.now()} withAllConnections writer done $value")),
155-
...readers
156-
.mapIndexed((i, r) => readWithRandomDelay(r, i).then((results) {
157-
print(
158-
"${DateTime.now()} withAllConnections readers done $results");
159-
}))
160-
]);
161-
}).then((_) => finishedWithAllConns = true);
162-
163-
await readsCalledWhileWithAllConnsRunning;
164-
});
165-
166106
test('prevent opening new readers while in withAllConnections', () async {
167107
final sharedStateDir = Directory.systemTemp.createTempSync();
168108
addTearDown(() => sharedStateDir.deleteSync(recursive: true));

0 commit comments

Comments
 (0)