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}) 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.registerPacketProcessor(async function (packet) { return new Promise((resolve) => { packet.payload = packet.payload +':local' resolve(packet)}) }) let packet = {payload:'tcp payload'} let res = await tcpconsumer_default.send(packet) expect(res.payload).to.equal('8080:tcp payload:local') }) // 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'}) 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(async function (packet) { return new Promise((resolve) => { packet.payload = packet.payload +':local' resolve(packet)}) }) let packet = {payload:'tcp payload'} let res = await tcpconsumer_9080.send(packet) expect(res.payload).to.equal('9080:tcp payload:local') }) // end tcp socket 2 test }) // end describe