60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
|
|
// Test script to verify that the function string format works with the fallback mechanism
|
||
|
|
import { executeInThread } from './dist/unified.js'
|
||
|
|
|
||
|
|
// Define a compute-intensive function using a named function declaration
|
||
|
|
// followed by a statement that returns the function
|
||
|
|
const computeIntensiveFunction = `
|
||
|
|
// Define a named function
|
||
|
|
function computeTask(data) {
|
||
|
|
console.log('Worker/Fallback: Starting computation...');
|
||
|
|
|
||
|
|
// Simulate a compute-intensive task
|
||
|
|
const start = Date.now();
|
||
|
|
let result = 0;
|
||
|
|
for (let i = 0; i < data.iterations; i++) {
|
||
|
|
result += Math.sqrt(i) * Math.sin(i);
|
||
|
|
}
|
||
|
|
|
||
|
|
const duration = Date.now() - start;
|
||
|
|
console.log('Worker/Fallback: Computation completed in ' + duration + 'ms');
|
||
|
|
|
||
|
|
return {
|
||
|
|
result,
|
||
|
|
duration,
|
||
|
|
iterations: data.iterations
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
// Return the function
|
||
|
|
computeTask;
|
||
|
|
`
|
||
|
|
|
||
|
|
// Test with different environments
|
||
|
|
async function runTests() {
|
||
|
|
try {
|
||
|
|
console.log('Testing executeInThread with fallback...')
|
||
|
|
|
||
|
|
// Disable Web Workers to force fallback
|
||
|
|
const originalWorker = globalThis.Worker
|
||
|
|
globalThis.Worker = function() {
|
||
|
|
throw new Error('Worker constructor disabled for testing')
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
// Execute the function in fallback mode
|
||
|
|
const result = await executeInThread(computeIntensiveFunction, {
|
||
|
|
iterations: 1000000
|
||
|
|
})
|
||
|
|
console.log('Fallback result:', result)
|
||
|
|
console.log('Test passed!')
|
||
|
|
} finally {
|
||
|
|
// Restore Web Workers
|
||
|
|
globalThis.Worker = originalWorker
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Test failed:', error)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
runTests()
|