2018-01-23 16:02:56 -08:00
|
|
|
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 () {
|
2018-01-30 16:59:57 -08:00
|
|
|
let tcpconsumer_default = new Consumer({name:'tcpconsumer', log:false})
|
2018-01-23 16:02:56 -08:00
|
|
|
|
|
|
|
return new Promise(async function (resolve, reject) {
|
|
|
|
|
2018-01-30 16:59:57 -08:00
|
|
|
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
|
2018-02-03 13:33:25 -08:00
|
|
|
tcpconsumer_default._packetProcess = function (packet) {
|
2018-01-23 16:02:56 -08:00
|
|
|
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
|
|
|
|
|
2018-01-25 18:07:45 -08:00
|
|
|
it('with alternate port, and registered consumer processor', async function () {
|
2018-01-23 16:02:56 -08:00
|
|
|
|
2018-01-25 18:07:45 -08:00
|
|
|
let tcpconsumer_9080 = new Consumer({port:9080, name:'tcp-consumer-9080'})
|
2018-01-23 16:02:56 -08:00
|
|
|
|
|
|
|
return new Promise(async function (resolve, reject) {
|
|
|
|
|
2018-01-30 16:59:57 -08:00
|
|
|
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
|
2018-01-25 18:07:45 -08:00
|
|
|
tcpconsumer_9080.registerPacketProcessor(function (packet) {
|
2018-01-23 16:02:56 -08:00
|
|
|
try {
|
|
|
|
expect(packet.payload).to.equal('9080:tcp payload')
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
catch(error) {
|
|
|
|
reject(error)
|
|
|
|
}
|
2018-01-25 18:07:45 -08:00
|
|
|
})
|
2018-01-23 16:02:56 -08:00
|
|
|
|
|
|
|
let packet = {payload:'tcp payload'}
|
|
|
|
tcpconsumer_9080.send(packet)
|
|
|
|
|
|
|
|
}) //end promise
|
|
|
|
|
|
|
|
}) // end tcp socket 2 test
|
|
|
|
|
|
|
|
|
|
|
|
}) // end describe
|