UNPKG

wowok

Version:

Wowok Blockchain TypeScript API

1 lines 4.77 kB
import{PersistentStorage,MultiProcessKVStore}from'./persistent-storage.js';import{existsSync,unlinkSync}from'fs';const STORAGE_FILE='./test-storage.json',KV_STORE_FILE='./test-kv-store.json';function cleanupTestFiles(){existsSync(STORAGE_FILE)&&unlinkSync(STORAGE_FILE),existsSync(STORAGE_FILE+'.lock')&&unlinkSync(STORAGE_FILE+'.lock'),existsSync(STORAGE_FILE+'.lock.shared')&&unlinkSync(STORAGE_FILE+'.lock.shared'),existsSync(KV_STORE_FILE)&&unlinkSync(KV_STORE_FILE),existsSync(KV_STORE_FILE+'.lock')&&unlinkSync(KV_STORE_FILE+'.lock'),existsSync(KV_STORE_FILE+'.lock.shared')&&unlinkSync(KV_STORE_FILE+'.lock.shared');}async function testPersistentStorage(){console['log']('===\x20Testing\x20PersistentStorage\x20===');const a=new PersistentStorage(STORAGE_FILE);console['log']('Writing\x20initial\x20data...'),await a['writeAll']({'port':0xbb8,'host':'localhost','debug':!![]}),console['log']('Initial\x20data\x20written\x20successfully!'),console['log']('Reading\x20all\x20data...');const b=await a['readAll']();console['log']('Read\x20data:',b),console['log']('Getting\x20specific\x20key\x20(port)...');const c=await a['get']('port');console['log']('Port:',c),console['log']('Setting\x20new\x20value\x20for\x20port...'),await a['set']('port',0xfa0),console['log']('New\x20port\x20set\x20to:',await a['get']('port')),console['log']('Adding\x20new\x20key\x20(apiKey)...'),await a['set']('apiKey','test-12345'),console['log']('New\x20apiKey:',await a['get']('apiKey')),console['log']('Running\x20transaction...'),await a['transaction'](d=>{return d['debug']=![],d['host']='0.0.0.0',d;}),console['log']('After\x20transaction:',await a['readAll']()),console['log']('Clearing\x20all\x20data...'),await a['clear'](),console['log']('After\x20clearing:',await a['readAll']()),console['log']('PersistentStorage\x20tests\x20completed!\x0a');}async function testMultiProcessKVStore(){console['log']('===\x20Testing\x20MultiProcessKVStore\x20===');const a=new MultiProcessKVStore(KV_STORE_FILE);console['log']('Setting\x20values...'),await a['set']('username','john_doe'),await a['set']('email','john@example.com'),await a['set']('role','admin'),console['log']('Values\x20set\x20successfully!'),console['log']('Getting\x20values...'),console['log']('Username:',await a['get']('username')),console['log']('Email:',await a['get']('email')),console['log']('Role:',await a['get']('role')),console['log']('Checking\x20if\x20keys\x20exist...'),console['log']('Username\x20exists:',await a['has']('username')),console['log']('Password\x20exists:',await a['has']('password')),console['log']('Getting\x20all\x20keys...');const b=await a['keys']();console['log']('Keys:',b),console['log']('Getting\x20all\x20values...');const c=await a['values']();console['log']('Values:',c),console['log']('Getting\x20all\x20entries...');const d=await a['entries']();console['log']('Entries:',d),console['log']('Getting\x20size...');const e=await a['size']();console['log']('Size:',e),console['log']('Deleting\x20a\x20key\x20(role)...'),await a['delete']('role'),console['log']('Role\x20exists\x20after\x20deletion:',await a['has']('role')),console['log']('Size\x20after\x20deletion:',await a['size']()),console['log']('Clearing\x20all\x20data...'),await a['clear'](),console['log']('Size\x20after\x20clearing:',await a['size']()),console['log']('MultiProcessKVStore\x20tests\x20completed!\x0a');}async function testMultiProcessSimulation(){console['log']('===\x20Testing\x20Multi-Process\x20Simulation\x20===');const a=new PersistentStorage(STORAGE_FILE),b=new PersistentStorage(STORAGE_FILE);await a['writeAll']({'counter':0x0}),console['log']('Initial\x20counter:',await a['get']('counter')),console['log']('Simulating\x20concurrent\x20updates...');const c=async()=>{for(let f=0x0;f<0x5;f++){await a['transaction'](g=>{return g['counter']+=0x1,g;}),console['log']('Process\x201:\x20counter\x20=',await a['get']('counter')),await new Promise(g=>setTimeout(g,0x64));}},d=async()=>{for(let f=0x0;f<0x5;f++){await b['transaction'](g=>{return g['counter']+=0x1,g;}),console['log']('Process\x202:\x20counter\x20=',await b['get']('counter')),await new Promise(g=>setTimeout(g,0x96));}};await Promise['all']([c(),d()]);const e=await a['get']('counter');console['log']('Final\x20counter\x20value:',e),console['log']('Expected\x20final\x20counter\x20value:\x2010'),console['log']('Multi-process\x20simulation\x20completed!\x0a');}async function runAllTests(){try{cleanupTestFiles(),await testPersistentStorage(),await testMultiProcessKVStore(),await testMultiProcessSimulation(),cleanupTestFiles(),console['log']('All\x20tests\x20completed\x20successfully!');}catch(a){console['error']('Error\x20running\x20tests:',a),cleanupTestFiles();}}require['main']===module&&runAllTests()['catch'](console['error']);