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 + '/sockets/test.sock' const SOCKET_FILE = 'usocket-app-default-name' const delay = time => new Promise(res=>setTimeout(()=>res(),time)) let socket = {} describe('Connects and Processes a payload via Unix Socket using JSON packet with passed processor using default name', function(){ before(async function(){ socket = spawn('node',['-r', '@std/esm', './test/sockets/'+SOCKET_FILE]) socket.stdout.on('data', function(buf) { console.log('[Socket]', String(buf)) }) await delay(500) // wait for sockets to get going }) // beforeEach(async function(){ // let consumer = new Consumer(USOCKET, {name:'unix-consumer'}) // }) after(async function(){ socket.kill() }) it('Tests JSON packet procssing, 10 packets', async function () { let consumer = new Consumer(USOCKET, {name:'unix-consumer'}) return new Promise(async function (resolve, reject) { setTimeout(() =>{ reject('10 packets not received in time')},1900) let app = {} app.times = 0 app.processPacket = function (packet) { this.times++ // console.log('times=',this.times,'\n',packet.payload) if (this.times!==11) return consumer.end() consumer.destroy() try { expect(packet.payload).to.equal('using passed processor with default name unix payload') resolve() } catch(error) { reject(error) } } let [err] = await btc(consumer.connect)(app) if (err) reject(err) let packet = {payload:'unix payload'} for (var i = 0; i < 11; i++) { consumer.send(packet) } }) //end promise }) // end unix socket test it('unix socket with two consumers alternating packets, 10 packets each, alt consumer processor name', async function () { let consumer = new Consumer(USOCKET, {name:'unix-consumer'}) let consumer2 = new Consumer(USOCKET, {name:'unix-consumer2'}) return new Promise(async function (resolve, reject) { setTimeout(() =>{ reject('10 packets not received in time')},1900) let app = {} app.times = 0 app.processPacket = function (packet) { this.times++ if (this.times!==11) return try { expect(packet.payload).to.equal('using passed processor with default name consumer 1 unix payload') resolve() } catch(error) { reject(error) } } consumer2.processPacket = function (packet) { return packet } let [err] = await btc(consumer.connect)(app) if (err) reject(err) let [err2] = await btc(consumer2.connect)() if (err2) reject(err2) let packet1 = {payload:'consumer 1 unix payload'} let packet2 = {payload:'consumer2 unix payload'} for (var i = 0; i < 11; i++) { consumer.send(packet1) consumer2.send(packet2) } }) //end promise }) // end unix socket test }) // end describe