-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
58 lines (50 loc) · 1.47 KB
/
example.js
File metadata and controls
58 lines (50 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Example usage of fakesync package
const fakesync = require('./index');
// Set global default options (optional)
// This sets defaults that can be overridden per register call
fakesync.registerDefaults({
minDelay: 100, // minimum delay in ms
maxDelay: 1000, // maximum delay in ms
failRate: 0.1 // 10% chance of failure
});
// Define some functions to wrap
function syncFunc(x) {
console.log(`syncFunc called with ${x}`);
return x * 2;
}
async function asyncFunc(x) {
console.log(`asyncFunc called with ${x}`);
// Simulate some async work
return new Promise(resolve => {
setTimeout(() => resolve(x + 10), 50);
});
}
// Register the functions with fakesync and get back wrapped functions
// Options here will override defaults
const api = fakesync.register({
syncFunc,
asyncFunc
}, {
// Override defaults for these functions
minDelay: 200,
maxDelay: 800,
failRate: 0.2 // 20% chance of failure
});
// Now use the wrapped functions
(async () => {
console.log('Testing sync function:');
try {
const result1 = await api.syncFunc(5);
console.log('syncFunc result:', result1);
} catch (e) {
console.log('syncFunc failed:', e.message);
}
console.log('\nTesting async function:');
try {
const result2 = await api.asyncFunc(5);
console.log('asyncFunc result:', result2);
} catch (e) {
console.log('asyncFunc failed:', e.message);
}
console.log('\nRun this script multiple times to see simulated delays and failures.');
})();