2018-01-13 20:46:14 -08:00
|
|
|
import { spawn } from 'child_process'
|
|
|
|
import chai from 'chai'
|
|
|
|
import chaiAsPromised from 'chai-as-promised'
|
2018-01-16 12:43:36 -08:00
|
|
|
import btc from 'better-try-catch'
|
2018-01-13 20:46:14 -08:00
|
|
|
chai.use(chaiAsPromised)
|
|
|
|
const expect = chai.expect
|
|
|
|
|
|
|
|
import { Consumer } from '../src'
|
|
|
|
|
|
|
|
const USOCKET = __dirname + '/sample.sock'
|
|
|
|
|
2018-01-16 12:43:36 -08:00
|
|
|
let uconsumer = new Consumer(USOCKET, {name:'test-uconsumer'})
|
2018-01-18 21:21:06 -08:00
|
|
|
let tcpconsumer = new Consumer({name:'test-tcpconsumer'})
|
2018-01-19 20:43:16 -08:00
|
|
|
let tcpconsumer2 = new Consumer({port:9080, packetProcessor:'test', name:'test-tcpconsumer-2'})
|
|
|
|
|
2018-01-13 20:46:14 -08:00
|
|
|
|
|
|
|
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
|
|
|
|
2018-01-16 12:43:36 -08:00
|
|
|
let usocket = {}
|
|
|
|
let tcpsocket = {}
|
2018-01-19 20:43:16 -08:00
|
|
|
let tcpsocket2 = {}
|
2018-01-13 20:46:14 -08:00
|
|
|
|
2018-01-16 12:43:36 -08:00
|
|
|
describe('Connects and Processes a payload in a JSON packet', function(){
|
2018-01-13 20:46:14 -08:00
|
|
|
|
|
|
|
before(async function(){
|
2018-01-16 12:43:36 -08:00
|
|
|
usocket = spawn('node',['-r', '@std/esm', './test/usocket'])
|
|
|
|
usocket.stdout.on('data', function(buf) {
|
|
|
|
console.log('[Socket]', String(buf))
|
|
|
|
})
|
|
|
|
|
|
|
|
tcpsocket = spawn('node',['-r', '@std/esm', './test/tcpsocket'])
|
|
|
|
tcpsocket.stdout.on('data', function(buf) {
|
|
|
|
console.log('[Socket]', String(buf))
|
|
|
|
})
|
|
|
|
|
2018-01-19 20:43:16 -08:00
|
|
|
tcpsocket2 = spawn('node',['-r', '@std/esm', './test/tcpsocket2'])
|
|
|
|
tcpsocket2.stdout.on('data', function(buf) {
|
|
|
|
console.log('[Socket]', String(buf))
|
|
|
|
})
|
|
|
|
|
2018-01-16 12:43:36 -08:00
|
|
|
await delay(500) // wait for sockets to get going
|
2018-01-13 20:46:14 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
after(async function(){
|
2018-01-16 12:43:36 -08:00
|
|
|
usocket.kill()
|
|
|
|
tcpsocket.kill()
|
2018-01-19 20:43:16 -08:00
|
|
|
tcpsocket2.kill()
|
2018-01-13 20:46:14 -08:00
|
|
|
})
|
|
|
|
|
2018-01-19 20:43:16 -08:00
|
|
|
it('via unix socket with defaults', async function () {
|
|
|
|
|
|
|
|
uconsumer.times = 0
|
2018-01-13 20:46:14 -08:00
|
|
|
|
2018-01-16 12:43:36 -08:00
|
|
|
return new Promise(async function (resolve, reject) {
|
|
|
|
|
2018-01-19 20:43:16 -08:00
|
|
|
setTimeout(() =>{ reject('10 packets not received in time')},1900)
|
|
|
|
|
|
|
|
uconsumer.processPacket = function (packet) {
|
|
|
|
this.times++
|
|
|
|
if (this.times<10) return
|
|
|
|
|
|
|
|
try {
|
|
|
|
// expect(packet.payload).to.equal('unix payload')
|
|
|
|
expect(packet.payload).to.equal('unix payload')
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
catch(error) {
|
|
|
|
reject(error)
|
2018-01-16 12:43:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 20:43:16 -08:00
|
|
|
let [err] = await btc(uconsumer.connect)()
|
2018-01-16 12:43:36 -08:00
|
|
|
if (err) reject(err)
|
|
|
|
let packet = {payload:'unix payload'}
|
2018-01-19 20:43:16 -08:00
|
|
|
for (var i = 0; i < 11; i++) {
|
|
|
|
uconsumer.send(packet)
|
|
|
|
}
|
2018-01-16 12:43:36 -08:00
|
|
|
|
|
|
|
}) //end promise
|
|
|
|
|
|
|
|
}) // end unix socket test
|
|
|
|
|
2018-01-19 20:43:16 -08:00
|
|
|
it('via tcp socket with defaults', async function () {
|
2018-01-16 12:43:36 -08:00
|
|
|
|
|
|
|
return new Promise(async function (resolve, reject) {
|
|
|
|
|
2018-01-19 20:43:16 -08:00
|
|
|
tcpconsumer.processPacket = function (packet) {
|
|
|
|
try {
|
|
|
|
expect(packet.payload).to.equal('tcp processed tcp payload')
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
catch(error) {
|
|
|
|
reject(error)
|
2018-01-16 12:43:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 20:43:16 -08:00
|
|
|
let [err] = await btc(tcpconsumer.connect)()
|
2018-01-16 12:43:36 -08:00
|
|
|
if (err) reject(err)
|
|
|
|
let packet = {payload:'tcp payload'}
|
|
|
|
tcpconsumer.send(packet)
|
|
|
|
|
|
|
|
}) //end promise
|
2018-01-13 20:46:14 -08:00
|
|
|
|
2018-01-19 20:43:16 -08:00
|
|
|
}) // end tcp socket test
|
|
|
|
|
|
|
|
it('via alternate port tcp socket with attached custom socket processing and alt named socket processing', async function () {
|
|
|
|
|
|
|
|
return new Promise(async function (resolve, reject) {
|
|
|
|
|
|
|
|
tcpconsumer2.test = function (packet) {
|
|
|
|
try {
|
|
|
|
expect(packet.payload).to.equal('alt tcp processed tcp payload')
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
catch(error) {
|
|
|
|
reject(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let [err] = await btc(tcpconsumer2.connect)()
|
|
|
|
if (err) reject(err)
|
|
|
|
let packet = {payload:'tcp payload'}
|
|
|
|
tcpconsumer2.send(packet)
|
|
|
|
|
|
|
|
}) //end promise
|
|
|
|
|
|
|
|
}) // end tcp socket 2 test
|
|
|
|
|
2018-01-13 20:46:14 -08:00
|
|
|
|
|
|
|
})
|