Skip to content

Commit 74075c5

Browse files
committed
Add tests
1 parent 7778347 commit 74075c5

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

packages/sqlite_async/lib/src/utils/shared_utils.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ extension ThrottledUpdates on CommonDatabase {
133133
final wrapped = _UpdateListener(listener);
134134
addListener(wrapped);
135135

136+
listener.onResume = wrapped.addPending;
136137
listener.onCancel = () => removeListener(wrapped);
137138
},
138139
isBroadcast: true,
@@ -149,6 +150,12 @@ class _UpdateListener {
149150
void notify(Set<String> pendingUpdates) {
150151
buffered.addAll(pendingUpdates);
151152
if (!downstream.isPaused) {
153+
addPending();
154+
}
155+
}
156+
157+
void addPending() {
158+
if (buffered.isNotEmpty) {
152159
downstream.add(buffered);
153160
buffered = {};
154161
}

packages/sqlite_async/test/native/watch_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:math';
77

88
import 'package:sqlite3/common.dart';
99
import 'package:sqlite_async/sqlite_async.dart';
10+
import 'package:sqlite_async/src/utils/shared_utils.dart';
1011
import 'package:test/test.dart';
1112

1213
import '../utils/test_utils_impl.dart';
@@ -31,6 +32,51 @@ void main() {
3132
return db;
3233
});
3334

35+
test('raw update notifications', () async {
36+
final factory = await testUtils.testFactory(path: path);
37+
final db = factory
38+
.openDB(SqliteOpenOptions(primaryConnection: true, readOnly: false));
39+
40+
db.execute('CREATE TABLE a (bar INTEGER);');
41+
db.execute('CREATE TABLE b (bar INTEGER);');
42+
final events = <Set<String>>[];
43+
final subscription = db.updatedTables.listen(events.add);
44+
45+
db.execute('insert into a default values');
46+
expect(events, isEmpty); // should be async
47+
await pumpEventQueue();
48+
expect(events.removeLast(), {'a'});
49+
50+
db.execute('begin');
51+
db.execute('insert into a default values');
52+
db.execute('insert into b default values');
53+
await pumpEventQueue();
54+
expect(events, isEmpty); // should only trigger on commit
55+
db.execute('commit');
56+
57+
await pumpEventQueue();
58+
expect(events.removeLast(), {'a', 'b'});
59+
60+
db.execute('begin');
61+
db.execute('insert into a default values');
62+
db.execute('rollback');
63+
expect(events, isEmpty);
64+
await pumpEventQueue();
65+
expect(events, isEmpty); // should ignore cancelled transactions
66+
67+
// Should still listen during pause, and dispatch on resume
68+
subscription.pause();
69+
db.execute('insert into a default values');
70+
await pumpEventQueue();
71+
expect(events, isEmpty);
72+
73+
subscription.resume();
74+
await pumpEventQueue();
75+
expect(events.removeLast(), {'a'});
76+
77+
subscription.pause();
78+
});
79+
3480
test('watch in isolate', () async {
3581
final db = await testUtils.setupDatabase(path: path);
3682
await createTables(db);

0 commit comments

Comments
 (0)