Skip to content

Commit

Permalink
replaced dgram with udx socket in tests (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: rafapaezbas <[email protected]>
  • Loading branch information
rafapaezbas and rafapaezbas authored Jan 9, 2024
1 parent adab635 commit cc4aeeb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
32 changes: 15 additions & 17 deletions test/helpers/proxy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const dgram = require('dgram')
const { inspect } = require('util')
const UDX = require('../..')

// from udx.h

Expand All @@ -16,7 +15,8 @@ module.exports = function proxy ({ from, to, bind } = {}, drop) {
from = toPort(from)
to = toPort(to)

const socket = dgram.createSocket('udp4')
const u = new UDX()
const socket = u.createSocket()

socket.bind(bind || 0)

Expand All @@ -31,15 +31,11 @@ module.exports = function proxy ({ from, to, bind } = {}, drop) {

function fwd (dropping) {
if (dropping === true) return
socket.send(buf, 0, buf.byteLength, port, '127.0.0.1')
socket.send(buf, port, '127.0.0.1')
}
})

return new Promise((resolve) => {
socket.on('listening', function () {
resolve(socket)
})
})
return socket
}

function toPort (n) {
Expand All @@ -63,7 +59,7 @@ function prettyPrint (pkt, { peer }, opts) {
s += ': '

if (pkt.protocol !== 'udx') {
s += style('unknown', 'symbol') + ' data=' + inspect(pkt.data)
s += style('unknown', 'symbol') + ' data=' + opts.stylize(pkt.data)
return s
}

Expand All @@ -78,13 +74,13 @@ function prettyPrint (pkt, { peer }, opts) {
if (!flags.length) flags.push(style('state', 'special'))

s += flags.join('+') + ' '
s += 'stream=' + inspect(pkt.stream, opts) + ' '
s += 'recv=' + inspect(pkt.recv, opts) + ' '
s += 'seq=' + inspect(pkt.seq, opts) + ' '
s += 'ack=' + inspect(pkt.ack, opts) + ' '
s += 'stream=' + opts.stylize(pkt.stream, opts) + ' '
s += 'recv=' + opts.stylize(pkt.recv, opts) + ' '
s += 'seq=' + opts.stylize(pkt.seq, opts) + ' '
s += 'ack=' + opts.stylize(pkt.ack, opts) + ' '

if (pkt.additionalHeader.byteLength) s += 'additional = ' + inspect(pkt.additionalHeader, opts) + ' '
if (pkt.data.byteLength) s += 'data=' + inspect(pkt.data, opts)
if (pkt.additionalHeader.byteLength) s += 'additional = ' + opts.stylize(pkt.additionalHeader, opts) + ' '
if (pkt.data.byteLength) s += 'data=' + opts.stylize(pkt.data, opts)

s = s.trim()

Expand All @@ -97,6 +93,8 @@ function parsePacket (buf, source) {
const type = buf[2]
const dataOffset = buf[3]

const inspect = Symbol.for('nodejs.util.inspect.custom')

return {
protocol: 'udx',
version: buf[1],
Expand All @@ -111,7 +109,7 @@ function parsePacket (buf, source) {
ack: buf.readUint32LE(16),
additionalHeader: buf.subarray(UDX_HEADER_SIZE, UDX_HEADER_SIZE + dataOffset),
data: buf.subarray(UDX_HEADER_SIZE + dataOffset),
[inspect.custom] (depth, opts) {
[inspect] (depth, opts) {
return prettyPrint(this, source, opts)
}
}
Expand Down
4 changes: 3 additions & 1 deletion test/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ test('throw in message callback', async function (t) {

b.send(b4a.from('hello'), a.address().port)

process.once('uncaughtException', async (err) => {
const handler = typeof process !== 'undefined' ? process : Bare // eslint-disable-line

handler.once('uncaughtException', async (err) => {
t.is(err.message, 'boom')

await a.close()
Expand Down

0 comments on commit cc4aeeb

Please sign in to comment.