uci-socket/test/socket.test.mjs

173 lines
4.3 KiB
JavaScript
Raw Normal View History

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 uconsumer2 = new Consumer(USOCKET, {name:'test-uconsumer-2'})
let tcpconsumer = new Consumer({name:'test-tcpconsumer'})
let tcpconsumer2 = new Consumer({port:9080, packetProcessor:'test', name:'test-tcpconsumer-2'})
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
let usocket = {}
let tcpsocket = {}
let tcpsocket2 = {}
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))
})
tcpsocket2 = spawn('node',['-r', '@std/esm', './test/tcpsocket2'])
tcpsocket2.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()
tcpsocket2.kill()
})
it('via unix socket with defaults testing stream JSON packet parser, 10 packets', async function () {
uconsumer.times = 0
return new Promise(async function (resolve, reject) {
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)
}
}
let [err] = await btc(uconsumer.connect)()
if (err) reject(err)
let packet = {payload:'unix payload'}
for (var i = 0; i < 11; i++) {
uconsumer.send(packet)
}
}) //end promise
}) // end unix socket test
it('unix socket with two consumers alternating packets, 10 packets each', async function () {
uconsumer.times = 0
return new Promise(async function (resolve, reject) {
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)
}
}
uconsumer2.processPacket = function (packet) {
return packet
}
let [err] = await btc(uconsumer2.connect)()
console.log('connect error', err)
let packet = {payload:'unix payload'}
for (var i = 0; i < 11; i++) {
uconsumer.send(packet)
uconsumer2.send(packet)
}
}) //end promise
}) // end unix socket test
it('via tcp socket with defaults', async function () {
return new Promise(async function (resolve, reject) {
tcpconsumer.processPacket = function (packet) {
try {
expect(packet.payload).to.equal('tcp processed tcp payload')
resolve()
}
catch(error) {
reject(error)
}
}
let [err] = await btc(tcpconsumer.connect)()
if (err) reject(err)
let packet = {payload:'tcp payload'}
tcpconsumer.send(packet)
}) //end promise
}) // 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
})