smartspawn/test/test.ts

61 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-11-28 15:10:15 +00:00
import { expect, tap } from 'tapbundle'
2017-11-28 15:10:15 +00:00
import * as smartipc from '../ts/index'
2017-01-29 19:50:36 +00:00
let testThreadFunction: smartipc.ThreadFunction
let testThread: smartipc.Thread
2017-01-29 22:41:26 +00:00
let testPool: smartipc.Pool
2017-11-28 15:10:15 +00:00
/**
* create a normal ThreadFunction
*/
tap.test('should create an instance of ThreadFunction', async () => {
testThreadFunction = new smartipc.ThreadFunction((input, done) => {
let url = require('url')
done(url.parse(input))
2017-03-03 19:52:23 +00:00
})
2017-11-28 15:10:15 +00:00
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 () => {
smartipc.setWorkerBasePath(__dirname)
testThread = new smartipc.Thread('child.js')
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 () => {
smartipc.setWorkerBasePath(__dirname)
let testThread = new smartipc.Thread('child.js')
})
2017-01-29 22:41:26 +00:00
2017-11-28 15:10:15 +00:00
tap.test('should run in a Pool', async () => {
let testPool = new smartipc.Pool()
let testThread = new smartipc.Thread('child.js')
testThread.assignToPool(testPool)
// first run
let message = await testThread.send('what')
expect(message).to.equal('what')
console.log(message)
// second run
message = await testThread.send('another')
expect(message).to.equal('another')
console.log(message)
// kill all
testThread.assignedPool.pool.killAll()
})
2017-11-28 15:10:15 +00:00
tap.test('should once', async () => {
let testThread = new smartipc.Thread('child.js')
const message = await testThread.sendOnce('what')
expect(message).to.equal('what')
})
tap.start()