2018-08-02 13:31:05 +00:00
|
|
|
import { expect, tap } from '@pushrocks/tapbundle';
|
2016-09-28 21:46:28 +00:00
|
|
|
|
2018-08-02 13:31:05 +00:00
|
|
|
import * as smartspawn from '../ts/index';
|
2016-09-28 21:46:28 +00:00
|
|
|
|
2018-08-02 13:31:05 +00:00
|
|
|
let testThreadFunction: smartspawn.ThreadFunction;
|
|
|
|
let testThread: smartspawn.Thread;
|
|
|
|
let testPool: smartspawn.Pool;
|
2016-09-28 21:46:28 +00:00
|
|
|
|
2017-11-28 15:10:15 +00:00
|
|
|
/**
|
|
|
|
* create a normal ThreadFunction
|
|
|
|
*/
|
|
|
|
tap.test('should create an instance of ThreadFunction', async () => {
|
2018-08-02 13:31:05 +00:00
|
|
|
testThreadFunction = new smartspawn.ThreadFunction((input, done) => {
|
|
|
|
let url = require('url');
|
|
|
|
done(url.parse(input));
|
|
|
|
});
|
|
|
|
const message = await testThreadFunction.send('https://google.com');
|
|
|
|
console.log(message);
|
|
|
|
testThreadFunction.kill();
|
|
|
|
});
|
2017-03-03 19:52:23 +00:00
|
|
|
|
2017-11-28 15:10:15 +00:00
|
|
|
tap.test('should create an instance of Thread', async () => {
|
2018-08-02 13:31:05 +00:00
|
|
|
smartspawn.setWorkerBasePath(__dirname);
|
2018-08-07 09:13:03 +00:00
|
|
|
testThread = new smartspawn.Thread('../testassets/child.ts');
|
2018-08-02 13:31:05 +00:00
|
|
|
testThread.enableTypeScript();
|
|
|
|
const message = await testThread.send('https://google.com');
|
|
|
|
console.log(message);
|
|
|
|
testThread.kill();
|
|
|
|
});
|
2017-01-29 22:41:26 +00:00
|
|
|
|
2017-11-28 15:10:15 +00:00
|
|
|
tap.test('should not spawn when nothing is sent', async () => {
|
2018-08-02 13:31:05 +00:00
|
|
|
smartspawn.setWorkerBasePath(__dirname);
|
2018-08-07 09:13:03 +00:00
|
|
|
let testThread = new smartspawn.Thread('../testassets/child.ts');
|
2018-08-02 13:31:05 +00:00
|
|
|
});
|
2017-01-29 22:41:26 +00:00
|
|
|
|
2017-11-28 15:10:15 +00:00
|
|
|
tap.test('should run in a Pool', async () => {
|
2018-08-02 13:31:05 +00:00
|
|
|
let testPool = new smartspawn.Pool();
|
2018-08-07 09:13:03 +00:00
|
|
|
let testThread = new smartspawn.Thread('../testassets/child.ts');
|
2018-08-02 13:31:05 +00:00
|
|
|
testThread.assignToPool(testPool);
|
2017-11-28 15:10:15 +00:00
|
|
|
|
|
|
|
// first run
|
2018-08-02 13:31:05 +00:00
|
|
|
let message = await testThread.send('what');
|
|
|
|
expect(message).to.equal('what');
|
|
|
|
console.log(message);
|
2017-11-28 15:10:15 +00:00
|
|
|
|
|
|
|
// second run
|
2018-08-02 13:31:05 +00:00
|
|
|
message = await testThread.send('another');
|
|
|
|
expect(message).to.equal('another');
|
|
|
|
console.log(message);
|
|
|
|
|
2017-11-28 15:10:15 +00:00
|
|
|
// kill all
|
2018-08-02 13:31:05 +00:00
|
|
|
testThread.assignedPool.pool.killAll();
|
|
|
|
});
|
2017-11-28 15:10:15 +00:00
|
|
|
|
|
|
|
tap.test('should once', async () => {
|
2018-08-07 09:13:03 +00:00
|
|
|
let testThread = new smartspawn.Thread('../testassets/child.ts');
|
2018-08-02 13:31:05 +00:00
|
|
|
const message = await testThread.sendOnce('what');
|
|
|
|
expect(message).to.equal('what');
|
|
|
|
});
|
2017-11-28 15:10:15 +00:00
|
|
|
|
2018-08-02 13:31:05 +00:00
|
|
|
tap.start();
|