sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / wasm / tests / opfs / sahpool / sahpool-pausing.js
| 1 | /* |
| 2 | 2025-01-31 |
| 3 | |
| 4 | The author disclaims copyright to this source code. In place of a |
| 5 | legal notice, here is a blessing: |
| 6 | |
| 7 | * May you do good and not evil. |
| 8 | * May you find forgiveness for yourself and forgive others. |
| 9 | * May you share freely, never taking more than you give. |
| 10 | |
| 11 | *********************************************************************** |
| 12 | |
| 13 | These tests are specific to the opfs-sahpool VFS and are limited to |
| 14 | demonstrating its pause/unpause capabilities. |
| 15 | |
| 16 | Most of this file is infrastructure for displaying results to the |
| 17 | user. Search for runTests() to find where the work actually starts. |
| 18 | */ |
| 19 | 'use strict'; |
| 20 | (function(){ |
| 21 | let logClass; |
| 22 | |
| 23 | const mapToString = (v)=>{ |
| 24 | switch(typeof v){ |
| 25 | case 'number': case 'string': case 'boolean': |
| 26 | case 'undefined': case 'bigint': |
| 27 | return ''+v; |
| 28 | default: break; |
| 29 | } |
| 30 | if(null===v) return 'null'; |
| 31 | if(v instanceof Error){ |
| 32 | v = { |
| 33 | message: v.message, |
| 34 | stack: v.stack, |
| 35 | errorClass: v.name |
| 36 | }; |
| 37 | } |
| 38 | return JSON.stringify(v,undefined,2); |
| 39 | }; |
| 40 | const normalizeArgs = (args)=>args.map(mapToString); |
| 41 | const logTarget = document.querySelector('#test-output'); |
| 42 | logClass = function(cssClass,...args){ |
| 43 | const ln = document.createElement('div'); |
| 44 | if(cssClass){ |
| 45 | for(const c of (Array.isArray(cssClass) ? cssClass : [cssClass])){ |
| 46 | ln.classList.add(c); |
| 47 | } |
| 48 | } |
| 49 | ln.append(document.createTextNode(normalizeArgs(args).join(' '))); |
| 50 | logTarget.append(ln); |
| 51 | }; |
| 52 | const cbReverse = document.querySelector('#cb-log-reverse'); |
| 53 | //cbReverse.setAttribute('checked','checked'); |
| 54 | const cbReverseKey = 'tester1:cb-log-reverse'; |
| 55 | const cbReverseIt = ()=>{ |
| 56 | logTarget.classList[cbReverse.checked ? 'add' : 'remove']('reverse'); |
| 57 | //localStorage.setItem(cbReverseKey, cbReverse.checked ? 1 : 0); |
| 58 | }; |
| 59 | cbReverse.addEventListener('change', cbReverseIt, true); |
| 60 | /*if(localStorage.getItem(cbReverseKey)){ |
| 61 | cbReverse.checked = !!(+localStorage.getItem(cbReverseKey)); |
| 62 | }*/ |
| 63 | cbReverseIt(); |
| 64 | |
| 65 | const log = (...args)=>{ |
| 66 | //console.log(...args); |
| 67 | logClass('',...args); |
| 68 | } |
| 69 | const warn = (...args)=>{ |
| 70 | console.warn(...args); |
| 71 | logClass('warning',...args); |
| 72 | } |
| 73 | const error = (...args)=>{ |
| 74 | console.error(...args); |
| 75 | logClass('error',...args); |
| 76 | }; |
| 77 | |
| 78 | const toss = (...args)=>{ |
| 79 | error(...args); |
| 80 | throw new Error(args.join(' ')); |
| 81 | }; |
| 82 | |
| 83 | const endOfWork = (passed=true)=>{ |
| 84 | const eH = document.querySelector('#color-target'); |
| 85 | const eT = document.querySelector('title'); |
| 86 | if(passed){ |
| 87 | log("End of work chain. If you made it this far, you win."); |
| 88 | eH.innerText = 'PASS: '+eH.innerText; |
| 89 | eH.classList.add('tests-pass'); |
| 90 | eT.innerText = 'PASS: '+eT.innerText; |
| 91 | }else{ |
| 92 | eH.innerText = 'FAIL: '+eH.innerText; |
| 93 | eH.classList.add('tests-fail'); |
| 94 | eT.innerText = 'FAIL: '+eT.innerText; |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | const nextHandlerQueue = []; |
| 99 | |
| 100 | const nextHandler = function(workerId,...msg){ |
| 101 | log(workerId,...msg); |
| 102 | (nextHandlerQueue.shift())(); |
| 103 | }; |
| 104 | |
| 105 | const postThen = function(W, msgType, callback){ |
| 106 | nextHandlerQueue.push(callback); |
| 107 | W.postMessage({type:msgType}); |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | Run a series of operations on an sahpool db spanning two workers. |
| 112 | This would arguably be more legible with Promises, but creating a |
| 113 | Promise-based communication channel for this purpose is left as |
| 114 | an exercise for the reader. An example of such a proxy can be |
| 115 | found in the SQLite source tree: |
| 116 | |
| 117 | https://sqlite.org/src/file/ext/wasm/api/sqlite3-worker1-promiser.c-pp.js |
| 118 | */ |
| 119 | const runPyramidOfDoom = function(W1, W2){ |
| 120 | postThen(W1, 'vfs-acquire', function(){ |
| 121 | postThen(W1, 'db-init', function(){ |
| 122 | postThen(W1, 'db-query', function(){ |
| 123 | postThen(W1, 'vfs-pause', function(){ |
| 124 | postThen(W2, 'vfs-acquire', function(){ |
| 125 | postThen(W2, 'db-query', function(){ |
| 126 | postThen(W2, 'vfs-remove', endOfWork); |
| 127 | }); |
| 128 | }); |
| 129 | }); |
| 130 | }); |
| 131 | }); |
| 132 | }); |
| 133 | }; |
| 134 | |
| 135 | const runTests = function(){ |
| 136 | log("Running opfs-sahpool pausing tests..."); |
| 137 | const wjs = 'sahpool-worker.js?sqlite3.dir=../../../jswasm'; |
| 138 | const W1 = new Worker(wjs+'&workerId=w1'), |
| 139 | W2 = new Worker(wjs+'&workerId=w2'); |
| 140 | W1.workerId = 'w1'; |
| 141 | W2.workerId = 'w2'; |
| 142 | let initCount = 0; |
| 143 | const onmessage = function({data}){ |
| 144 | //log("onmessage:",data); |
| 145 | switch(data.type){ |
| 146 | case 'vfs-acquired': |
| 147 | nextHandler(data.workerId, "VFS acquired"); |
| 148 | break; |
| 149 | case 'vfs-paused': |
| 150 | nextHandler(data.workerId, "VFS paused"); |
| 151 | break; |
| 152 | case 'vfs-unpaused': |
| 153 | nextHandler(data.workerId, 'VFS unpaused'); |
| 154 | break; |
| 155 | case 'vfs-removed': |
| 156 | nextHandler(data.workerId, 'VFS removed'); |
| 157 | break; |
| 158 | case 'db-inited': |
| 159 | nextHandler(data.workerId, 'db initialized'); |
| 160 | break; |
| 161 | case 'query-result': |
| 162 | nextHandler(data.workerId, 'query result', data.payload); |
| 163 | break; |
| 164 | case 'log': |
| 165 | log(data.workerId, ':', ...data.payload); |
| 166 | break; |
| 167 | case 'error': |
| 168 | error(data.workerId, ':', ...data.payload); |
| 169 | endOfWork(false); |
| 170 | break; |
| 171 | case 'initialized': |
| 172 | log(data.workerId, ': Worker initialized',...data.payload); |
| 173 | if( 2===++initCount ){ |
| 174 | runPyramidOfDoom(W1, W2); |
| 175 | } |
| 176 | break; |
| 177 | } |
| 178 | }; |
| 179 | W1.onmessage = W2.onmessage = onmessage; |
| 180 | }; |
| 181 | |
| 182 | runTests(); |
| 183 | })(); |