diff --git a/test/relaying.js b/test/relaying.js index 76627e28..6794c527 100644 --- a/test/relaying.js +++ b/test/relaying.js @@ -117,3 +117,62 @@ test('relay connections through node, server side', async function (t) { await b.destroy() await c.destroy() }) + +test('relay connections through node, client and server side', async function (t) { + const { bootstrap } = await swarm(t) + + const a = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) + const b = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) + const c = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) + + const lc = t.test('socket lifecycle') + lc.plan(5) + + const aServer = a.createServer({ + shareLocalAddress: false, + connectionRelay: true + }) + + await aServer.listen() + + const bServer = b.createServer({ + shareLocalAddress: false, + relayThrough: aServer.publicKey, + holepunch () { + return false + } + }, function (socket) { + lc.pass('server socket opened') + socket + .on('data', (data) => { + lc.alike(data, Buffer.from('hello world')) + }) + .on('close', () => { + lc.pass('server socket closed') + }) + .end() + }) + + await bServer.listen() + + const bSocket = c.connect(bServer.publicKey, { + fastOpen: false, + localConnection: false, + relayThrough: aServer.publicKey + }) + + bSocket + .on('open', () => { + lc.pass('client socket opened') + }) + .on('close', () => { + lc.pass('client socket closed') + }) + .end('hello world') + + await lc + + await a.destroy() + await b.destroy() + await c.destroy() +})