diff --git a/test/all.js b/test/all.js index 12897122..b2543d64 100644 --- a/test/all.js +++ b/test/all.js @@ -18,8 +18,8 @@ async function runTests () { await import('./pool.js') await import('./relaying.js') await import('./storing.js') - await import('./keet-relay-bug-hunter.js') await import('./keep-alive.js') + await import('./keet-relay-bug-hunter.js') test.resume() } diff --git a/test/fixtures/server-through-relay.js b/test/fixtures/server-through-relay.js index 0de675d5..3f9058b6 100644 --- a/test/fixtures/server-through-relay.js +++ b/test/fixtures/server-through-relay.js @@ -3,28 +3,24 @@ const DHT = require('../../') const publicKey = Buffer.from(process.argv[2], 'hex') const secretKey = Buffer.from(process.argv[3], 'hex') const relayServerPublicKey = Buffer.from(process.argv[4], 'hex') +const bootstrap = JSON.parse(process.argv[5]) const keyPair = { publicKey, secretKey } -main() -console.log('publicKey', publicKey.toString('hex')) -console.log('secretKey', secretKey.toString('hex')) -console.log('relayServerPublicKey', relayServerPublicKey.toString('hex')) +main() async function main () { - const node = new DHT() + const node = new DHT({ bootstrap }) const server = node.createServer({ holepunch: false, // To ensure it relies only on relaying shareLocalAddress: false, // To help ensure it relies only on relaying (otherwise it can connect directly over LAN, without even trying to holepunch) relayThrough: relayServerPublicKey }, socket => { + socket.setKeepAlive(5000) socket - .on('open', () => console.log('socket_opened')) - .on('data', data => { - console.log(`socket_ondata ${data.toString('hex')}`) - // client.destroy() - }) + .on('open', () => console.log('socket_onopen')) .on('close', () => console.log('socket_onclose')) .on('error', err => console.log(`socket_onerror ${err.code}`)) + setTimeout(() => socket.destroy(), 1000 + 10 * Math.random()) }) server.on('open', () => console.log('server_onopen')) server.on('error', err => console.log(`server_onerror ${err.code}`)) diff --git a/test/keet-relay-bug-hunter.js b/test/keet-relay-bug-hunter.js index a6fa7eb4..172ea4ab 100644 --- a/test/keet-relay-bug-hunter.js +++ b/test/keet-relay-bug-hunter.js @@ -10,14 +10,15 @@ const path = require('path') // client.set keep-alive // client - should reconnect when connection dies -test.solo('Client connects to Server and keeps reconnectings - with relay', async t => { +test.solo('Client connects to Server and keeps reconnectings - with relay', { timeout: 0 }, async t => { + t.plan(2000) + 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 }) - t.plan(5) t.teardown(async () => { await a.destroy() await c.destroy() @@ -27,19 +28,16 @@ test.solo('Client connects to Server and keeps reconnectings - with relay', asyn const relay = new RelayServer({ createStream (opts) { - console.log('[relay] createStream()') return a.createRawStream({ ...opts, framed: true }) } }) - const relayServer = a.createServer(function (socket) { - console.log('[relayServer] Got connection') - + const relayServer = a.createServer(socket => { const relaySession = relay.accept(socket, { id: socket.remotePublicKey }) - relaySession.on('pair', (isInitiator) => { - console.log(`[relayServer] on(pair) isInitiator=${isInitiator}`) - }) + // relaySession.on('pair', (isInitiator) => { + // console.log(`[relayServer] on(pair) isInitiator=${isInitiator}`) + // }) relaySession.on('error', (err) => t.comment(err.message)) }) @@ -53,44 +51,42 @@ test.solo('Client connects to Server and keeps reconnectings - with relay', asyn startServer() function startServer () { - const serverProcess = spawn('node', [path.join(__dirname, 'fixtures/server-through-relay.js'), serverPublicKey, serverSecretKey, relayServerPublicKey]) + const serverProcess = spawn('node', [ + path.join(__dirname, 'fixtures/server-through-relay.js'), + serverPublicKey, + serverSecretKey, + relayServerPublicKey, + JSON.stringify(bootstrap) + ]) serverProcess.stderr.on('data', () => t.fail()) serverProcess.stdout.on('data', data => { data = data.toString().trim() - console.log(`[serverProcess] ${data}`) + // console.log(`[serverProcess] ${data}`) const isStarted = data === 'started' - const isSocketConnected = data === 'socket_connected' + const isSocketOpened = data === 'socket_onopen' + const isSocketClosed = data === 'socket_onclose' + const isSocketError = data.startsWith('socket_onerror') if (isStarted) { - t.pass('Server started. Now starting client') - setTimeout(() => startClient(), 1000) - } - if (isSocketConnected) { - t.pass('Client connected. Killing server process in 1 second') - // setTimeout(() => serverProcess.kill('SIGKILL'), 1000) // Wait a bit to make sure the handshake has happened - // setTimeout(() => { - // timedout = true - - // t.pass('After 20 seconds the connection was still open. Closing it now') - // node.destroy() - // }, 24000) + t.pass('[server] Started. Now starting client') + startClient() } + if (isSocketOpened) t.pass('[server] Socket connected') + if (isSocketClosed) t.pass('[server] Socket closed') + if (isSocketError) console.error(data) }) - serverProcess.on('close', () => console.log('[serverProcess] on(close)')) - serverProcess.on('exit', () => console.log('[serverProcess] on(exit)')) } function startClient () { - console.log('[startClient()] serverKeyPair.publicKey', serverKeyPair.publicKey.toString('hex')) - console.log('[startClient()] relayServer.publicKey', relayServer.publicKey.toString('hex')) const client = c.connect(serverKeyPair.publicKey, { relayThrough: relayServer.publicKey }) + client.setKeepAlive(5000) client .on('open', () => { - t.pass('client socket opened') - client.write('hello world') + t.pass('[client] Socket opened') }) .on('close', () => { - t.pass('client socket closed') + t.pass('[client] Socket closed. reconnecting') + startClient() }) - .on('error', err => console.error('client error', err)) + .on('error', err => console.error('[client] error', err)) } })