Skip to content

Commit 1d33dc8

Browse files
committed
add tests
1 parent ab861ec commit 1d33dc8

File tree

2 files changed

+83
-40
lines changed

2 files changed

+83
-40
lines changed

packages/sqlite_async/test/basic_test.dart

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import 'dart:async';
2-
import 'dart:math';
3-
import 'package:collection/collection.dart';
42
import 'package:sqlite_async/sqlite3_common.dart';
53
import 'package:sqlite_async/sqlite_async.dart';
64
import 'package:test/test.dart';
@@ -9,6 +7,7 @@ import 'utils/test_utils_impl.dart';
97

108
final testUtils = TestUtils();
119
const _isDart2Wasm = bool.fromEnvironment('dart.tool.dart2wasm');
10+
const _isWeb = identical(0, 0.0) || _isDart2Wasm;
1211

1312
void 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
});

packages/sqlite_async/test/native/basic_test.dart

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

8+
import 'package:collection/collection.dart';
89
import 'package:path/path.dart' show join;
910
import 'package:sqlite3/common.dart' as sqlite;
1011
import 'package:sqlite_async/sqlite_async.dart';
@@ -161,6 +162,68 @@ void main() {
161162
expect(readerInfo, {'state': 'updated'});
162163
});
163164

165+
test('with all connections', () async {
166+
final maxReaders = 3;
167+
168+
final db = SqliteDatabase.withFactory(
169+
await testUtils.testFactory(path: path),
170+
maxReaders: maxReaders,
171+
);
172+
await db.initialize();
173+
await createTables(db);
174+
175+
Future<sqlite.Row> readWithRandomDelay(
176+
SqliteReadContext ctx, int id) async {
177+
return await ctx.get(
178+
'SELECT ? as i, test_sleep(?) as sleep, test_connection_name() as connection',
179+
[id, 5 + Random().nextInt(10)]);
180+
}
181+
182+
// Warm up to spawn the max readers
183+
await Future.wait(
184+
[1, 2, 3, 4, 5, 6, 7, 8].map((i) => readWithRandomDelay(db, i)),
185+
);
186+
187+
bool finishedWithAllConns = false;
188+
189+
late Future<void> readsCalledWhileWithAllConnsRunning;
190+
191+
print("${DateTime.now()} start");
192+
await db.withAllConnections((writer, readers) async {
193+
expect(readers.length, maxReaders);
194+
195+
// Run some reads during the block that they should run after the block finishes and releases
196+
// all locks
197+
readsCalledWhileWithAllConnsRunning = Future.wait(
198+
[1, 2, 3, 4, 5, 6, 7, 8].map((i) async {
199+
final r = await db.readLock((c) async {
200+
expect(finishedWithAllConns, isTrue);
201+
return await readWithRandomDelay(c, i);
202+
});
203+
print(
204+
"${DateTime.now()} After withAllConnections, started while running $r");
205+
}),
206+
);
207+
208+
await Future.wait([
209+
writer.execute(
210+
"INSERT OR REPLACE INTO test_data(id, description) SELECT ? as i, test_sleep(?) || ' ' || test_connection_name() || ' 1 ' || datetime() as connection RETURNING *",
211+
[
212+
123,
213+
5 + Random().nextInt(20)
214+
]).then((value) =>
215+
print("${DateTime.now()} withAllConnections writer done $value")),
216+
...readers
217+
.mapIndexed((i, r) => readWithRandomDelay(r, i).then((results) {
218+
print(
219+
"${DateTime.now()} withAllConnections readers done $results");
220+
}))
221+
]);
222+
}).then((_) => finishedWithAllConns = true);
223+
224+
await readsCalledWhileWithAllConnsRunning;
225+
});
226+
164227
test('read-only transactions', () async {
165228
final db = await testUtils.setupDatabase(path: path);
166229
await createTables(db);

0 commit comments

Comments
 (0)