Skip to content

Commit

Permalink
Add test for keepalive (#168)
Browse files Browse the repository at this point in the history
* Add test for keepalive

* lol

* Add another test for not using keepalive

* Use path
  • Loading branch information
freeall authored Mar 20, 2024
1 parent 18ec3b5 commit 15fa935
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async function runTests () {
await import('./pool.js')
await import('./relaying.js')
await import('./storing.js')
await import('./keep-alive.js')

test.resume()
}
17 changes: 17 additions & 0 deletions test/fixtures/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const DHT = require('../../')

main()

async function main () {
const publicKey = Buffer.from(process.argv[2], 'hex')
const secretKey = Buffer.from(process.argv[3], 'hex')
const keyPair = { publicKey, secretKey }

const node = new DHT()
const server = node.createServer(socket => {
console.log('socket_connected')
})

await server.listen(keyPair)
console.log('started')
}
82 changes: 82 additions & 0 deletions test/keep-alive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const test = require('brittle')
const DHT = require('../')
const { spawn } = require('child_process')
const path = require('path')

// Server is run in a separate proces to make sure that the client doesn't see
// the socket is closed from being run by the same process
test('Client use keepalive to detect disconnect - separated by processes', async t => {
t.plan(5)

const keyPair = DHT.keyPair()
const publicKey = keyPair.publicKey.toString('hex')
const secretKey = keyPair.secretKey.toString('hex')

const serverProcess = spawn('node', [path.join(__dirname, 'fixtures/server.js'), publicKey, secretKey])
serverProcess.stderr.on('data', () => t.fail())
serverProcess.stdout.on('data', data => {
data = data.toString().trim()
const isStarted = data === 'started'
const isSocketConnected = data === 'socket_connected'
if (isStarted) {
t.pass('Server started. Now starting client')
startClient()
}
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
}
})

function startClient () {
const node = new DHT()
const clientSocket = node.connect(publicKey)
clientSocket.setKeepAlive(5000)
clientSocket.on('open', () => t.pass('Client connected'))
clientSocket.on('error', err => t.is(err.code, 'ETIMEDOUT'))
clientSocket.on('close', () => {
t.pass('Client discovered that the connection has been lost')
node.destroy()
})
}
})

test('Client not using keepalive does not detect disconnect - separated by processes', async t => {
t.plan(4)

const keyPair = DHT.keyPair()
const publicKey = keyPair.publicKey.toString('hex')
const secretKey = keyPair.secretKey.toString('hex')

const serverProcess = spawn('node', [path.join(__dirname, 'fixtures/server.js'), publicKey, secretKey])
serverProcess.stderr.on('data', () => t.fail())
serverProcess.stdout.on('data', data => {
data = data.toString().trim()
const isStarted = data === 'started'
const isSocketConnected = data === 'socket_connected'
if (isStarted) {
t.pass('Server started. Now starting client')
startClient()
}
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)
}
})

let timedout = false
const node = new DHT()

function startClient () {
const clientSocket = node.connect(publicKey)
clientSocket.on('open', () => t.pass('Client connected'))
clientSocket.on('error', () => !timedout && t.fail())
clientSocket.on('close', () => !timedout && t.fail())
}
})

0 comments on commit 15fa935

Please sign in to comment.