Skip to content

Commit b9fb48f

Browse files
committed
Fix sqlite web integration tests
1 parent 7ecc4cc commit b9fb48f

File tree

5 files changed

+49
-26
lines changed

5 files changed

+49
-26
lines changed

sqlite3_web/lib/src/database.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ abstract class WebSqlite {
184184
return DatabaseClient(worker, wasmModule);
185185
}
186186

187+
/// Connects to an endpoint previously obtained with [Database.additionalConnection].
188+
///
189+
/// As a [SqliteWebEndpoint] record only consists of fields that are
190+
/// transferrable in JavaScript, these endpoints can be sent to other workers,
191+
/// which can then call [connectToPort] to open a database connection
192+
/// originally established by another JavaScript connection.
193+
///
194+
/// Note that, depending on the access mode, the returned [Database] may only
195+
/// be valid as long as the original [Database] where [Database.additionalConnection]
196+
/// was called. This limitation does not exist for databases hosted by shared
197+
/// workers.
187198
static Future<Database> connectToPort(SqliteWebEndpoint endpoint) {
188199
final client = DatabaseClient(Uri.base, Uri.base);
189200
return client.connectToExisting(endpoint);

sqlite3_web/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: sqlite3_web
22
description: Utilities to simplify accessing sqlite3 on the web, with automated feature detection.
3-
version: 0.1.2-wip
3+
version: 0.1.3
44
homepage: https://github.com/simolus3/sqlite3.dart/tree/main/sqlite3_web
55
repository: https://github.com/simolus3/sqlite3.dart
66

sqlite3_web/test/integration_test.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ void main() {
103103
},
104104
);
105105

106+
rawDriver.logs.get(LogType.browser).listen((entry) {
107+
print('[console]: ${entry.message}');
108+
});
109+
106110
driver = TestWebDriver(server, rawDriver);
107111
await driver.driver.get('http://localhost:8080/');
108112

@@ -136,9 +140,9 @@ void main() {
136140
test('$storage through $access', () async {
137141
await driver.openDatabase((storage, access));
138142
await driver.execute('CREATE TABLE foo (bar TEXT);');
139-
final event = driver.waitForUpdate();
143+
expect(await driver.countUpdateEvents(), 0);
140144
await driver.execute("INSERT INTO foo (bar) VALUES ('hello');");
141-
await event;
145+
expect(await driver.countUpdateEvents(), 1);
142146
});
143147
}
144148
});

sqlite3_web/tool/server.dart

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,29 +146,33 @@ class TestWebDriver {
146146
final res = await driver
147147
.executeAsync('open(arguments[0], arguments[1])', [desc]) as String?;
148148

149-
if (res == null) {
150-
return implementation!;
151-
} else {
152-
// If we're using connectToRecommended, this returns the storage/access
153-
// mode actually chosen.
154-
final split = res.split(':');
155-
156-
return (
157-
StorageMode.values.byName(split[0]),
158-
AccessMode.values.byName(split[1])
159-
);
160-
}
149+
// This returns the storage/access mode actually chosen.
150+
final split = res!.split(':');
151+
152+
return (
153+
StorageMode.values.byName(split[0]),
154+
AccessMode.values.byName(split[1])
155+
);
161156
}
162157

163158
Future<void> closeDatabase() async {
164159
await driver.executeAsync("close('', arguments[0])", []);
165160
}
166161

167-
Future<void> waitForUpdate() async {
168-
await driver.executeAsync('wait_for_update("", arguments[0])', []);
162+
Future<int> countUpdateEvents() async {
163+
final result =
164+
await driver.executeAsync('get_updates("", arguments[0])', []);
165+
return result as int;
169166
}
170167

171168
Future<void> execute(String sql) async {
172169
await driver.executeAsync('exec(arguments[0], arguments[1])', [sql]);
173170
}
171+
172+
Future<void> testSecond(String sql) async {
173+
final res = await driver.executeAsync('test_second("", arguments[0])', []);
174+
if (res != true) {
175+
throw 'test_second failed! More information may be available in the console.';
176+
}
177+
}
174178
}

sqlite3_web/web/main.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:html';
33
import 'dart:js_interop';
44
import 'dart:js_interop_unsafe';
55

6-
import 'package:async/async.dart';
76
import 'package:sqlite3_web/sqlite3_web.dart';
87

98
final sqlite3WasmUri = Uri.parse('sqlite3.wasm');
@@ -13,17 +12,27 @@ const databaseName = 'database';
1312
WebSqlite? webSqlite;
1413

1514
Database? database;
16-
StreamQueue<void>? updates;
15+
int updates = 0;
1716

1817
void main() {
1918
_addCallbackForWebDriver('detectImplementations', _detectImplementations);
2019
_addCallbackForWebDriver('close', (arg) async {
2120
await database?.dispose();
2221
return null;
2322
});
24-
_addCallbackForWebDriver('wait_for_update', _waitForUpdate);
23+
_addCallbackForWebDriver('get_updates', (arg) async {
24+
return updates.toJS;
25+
});
2526
_addCallbackForWebDriver('open', _open);
2627
_addCallbackForWebDriver('exec', _exec);
28+
_addCallbackForWebDriver('test_second', (arg) async {
29+
final endpoint = await database!.additionalConnection();
30+
final second = await WebSqlite.connectToPort(endpoint);
31+
32+
await second.execute('SELECT 1');
33+
await second.dispose();
34+
return true.toJS;
35+
});
2736

2837
document.getElementById('selfcheck')?.onClick.listen((event) async {
2938
print('starting');
@@ -79,11 +88,6 @@ Future<JSString> _detectImplementations(String? _) async {
7988
}).toJS;
8089
}
8190

82-
Future<JSAny?> _waitForUpdate(String? _) async {
83-
await updates!.next;
84-
return null;
85-
}
86-
8791
Future<JSAny?> _open(String? implementationName) async {
8892
final sqlite = initializeSqlite();
8993
Database db;
@@ -103,7 +107,7 @@ Future<JSAny?> _open(String? implementationName) async {
103107
// Make sure it works!
104108
await db.select('SELECT database_host()');
105109

106-
updates = StreamQueue(db.updates);
110+
db.updates.listen((_) => updates++);
107111
database = db;
108112
return returnValue?.toJS;
109113
}

0 commit comments

Comments
 (0)