uci-socket/test/tcp.test.mjs

87 lines
2.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 delay = time => new Promise(res=>setTimeout(()=>res(),time))
let tcpsocket_default = {}
let tcpsocket_9080 = {}
describe('Connects and Processes a payload in a JSON packet via TCP Socket', function(){
before(async function(){
tcpsocket_default = spawn('node',['-r', '@std/esm', './test/sockets/tcpsocket-default'])
tcpsocket_default.stdout.on('data', function(buf) {
console.log('[Socket]', String(buf))
})
tcpsocket_9080 = spawn('node',['-r', '@std/esm', './test/sockets/tcpsocket-9080'])
tcpsocket_9080.stdout.on('data', function(buf) {
console.log('[Socket]', String(buf))
})
await delay(500) // wait for sockets to get going
})
after(async function(){
tcpsocket_default.kill()
tcpsocket_9080.kill()
})
it('with default host and port', async function () {
let tcpconsumer_default = new Consumer({name:'tcpconsumer'})
return new Promise(async function (resolve, reject) {
tcpconsumer_default.processPacket = function (packet) {
try {
expect(packet.payload).to.equal('8080:tcp payload')
resolve()
}
catch(error) {
reject(error)
}
}
let [err] = await btc(tcpconsumer_default.connect)()
if (err) reject(err)
let packet = {payload:'tcp payload'}
tcpconsumer_default.send(packet)
}) //end promise
}) // end tcp socket test
it('with alternate port, and passed consumer processor', async function () {
let tcpconsumer_9080 = new Consumer({packetProcessor: 'test', port:9080, name:'tcp-consumer-9080'})
return new Promise(async function (resolve, reject) {
tcpconsumer_9080.test = function (packet) {
try {
expect(packet.payload).to.equal('9080:tcp payload')
resolve()
}
catch(error) {
reject(error)
}
}
let [err] = await btc(tcpconsumer_9080.connect)()
if (err) reject(err)
let packet = {payload:'tcp payload'}
tcpconsumer_9080.send(packet)
}) //end promise
}) // end tcp socket 2 test
}) // end describe