uci-socket/test/tcp.test.mjs

94 lines
2.4 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'
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))
})
})
after(async function(){
tcpsocket_default.kill()
tcpsocket_9080.kill()
})
it('with default host and port', async function () {
let tcpconsumer_default = new Consumer({name:'tcpconsumer', log:false})
return new Promise(async function (resolve, reject) {
let [err] = await btc(tcpconsumer_default.connect)()
if (err) {
console.log('unable to connect to socket to start test', tcpconsumer_default.port)
process.kill(process.pid, 'SIGTERM')
}
tcpconsumer_default._packetProcess = function (packet) {
try {
expect(packet.payload).to.equal('8080:tcp payload')
resolve()
}
catch(error) {
reject(error)
}
}
let packet = {payload:'tcp payload'}
tcpconsumer_default.send(packet)
}) //end promise
}) // end tcp socket test
it('with alternate port, and registered consumer processor', async function () {
let tcpconsumer_9080 = new Consumer({port:9080, name:'tcp-consumer-9080'})
return new Promise(async function (resolve, reject) {
let [err] = await btc(tcpconsumer_9080.connect)()
if (err) {
console.log('unable to connect to socket to start test', tcpconsumer_9080.port)
process.kill(process.pid, 'SIGTERM')
}
tcpconsumer_9080.registerPacketProcessor(function (packet) {
try {
expect(packet.payload).to.equal('9080:tcp payload')
resolve()
}
catch(error) {
reject(error)
}
})
let packet = {payload:'tcp payload'}
tcpconsumer_9080.send(packet)
}) //end promise
}) // end tcp socket 2 test
}) // end describe