From 1dbc95cbdc2e059c5e3c6713bfe1b2d7823ac0c1 Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Thu, 9 Jun 2016 01:28:49 +0200 Subject: [PATCH] Add pruning to server tests. Issue #4 --- test/server.js | 56 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/test/server.js b/test/server.js index d8e2f4d..706cb04 100644 --- a/test/server.js +++ b/test/server.js @@ -12,9 +12,10 @@ var test = require('tape') var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' var peerId = Buffer.from('01234567890123456789') var peerId2 = Buffer.from('12345678901234567890') +var peerId3 = Buffer.from('23456789012345678901') function serverTest (t, serverType, serverFamily) { - t.plan(30) + t.plan(32) var hostname = serverFamily === 'inet6' ? '[::1]' @@ -23,7 +24,12 @@ function serverTest (t, serverType, serverFamily) { ? '::1' : '127.0.0.1' - common.createServer(t, serverType, function (server) { + var opts = { + serverType: serverType, + peersCacheLength: 2 + } + + common.createServer(t, opts, function (server) { var port = server[serverType].address().port var announceUrl = serverType + '://' + hostname + ':' + port + '/announce' @@ -103,22 +109,42 @@ function serverTest (t, serverType, serverFamily) { client2.once('peer', function (addr) { t.ok(addr === hostname + ':6881' || addr === hostname + ':6882' || addr.id === peerId.toString('hex')) - client2.stop() - client2.once('update', function (data) { - t.equal(data.announce, announceUrl) - t.equal(data.complete, 1) - t.equal(data.incomplete, 0) - client2.destroy() + swarm.peers.once('evict', function (evicted) { + t.equals(evicted.value.peerId, peerId.toString('hex')) + }) + var client3 = new Client({ + infoHash: infoHash, + announce: [ announceUrl ], + peerId: peerId3, + port: 6880 + // wrtc: wrtc + }) + client3.start() - client1.stop() - client1.once('update', function (data) { + server.once('start', function () { + t.pass('got start message from client3') + }) + + client3.once('update', function () { + client2.stop() + client2.once('update', function (data) { t.equal(data.announce, announceUrl) - t.equal(data.complete, 0) - t.equal(data.incomplete, 0) + t.equal(data.complete, 1) + t.equal(data.incomplete, 1) + client2.destroy() - client1.destroy(function () { - server.close() - // if (serverType === 'ws') wrtc.close() + client3.stop() + client3.once('update', function (data) { + t.equal(data.announce, announceUrl) + t.equal(data.complete, 1) + t.equal(data.incomplete, 0) + + client3.destroy(function () { + client1.destroy(function () { + server.close() + }) + // if (serverType === 'ws') wrtc.close() + }) }) }) })