2018-01-23 16:02:56 -08:00
|
|
|
import { spawn } from 'child_process'
|
|
|
|
import chai from 'chai'
|
|
|
|
import chaiAsPromised from 'chai-as-promised'
|
|
|
|
import btc from 'better-try-catch'
|
|
|
|
chai.use(chaiAsPromised)
|
|
|
|
const expect = chai.expect
|
|
|
|
|
|
|
|
import { Consumer } from '../src'
|
|
|
|
|
|
|
|
const USOCKET = __dirname + '/sockets/test.sock'
|
|
|
|
const SOCKET_FILE = 'usocket-default'
|
|
|
|
|
2018-02-03 13:33:25 -08:00
|
|
|
let consumer = new Consumer({path:USOCKET,name:'unix-consumer'})
|
|
|
|
let consumer2 = new Consumer({path:USOCKET, name:'unix-consumer2'})
|
2018-01-23 16:02:56 -08:00
|
|
|
|
2018-02-03 13:33:25 -08:00
|
|
|
// const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
2018-01-23 16:02:56 -08:00
|
|
|
|
|
|
|
let socket = {}
|
|
|
|
|
|
|
|
describe('Connects and Processes a payload via Unix Socket using JSON packet with defaults', function(){
|
|
|
|
|
|
|
|
before(async function(){
|
|
|
|
socket = spawn('node',['-r', '@std/esm', './test/sockets/'+SOCKET_FILE])
|
|
|
|
socket.stdout.on('data', function(buf) {
|
|
|
|
console.log('[Socket]', String(buf))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
after(async function(){
|
|
|
|
socket.kill()
|
|
|
|
})
|
|
|
|
|
2018-02-13 13:51:58 -08:00
|
|
|
const TIMES = 5000
|
2018-01-23 16:02:56 -08:00
|
|
|
|
2018-02-12 14:41:06 -08:00
|
|
|
it(`Tests unix socket with default echo JSON packet procssing with ${TIMES} packets sent`, async function () {
|
2018-01-23 16:02:56 -08:00
|
|
|
|
2018-02-12 14:41:06 -08:00
|
|
|
let [err] = await btc(consumer.connect)()
|
|
|
|
if (err) {
|
|
|
|
console.log('unable to connect to socket to start test', consumer.path)
|
|
|
|
process.kill(process.pid, 'SIGTERM')
|
|
|
|
}
|
2018-01-23 16:02:56 -08:00
|
|
|
|
2018-02-12 14:41:06 -08:00
|
|
|
consumer.registerPacketProcessor(async function (packet) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
packet.times += 1
|
|
|
|
if (packet.times === TIMES) packet.payload = 'local1:'+packet.payload
|
|
|
|
resolve(packet)})
|
|
|
|
})
|
2018-01-23 16:02:56 -08:00
|
|
|
|
|
|
|
|
2018-02-12 14:41:06 -08:00
|
|
|
let packet = {payload:'payload', times:0}
|
|
|
|
for (let i = 1; i <= TIMES; i++) {
|
|
|
|
packet = await consumer.send(packet)
|
|
|
|
}
|
|
|
|
expect(packet.payload+':'+packet.times).to.equal('local1:payload:'+TIMES)
|
2018-01-23 16:02:56 -08:00
|
|
|
|
|
|
|
}) // end unix socket test
|
|
|
|
|
|
|
|
|
2018-02-12 14:41:06 -08:00
|
|
|
it(`unix socket with two consumers alternating packets, ${TIMES} packets each and local processing`, async function () {
|
2018-01-23 16:02:56 -08:00
|
|
|
|
2018-01-30 16:59:57 -08:00
|
|
|
|
|
|
|
let [err] = await btc(consumer2.connect)()
|
|
|
|
if (err) {
|
|
|
|
console.log('unable to connect to socket to start test', consumer.path)
|
|
|
|
process.kill(process.pid, 'SIGTERM')
|
|
|
|
}
|
2018-01-23 16:02:56 -08:00
|
|
|
|
2018-02-12 14:41:06 -08:00
|
|
|
consumer2.registerPacketProcessor(async function (packet) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
packet.times += 1
|
|
|
|
if (packet.times === TIMES) packet.payload = 'local2:'+packet.payload
|
|
|
|
resolve(packet)})
|
|
|
|
})
|
2018-01-23 16:02:56 -08:00
|
|
|
|
2018-02-12 14:41:06 -08:00
|
|
|
let packet = {consumer:1, payload:'payload', times:-1}
|
|
|
|
for (let i = 0; i < TIMES; i++) {
|
|
|
|
packet = await consumer.send(packet)
|
|
|
|
if (packet.times === TIMES) packet.times = 1
|
|
|
|
packet = await consumer2.send(packet)
|
|
|
|
}
|
|
|
|
expect(packet.payload+':'+packet.times).to.equal('local2:local1:payload:'+TIMES)
|
2018-01-23 16:02:56 -08:00
|
|
|
|
|
|
|
}) // end unix socket test
|
|
|
|
|
|
|
|
}) // end describe
|