This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+12
View File
@@ -0,0 +1,12 @@
// gh-8
var Fiber = require('fibers');
try {
Fiber(function() {
var that = Fiber.current;
Fiber(function(){
that.run();
}).run();
}).run();
} catch(err) {
console.log('pass');
}
+38
View File
@@ -0,0 +1,38 @@
'use strict';
// This test must be run with --force-async-hooks-checks
if (process.versions.modules < 57) {
console.log('pass');
return;
}
const { AsyncResource } = require('async_hooks');
const Fiber = require('fibers');
class TestResource extends AsyncResource {
constructor() {
super('TestResource');
}
run(cb) {
// In the v8 API, only emitBefore() and emitAfter() are available
if (process.versions.modules < 59) {
this.emitBefore();
cb();
this.emitAfter();
} else {
// In v9 and higher, emitBefore() and emitAfter() are deperecated in favor of runInAsyncScope().
this.runInAsyncScope(cb);
}
}
}
let tmp = Fiber(function() {
let resource = new TestResource;
resource.run(function() {
Fiber.yield();
});
});
tmp.run();
setTimeout(function() {
tmp.run();
console.log('pass');
}, 5);
+7
View File
@@ -0,0 +1,7 @@
var Fiber = require('fibers');
try {
Fiber.prototype.run.call(null);
} catch (err) {
console.log('pass');
}
+27
View File
@@ -0,0 +1,27 @@
// gh-20
var Fiber = require('fibers');
function main() {
var proc = require('child_process').spawn(
process.execPath,
[process.argv[1], 'child'],
{env: process.env}
);
function ondata(data) {
process.stdout.write(data+ '');
}
proc.stdout.on('data', ondata);
proc.stderr.on('data', ondata);
}
function child() {
var fn = Fiber(function() {
Fiber.yield('pa');
return 'ss';
});
var r1 = fn.run();
var r2 = fn.run();
console.log(r1+ r2);
}
process.argv[2] === 'child' ? child() : main();
+49
View File
@@ -0,0 +1,49 @@
"use strict";
var Fiber = require('fibers');
Fiber.poolSize = 100;
let v8 = /^([0-9]+)\.([0-9]+)/.exec(process.versions.v8);
if (v8[1] > 4 || (v8[1] == 4 && v8[2] >= 10)) {
// Vague benchmark of fiber performance, lower is better
function bench() {
var d = new Date;
for (var ii = 0; ii < 100; ++ii) {
var fibers = [];
for (var jj = 0; jj < Fiber.poolSize; ++jj) {
var fiber = Fiber(function() {
Fiber.yield();
});
fiber.run();
fibers.push(fiber);
}
fibers.map(function(fiber) {
fiber.run();
});
}
return new Date - d;
}
// Run initial benchmark
var ts1 = Math.min(bench(), bench());
// Dirty up isolate list
var fibers = [];
for (var ii = 0; ii < Fiber.poolSize + 1000; ++ii) {
let fiber = Fiber(function() {
Fiber.yield();
});
fiber.run();
fibers.push(fiber);
}
fibers.map(function(fiber) {
fiber.run();
});
// Test again
var ts2 = Math.min(bench(), bench());
console.log(ts1 * 2 < ts2 ? 'fail' : 'pass');
} else {
// Feature is not supported
console.log('pass');
}
+13
View File
@@ -0,0 +1,13 @@
var Fiber = require('fibers');
var current;
Fiber(function() {
current = Fiber.current;
Fiber.yield();
console.log('pass');
}).run();
if (current) {
current.run();
} else {
console.log('fail');
}
+14
View File
@@ -0,0 +1,14 @@
// gh-1
var Fiber = require('fibers');
if (process.platform == 'win32') {
// There is a problem with running this from a script. Not fibers related.
console.log('pass');
} else {
Fiber(function() {
require('child_process').exec('echo pass', function(err, stdout) {
if (err) console.log(err);
process.stdout.write(stdout);
});
}).run();
}
+15
View File
@@ -0,0 +1,15 @@
var Fiber = require('fibers');
if (!process.stdout.write('pass\n')) {
process.stdout.on('drain', go);
} else {
go();
}
function go() {
// Windows needs some time to flush the output and I can't figure out a better way
setTimeout(function() {
Fiber(function() {
process.exit();
}).run();
console.log('fail');
}, 10);
}
+24
View File
@@ -0,0 +1,24 @@
var Fiber = require('fibers');
function Fibonacci() {
return Fiber.prototype.run.bind(Fiber(function() {
Fiber.yield(0); // F(0) -> 0
var prev = 0, curr = 1;
while (true) {
Fiber.yield(curr);
var tmp = prev + curr;
prev = curr;
curr = tmp;
}
}));
}
var seq = Fibonacci(), results = [];
for (var ii = seq(); ii <= 1597; ii = seq()) {
results.push(ii);
}
if (results+ '' !== '0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597') {
throw new Error;
}
console.log('pass');
+8
View File
@@ -0,0 +1,8 @@
// gh-16
var Fiber = require('fibers');
Fiber(function() {
Fiber(function() {
Fiber(function() {}).run();
}).run();
}).run();
console.log('pass');
+52
View File
@@ -0,0 +1,52 @@
var Fiber = require('fibers');
var Future = require('future');
// Possible outputs:
// pass: exception is thrown and caught in uncaughtException
// fail: exception is thrown and not caught
// no output: process dies
var thrown = false;
var caught = false;
var async = function(continuation) {
process.nextTick(function() {
continuation();
});
}
process.on('uncaughtException', function(err) {
if (err.message === 'Catch me if you can') {
caught = true;
} else {
throw err;
}
});
// This fiber's job is to throw an exception after yielding.
Fiber(function() {
// yield and resume via Future.wait() and its cb() helper
var sync = Future.wrap(async)();
sync.wait();
// this should get rethrown to the main event loop
thrown = true;
throw new Error('Catch me if you can');
}).run();
// This fiber's job is to make sure the process is still alive after the
// exception was thrown.
Fiber(function() {
// wait for other fiber to throw exception and yield
while (!thrown) {
var sync = Future.wrap(async)();
sync.wait();
}
// wait once more to allow exception to get caught
process.nextTick(function() {
// see if we have noticed the exception we expect to
console.log(caught ? 'pass' : 'fail');
});
}).run();
+26
View File
@@ -0,0 +1,26 @@
var Future;
try {
Future = require('fibers/future');
} catch (err) {
Future = require('future');
}
function Timer(ms) {
var future = new Future;
function ret() {
future.return();
}
ms ? setTimeout(ret, ms) : process.nextTick(ret);
return future;
}
~function() {
var timer = new Timer(10), tick = new Timer;
Future.wait(timer, tick);
timer.get();
tick.get();
return 'pass';
}.future()().resolve(function(err, val) {
if (err) throw err;
console.log(val);
});
+7
View File
@@ -0,0 +1,7 @@
// gh-3
var Fiber = require('fibers');
try {
Fiber.yield();
} catch(err) {
console.log('pass');
}
+16
View File
@@ -0,0 +1,16 @@
var Fiber = require('fibers');
for (var jj = 0; jj < 10; ++jj) {
var fibers = [];
for (var ii = 0; ii < 200; ++ii) {
var fn = Fiber(function() {
Fiber.yield();
});
fn.run();
fibers.push(fn);
}
for (var ii = 0; ii < fibers.length; ++ii) {
fibers[ii].run();
}
}
console.log('pass');
+8
View File
@@ -0,0 +1,8 @@
// gh-10
var Fiber = require('fibers');
var title = process.title;
Fiber(function() {
process.title = 'pass';
}).run();
console.log(process.title === 'pass' || process.title === title ? 'pass' : 'fail');
+12
View File
@@ -0,0 +1,12 @@
var Fiber = require('fibers');
try {
Fiber(function() {
function foo() {
var hello = Math.random();
foo();
}
foo();
}).run();
} catch (err) {
err.name === 'RangeError' && console.log('pass');
}
Generated Vendored Executable
+45
View File
@@ -0,0 +1,45 @@
var Fiber = require('fibers');
// Calculate how far we can go recurse without hitting the JS stack limit
function calculateStackSpace() {
var max = 0;
function testRecursion(ii) {
++max;
testRecursion(ii + 1);
}
try {
testRecursion();
} catch (err) {}
return max;
}
// Invoke a RepExp operation that eats a lot of stack space
function pathologicRegExp(preStack) {
function fn() {
var foo = '';
for (var ii = 0; ii < 1024; ++ii) {
foo += 'a';
}
new RegExp(foo, 'g');
}
// Recurse to the limit and then invoke a stack-heavy C++ operation
function wasteStack(ii) {
ii ? wasteStack(ii - 1) : fn();
}
wasteStack(preStack);
}
Fiber(function() {
// Ensure that this doesn't ruin everything while in a fiber
var max = calculateStackSpace();
for (var stack = max; stack > 0; --stack) {
try {
pathologicRegExp(stack);
break;
} catch (err) {}
}
}).run();
console.log('pass');
+8
View File
@@ -0,0 +1,8 @@
// gh-12
var Fiber = require('fibers');
Fiber(function() {
if (!Fiber.current.started) {
throw new Error;
}
}).run();
console.log('pass');
+14
View File
@@ -0,0 +1,14 @@
var Fiber = require('fibers');
var ii;
var fn = Fiber(function() {
for (ii = 0; ii < 1000; ++ii) {
try {
Fiber.yield();
} catch (err) {}
}
});
fn.run();
fn.reset();
ii === 1000 && console.log('pass');