Files
smartspawn/test/test.ts

62 lines
1.8 KiB
TypeScript
Raw Normal View History

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