92 lines
2.2 KiB
JavaScript
92 lines
2.2 KiB
JavaScript
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 + '/sample.sock'
|
|
|
|
let uconsumer = new Consumer(USOCKET, {name:'test-uconsumer'})
|
|
let tcpconsumer = new Consumer('localhost',{port: 8081, tcp:true, name:'test-tcpconsumer'})
|
|
|
|
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
|
|
|
|
let usocket = {}
|
|
let tcpsocket = {}
|
|
|
|
describe('Connects and Processes a payload in a JSON packet', function(){
|
|
|
|
before(async function(){
|
|
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))
|
|
})
|
|
|
|
await delay(500) // wait for sockets to get going
|
|
})
|
|
|
|
after(async function(){
|
|
usocket.kill()
|
|
tcpsocket.kill()
|
|
})
|
|
|
|
it('via unix socket', async function () {
|
|
|
|
return new Promise(async function (resolve, reject) {
|
|
|
|
const app = {
|
|
processPacket: function (packet) {
|
|
try {
|
|
expect(packet.payload).to.equal('processed unix payload')
|
|
resolve()
|
|
}
|
|
catch(error) {
|
|
reject(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
let [err] = await btc(uconsumer.connect.bind(uconsumer))(app)
|
|
if (err) reject(err)
|
|
let packet = {payload:'unix payload'}
|
|
uconsumer.send(packet)
|
|
|
|
}) //end promise
|
|
|
|
}) // end unix socket test
|
|
|
|
it('via tcp socket', async function () {
|
|
|
|
return new Promise(async function (resolve, reject) {
|
|
|
|
const app = {
|
|
processPacket: function (packet) {
|
|
try {
|
|
expect(packet.payload).to.equal('processed tcp payload')
|
|
resolve()
|
|
}
|
|
catch(error) {
|
|
reject(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
let [err] = await btc(tcpconsumer.connect.bind(tcpconsumer))(app)
|
|
if (err) reject(err)
|
|
let packet = {payload:'tcp payload'}
|
|
tcpconsumer.send(packet)
|
|
|
|
}) //end promise
|
|
|
|
}) // end unix socket test
|
|
|
|
})
|