-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbasic_test.dart
441 lines (379 loc) · 14.7 KB
/
basic_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import 'dart:async';
import 'dart:math';
import 'package:sqlite3/sqlite3.dart' as sqlite;
import 'package:sqlite_async/mutex.dart';
import 'package:sqlite_async/sqlite_async.dart';
import 'package:test/expect.dart';
import 'package:test/test.dart';
import 'util.dart';
void main() {
group('Basic Tests', () {
late String path;
setUp(() async {
path = dbPath();
await cleanDb(path: path);
});
tearDown(() async {
await cleanDb(path: path);
});
createTables(SqliteDatabase db) async {
await db.writeTransaction((tx) async {
await tx.execute(
'CREATE TABLE test_data(id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT)');
});
}
test('Basic Setup', () async {
final db = await setupDatabase(path: path);
await createTables(db);
await db.execute(
'INSERT INTO test_data(description) VALUES(?)', ['Test Data']);
final result = await db.get('SELECT description FROM test_data');
expect(result, equals({'description': 'Test Data'}));
expect(
await db.execute('PRAGMA journal_mode'),
equals([
{'journal_mode': 'wal'}
]));
expect(
await db.execute('PRAGMA locking_mode'),
equals([
{'locking_mode': 'normal'}
]));
});
// Manually verified
test('Concurrency', () async {
final db =
SqliteDatabase.withFactory(testFactory(path: path), maxReaders: 3);
await db.initialize();
await createTables(db);
print("${DateTime.now()} start");
var futures = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((i) => db.get(
'SELECT ? as i, test_sleep(?) as sleep, test_connection_name() as connection',
[i, 5 + Random().nextInt(10)]));
await for (var result in Stream.fromFutures(futures)) {
print("${DateTime.now()} $result");
}
});
test('Concurrency 2', () async {
final db1 =
SqliteDatabase.withFactory(testFactory(path: path), maxReaders: 3);
final db2 =
SqliteDatabase.withFactory(testFactory(path: path), maxReaders: 3);
await db1.initialize();
await createTables(db1);
await db2.initialize();
print("${DateTime.now()} start");
var futures1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((i) {
return db1.execute(
"INSERT OR REPLACE INTO test_data(id, description) SELECT ? as i, test_sleep(?) || ' ' || test_connection_name() || ' 1 ' || datetime() as connection RETURNING *",
[
i,
5 + Random().nextInt(20)
]).then((value) => print("${DateTime.now()} $value"));
}).toList();
var futures2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map((i) {
return db2.execute(
"INSERT OR REPLACE INTO test_data(id, description) SELECT ? as i, test_sleep(?) || ' ' || test_connection_name() || ' 2 ' || datetime() as connection RETURNING *",
[
i,
5 + Random().nextInt(20)
]).then((value) => print("${DateTime.now()} $value"));
}).toList();
await Future.wait(futures1);
await Future.wait(futures2);
print("${DateTime.now()} done");
});
test('read-only transactions', () async {
final db = await setupDatabase(path: path);
await createTables(db);
// Can read
await db.getAll("WITH test AS (SELECT 1 AS one) SELECT * FROM test");
// Cannot write
await expectLater(() async {
await db
.getAll('INSERT INTO test_data(description) VALUES(?)', ['test']);
},
throwsA((e) =>
e is sqlite.SqliteException &&
e.message
.contains('attempt to write in a read-only transaction')));
// Can use WITH ... SELECT
await db.getAll("WITH test AS (SELECT 1 AS one) SELECT * FROM test");
// Cannot use WITH .... INSERT
await expectLater(() async {
await db.getAll(
"WITH test AS (SELECT 1 AS one) INSERT INTO test_data(description) SELECT one FROM test");
},
throwsA((e) =>
e is sqlite.SqliteException &&
e.message
.contains('attempt to write in a read-only transaction')));
await db.writeTransaction((tx) async {
// Within a write transaction, this is fine
await tx.getAll(
'INSERT INTO test_data(description) VALUES(?) RETURNING *',
['test']);
});
});
test('should not allow direct db calls within a transaction callback',
() async {
final db = await setupDatabase(path: path);
await createTables(db);
await db.writeTransaction((tx) async {
await expectLater(() async {
await db.execute(
'INSERT INTO test_data(description) VALUES(?)', ['test']);
}, throwsA((e) => e is LockError && e.message.contains('tx.execute')));
});
});
test('should not allow read-only db calls within transaction callback',
() async {
final db = await setupDatabase(path: path);
await createTables(db);
await db.writeTransaction((tx) async {
// This uses a different connection, so it _could_ work.
// But it's likely unintentional and could cause weird bugs, so we don't
// allow it by default.
await expectLater(() async {
await db.getAll('SELECT * FROM test_data');
}, throwsA((e) => e is LockError && e.message.contains('tx.getAll')));
});
await db.readTransaction((tx) async {
// This does actually attempt a lock on the same connection, so it
// errors.
// This also exposes an interesting test case where the read transaction
// opens another connection, but doesn't use it.
await expectLater(() async {
await db.getAll('SELECT * FROM test_data');
}, throwsA((e) => e is LockError && e.message.contains('tx.getAll')));
});
});
test('should not allow read-only db calls within lock callback', () async {
final db = await setupDatabase(path: path);
await createTables(db);
// Locks - should behave the same as transactions above
await db.writeLock((tx) async {
await expectLater(() async {
await db.getOptional('SELECT * FROM test_data');
},
throwsA(
(e) => e is LockError && e.message.contains('tx.getOptional')));
});
await db.readLock((tx) async {
await expectLater(() async {
await db.getOptional('SELECT * FROM test_data');
},
throwsA(
(e) => e is LockError && e.message.contains('tx.getOptional')));
});
});
test(
'should allow read-only db calls within transaction callback in separate zone',
() async {
final db = await setupDatabase(path: path);
await createTables(db);
// Get a reference to the parent zone (outside the transaction).
final zone = Zone.current;
// Each of these are fine, since it could use a separate connection.
// Note: In highly concurrent cases, it could exhaust the connection pool and cause a deadlock.
await db.writeTransaction((tx) async {
// Use the parent zone to avoid the "recursive lock" error.
await zone.fork().run(() async {
await db.getAll('SELECT * FROM test_data');
});
});
await db.readTransaction((tx) async {
await zone.fork().run(() async {
await db.getAll('SELECT * FROM test_data');
});
});
await db.readTransaction((tx) async {
await zone.fork().run(() async {
await db.execute('SELECT * FROM test_data');
});
});
// Note: This would deadlock, since it shares a global write lock.
// await db.writeTransaction((tx) async {
// await zone.fork().run(() async {
// await db.execute('SELECT * FROM test_data');
// });
// });
});
test('should allow PRAMGAs', () async {
final db = await setupDatabase(path: path);
await createTables(db);
// Not allowed in transactions, but does work as a direct statement.
await db.execute('PRAGMA wal_checkpoint(TRUNCATE)');
await db.execute('VACUUM');
});
test('should allow ignoring errors', () async {
final db = await setupDatabase(path: path);
await createTables(db);
ignore(db.execute(
'INSERT INTO test_data(description) VALUES(json(?))', ['test3']));
});
test('should properly report errors in transactions', () async {
final db = await setupDatabase(path: path);
await createTables(db);
var tp = db.writeTransaction((tx) async {
await tx.execute(
'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)',
[1, 'test1']);
await tx.execute(
'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)',
[2, 'test2']);
expect(await tx.getAutoCommit(), equals(false));
try {
await tx.execute(
'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)',
[2, 'test3']);
} catch (e) {
// Ignore
}
expect(await tx.getAutoCommit(), equals(true));
expect(tx.closed, equals(false));
// Will not be executed because of the above rollback
ignore(tx.execute(
'INSERT OR ROLLBACK INTO test_data(id, description) VALUES(?, ?)',
[4, 'test4']));
});
// The error propagates up to the transaction
await expectLater(
tp,
throwsA((e) =>
e is sqlite.SqliteException &&
e.message
.contains('Transaction rolled back by earlier statement') &&
e.message.contains('UNIQUE constraint failed')));
expect(await db.get('SELECT count() count FROM test_data'),
equals({'count': 0}));
// Check that we can open another transaction afterwards
await db.writeTransaction((tx) async {});
});
test('should error on dangling transactions', () async {
final db = await setupDatabase(path: path);
await createTables(db);
await expectLater(() async {
await db.execute('BEGIN');
}, throwsA((e) => e is sqlite.SqliteException));
});
test('should handle normal errors', () async {
final db = await setupDatabase(path: path);
await createTables(db);
Error? caughtError;
final syntheticError = ArgumentError('foobar');
await db.computeWithDatabase<void>((db) async {
throw syntheticError;
}).catchError((error) {
caughtError = error;
});
expect(caughtError.toString(), equals(syntheticError.toString()));
// Check that we can still continue afterwards
final computed = await db.computeWithDatabase((db) async {
return 5;
});
expect(computed, equals(5));
});
test('should handle uncaught errors', () async {
final db = await setupDatabase(path: path);
await createTables(db);
Object? caughtError;
await db.computeWithDatabase<void>((db) async {
Future<void> asyncCompute() async {
throw ArgumentError('uncaught async error');
}
asyncCompute();
}).catchError((error) {
caughtError = error;
});
// The specific error message may change
expect(
caughtError.toString(),
equals(
"IsolateError in sqlite-writer: Invalid argument(s): uncaught async error"));
// Check that we can still continue afterwards
final computed = await db.computeWithDatabase((db) async {
return 5;
});
expect(computed, equals(5));
});
test('should handle uncaught errors in read connections', () async {
final db = await setupDatabase(path: path);
await createTables(db);
for (var i = 0; i < 10; i++) {
Object? caughtError;
await db.readTransaction((ctx) async {
await ctx.computeWithDatabase((db) async {
Future<void> asyncCompute() async {
throw ArgumentError('uncaught async error');
}
asyncCompute();
});
}).catchError((error) {
caughtError = error;
});
// The specific message may change
expect(
caughtError.toString(),
matches(RegExp(
r'IsolateError in sqlite-\d+: Invalid argument\(s\): uncaught async error')));
}
// Check that we can still continue afterwards
final computed = await db.readTransaction((ctx) async {
return await ctx.computeWithDatabase((db) async {
return 5;
});
});
expect(computed, equals(5));
});
test('should allow resuming transaction after errors', () async {
final db = await setupDatabase(path: path);
await createTables(db);
SqliteWriteContext? savedTx;
await db.writeTransaction((tx) async {
savedTx = tx;
var caught = false;
try {
// This error does not rollback the transaction
await tx.execute('NOT A VALID STATEMENT');
} catch (e) {
// Ignore
caught = true;
}
expect(caught, equals(true));
expect(await tx.getAutoCommit(), equals(false));
expect(tx.closed, equals(false));
final rs = await tx.execute(
'INSERT INTO test_data(description) VALUES(?) RETURNING description',
['Test Data']);
expect(rs.rows[0], equals(['Test Data']));
});
expect(await savedTx!.getAutoCommit(), equals(true));
expect(savedTx!.closed, equals(true));
});
test('closing', () async {
// Test race condition in SqliteConnectionPool:
// 1. Open two concurrent queries, which opens two connection.
// 2. Second connection takes longer to open than first.
// 3. Call db.close().
// 4. Now second connection is ready. Second query has two connections to choose from.
// 5. However, first connection is closed, so it's removed from the pool.
// 6. Triggers `Concurrent modification during iteration: Instance(length:1) of '_GrowableList'`
final db =
SqliteDatabase.withFactory(testFactory(path: path, initStatements: [
// Second connection to sleep more than first connection
'SELECT test_sleep(test_connection_number() * 10)'
]));
await db.initialize();
final future1 = db.get('SELECT test_sleep(10) as sleep');
final future2 = db.get('SELECT test_sleep(10) as sleep');
await db.close();
await future1;
await future2;
});
});
}
// For some reason, future.ignore() doesn't actually ignore errors in these tests.
void ignore(Future future) {
future.then((_) {}, onError: (_) {});
}