aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2025-01-31 16:25:18 +0000
committerstephan <stephan@noemail.net>2025-01-31 16:25:18 +0000
commitcb0da053edf42e314af7802e65200d85fce3729e (patch)
tree250deec16c3ba500f3409c860181c6dd3df53a12 /ext/wasm
parent654c94d68382493144a5c34e25937d56ba6fe0da (diff)
downloadsqlite-cb0da053edf42e314af7802e65200d85fce3729e.tar.gz
sqlite-cb0da053edf42e314af7802e65200d85fce3729e.zip
Add a small test app demonstrating cooperative semi-concurrency of the opfs-sahpool VFS using its un/pauseVfs() APIs.
FossilOrigin-Name: 09570c55a23e5af76dd2153a5b28a493f498d7d4a08b0089f3074d0a2c5d3d29
Diffstat (limited to 'ext/wasm')
-rw-r--r--ext/wasm/index.html4
-rw-r--r--ext/wasm/tests/opfs/sahpool/index.html31
-rw-r--r--ext/wasm/tests/opfs/sahpool/sahpool-pausing.js172
-rw-r--r--ext/wasm/tests/opfs/sahpool/sahpool-worker.js89
4 files changed, 296 insertions, 0 deletions
diff --git a/ext/wasm/index.html b/ext/wasm/index.html
index 5d53b62d4..78ff1d91a 100644
--- a/ext/wasm/index.html
+++ b/ext/wasm/index.html
@@ -119,6 +119,10 @@
<li><a href='tests/opfs/concurrency/index.html'>OPFS concurrency</a>
tests using multiple workers.
</li>
+ <li><a href='tests/opfs/sahpool/index.html'>OPFS SAHPool cooperative semi-concurrency</a>
+ demonstrates usage of the OPFS SAHPool VFS's "pause" feature to coordinate
+ access to a database.
+ </li>
</ul>
</li>
<li><strong>WASMFS</strong>-specific tests which require that
diff --git a/ext/wasm/tests/opfs/sahpool/index.html b/ext/wasm/tests/opfs/sahpool/index.html
new file mode 100644
index 000000000..f3d07f456
--- /dev/null
+++ b/ext/wasm/tests/opfs/sahpool/index.html
@@ -0,0 +1,31 @@
+<!doctype html>
+<html lang="en-us">
+ <head>
+ <meta charset="utf-8">
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
+ <link rel="stylesheet" href="../../../common/emscripten.css"/>
+ <link rel="stylesheet" href="../../../common/testing.css"/>
+ <title>sqlite3 tester: OpfsSAHPool Pausing</title>
+ <style></style>
+ </head>
+ <body><h1 id='color-target'></h1>
+
+ <p>
+ This page provides a <em>very basic</em> demonstration of
+ "pausing" and "unpausing" the OPFS SAHPool VFS such that
+ multiple pages or workers can use it by coordinating which
+ handler may have it open at any given time.
+ </p>
+ <div class='input-wrapper'>
+ <input type='checkbox' id='cb-log-reverse'>
+ <label for='cb-log-reverse'>Reverse log order?</label>
+ </div>
+ <div id='test-output'></div>
+ <script>(function(){
+ document.querySelector('h1').innerHTML =
+ document.querySelector('title').innerHTML;
+ })();</script>
+ <script src="sahpool-pausing.js"></script>
+ </body>
+</html>
diff --git a/ext/wasm/tests/opfs/sahpool/sahpool-pausing.js b/ext/wasm/tests/opfs/sahpool/sahpool-pausing.js
new file mode 100644
index 000000000..737b2e861
--- /dev/null
+++ b/ext/wasm/tests/opfs/sahpool/sahpool-pausing.js
@@ -0,0 +1,172 @@
+/*
+ 2022-10-12
+
+ The author disclaims copyright to this source code. In place of a
+ legal notice, here is a blessing:
+
+ * May you do good and not evil.
+ * May you find forgiveness for yourself and forgive others.
+ * May you share freely, never taking more than you give.
+
+ ***********************************************************************
+
+ These tests are specific to the opfs-sahpool VFS and are limited to
+ demonstrating its pause/unpause capabilities.
+*/
+'use strict';
+(function(self){
+ let logClass;
+
+ const mapToString = (v)=>{
+ switch(typeof v){
+ case 'number': case 'string': case 'boolean':
+ case 'undefined': case 'bigint':
+ return ''+v;
+ default: break;
+ }
+ if(null===v) return 'null';
+ if(v instanceof Error){
+ v = {
+ message: v.message,
+ stack: v.stack,
+ errorClass: v.name
+ };
+ }
+ return JSON.stringify(v,undefined,2);
+ };
+ const normalizeArgs = (args)=>args.map(mapToString);
+ console.log("Running in the UI thread.");
+ const logTarget = document.querySelector('#test-output');
+ logClass = function(cssClass,...args){
+ const ln = document.createElement('div');
+ if(cssClass){
+ for(const c of (Array.isArray(cssClass) ? cssClass : [cssClass])){
+ ln.classList.add(c);
+ }
+ }
+ ln.append(document.createTextNode(normalizeArgs(args).join(' ')));
+ logTarget.append(ln);
+ };
+ const cbReverse = document.querySelector('#cb-log-reverse');
+ //cbReverse.setAttribute('checked','checked');
+ const cbReverseKey = 'tester1:cb-log-reverse';
+ const cbReverseIt = ()=>{
+ logTarget.classList[cbReverse.checked ? 'add' : 'remove']('reverse');
+ //localStorage.setItem(cbReverseKey, cbReverse.checked ? 1 : 0);
+ };
+ cbReverse.addEventListener('change', cbReverseIt, true);
+ /*if(localStorage.getItem(cbReverseKey)){
+ cbReverse.checked = !!(+localStorage.getItem(cbReverseKey));
+ }*/
+ cbReverseIt();
+
+ const log = (...args)=>{
+ //console.log(...args);
+ logClass('',...args);
+ }
+ const warn = (...args)=>{
+ console.warn(...args);
+ logClass('warning',...args);
+ }
+ const error = (...args)=>{
+ console.error(...args);
+ logClass('error',...args);
+ };
+
+ const toss = (...args)=>{
+ error(...args);
+ throw new Error(args.join(' '));
+ };
+
+ const nextHandlerQueue = [];
+
+ const endOfWork = (passed=true)=>{
+ const eH = document.querySelector('#color-target');
+ const eT = document.querySelector('title');
+ if(passed){
+ log("End of work chain. If you made it this far, you win.");
+ eH.innerText = 'PASS: '+eH.innerText;
+ eH.classList.add('tests-pass');
+ eT.innerText = 'PASS: '+eT.innerText;
+ }else{
+ eH.innerText = 'FAIL: '+eH.innerText;
+ eH.classList.add('tests-fail');
+ eT.innerText = 'FAIL: '+eT.innerText;
+ }
+ };
+
+ const nextHandler = function(workerId,...msg){
+ log(workerId,...msg);
+ (nextHandlerQueue.shift())();
+ };
+
+ const postThen = function(W, msgType, callback){
+ nextHandlerQueue.push(callback);
+ W.postMessage({type:msgType});
+ };
+
+ const runTriangleOfDeath = function(W1, W2){
+ postThen(W1, 'vfs-acquire', function(){
+ postThen(W1, 'db-init', function(){
+ postThen(W1, 'db-query', function(){
+ postThen(W1, 'vfs-pause', function(){
+ postThen(W2, 'vfs-acquire', function(){
+ postThen(W2, 'db-query', function(){
+ postThen(W2, 'vfs-remove', endOfWork);
+ });
+ });
+ });
+ });
+ });
+ });
+ };
+
+ const runTests = function(){
+ log("Running sahpool pausing tests...");
+ const wjs = 'sahpool-worker.js?sqlite3.dir=../../../jswasm';
+ const W1 = new Worker(wjs+'&workerId=w1'),
+ W2 = new Worker(wjs+'&workerId=w2');
+ W1.workerId = 'w1';
+ W2.workerId = 'w2';
+ let initCount = 0;
+ const onmessage = function({data}){
+ //log("onmessage:",data);
+ switch(data.type){
+ case 'vfs-acquired':
+ nextHandler(data.workerId, "VFS acquired");
+ break;
+ case 'vfs-paused':
+ nextHandler(data.workerId, "VFS paused");
+ break;
+ case 'vfs-unpaused':
+ nextHandler(data.workerId, 'VFS unpaused');
+ break;
+ case 'vfs-removed':
+ nextHandler(data.workerId, 'VFS removed');
+ break;
+ case 'db-inited':
+ nextHandler(data.workerId, 'db initialized');
+ break;
+ case 'query-result':
+ nextHandler(data.workerId, 'query result', data.payload);
+ break;
+ case 'log':
+ log(data.workerId, ':', ...data.payload);
+ break;
+ case 'error':
+ error(data.workerId, ':', ...data.payload);
+ endOfWork(false);
+ break;
+ case 'initialized':
+ log(data.workerId, ': Worker initialized',...data.payload);
+ if( 2===++initCount ){
+ runTriangleOfDeath(W1, W2);
+ }
+ break;
+ }
+ };
+ W1.onmessage = W2.onmessage = onmessage;
+ };
+
+ runTests();
+})(globalThis);
diff --git a/ext/wasm/tests/opfs/sahpool/sahpool-worker.js b/ext/wasm/tests/opfs/sahpool/sahpool-worker.js
new file mode 100644
index 000000000..11815f1bf
--- /dev/null
+++ b/ext/wasm/tests/opfs/sahpool/sahpool-worker.js
@@ -0,0 +1,89 @@
+const searchParams = new URL(self.location.href).searchParams;
+const workerId = searchParams.get('workerId');
+const wPost = (type,...args)=>postMessage({type, workerId, payload:args});
+const log = (...args)=>wPost('log',...args);
+let capi, wasm, S, poolUtil;
+
+const sahPoolConfig = {
+ name: 'opfs-sahpool-pausable',
+ clearOnInit: false,
+ initialCapacity: 3
+};
+
+importScripts(searchParams.get('sqlite3.dir') + '/sqlite3.js');
+
+const sqlExec = function(sql){
+ const db = new poolUtil.OpfsSAHPoolDb('/my.db');
+ try{
+ return db.exec(sql);
+ }finally{
+ db.close();
+ }
+};
+
+const clog = console.log.bind(console);
+globalThis.onmessage = function({data}){
+ clog(workerId+": onmessage:",data);
+ switch(data.type){
+ case 'vfs-acquire':
+ if( poolUtil ){
+ poolUtil.unpauseVfs().then(()=>wPost('vfs-unpaused'));
+ }else{
+ S.installOpfsSAHPoolVfs(sahPoolConfig).then(pu=>{
+ poolUtil = pu;
+ wPost('vfs-acquired');
+ });
+ }
+ break;
+ case 'db-init':
+ try{
+ sqlExec([
+ "DROP TABLE IF EXISTS mytable;",
+ "CREATE TABLE mytable(a);",
+ "INSERT INTO mytable(a) VALUES(11),(22),(33)"
+ ]);
+ wPost('db-inited');
+ }catch(e){
+ wPost('error',e.message);
+ }
+ break;
+ case 'db-query': {
+ const rc = sqlExec({
+ sql: 'select * from mytable order by a',
+ rowMode: 'array',
+ returnValue: 'resultRows'
+ });
+ wPost('query-result',rc);
+ break;
+ }
+ case 'vfs-remove':
+ poolUtil.removeVfs().then(()=>wPost('vfs-removed'));
+ break;
+ case 'vfs-pause':
+ poolUtil.pauseVfs();
+ wPost('vfs-paused');
+ break;
+ }
+};
+
+const hasOpfs = ()=>{
+ return globalThis.FileSystemHandle
+ && globalThis.FileSystemDirectoryHandle
+ && globalThis.FileSystemFileHandle
+ && globalThis.FileSystemFileHandle.prototype.createSyncAccessHandle
+ && navigator?.storage?.getDirectory;
+};
+if( !hasOpfs() ){
+ wPost('error',"OPFS not detected");
+}else{
+ globalThis.sqlite3InitModule().then(async function(sqlite3){
+ S = sqlite3;
+ capi = S.capi;
+ wasm = S.wasm;
+ log("sqlite3 version:",capi.sqlite3_libversion(),
+ capi.sqlite3_sourceid());
+ //return sqlite3.installOpfsSAHPoolVfs(sahPoolConfig).then(pu=>poolUtil=pu);
+ }).then(()=>{
+ wPost('initialized');
+ });
+}