|
| 1 | +var Buffer = require('safe-buffer').Buffer |
| 2 | +var Client = require('../') |
| 3 | +var common = require('./common') |
| 4 | +var test = require('tape') |
| 5 | +var electronWebrtc = require('electron-webrtc') |
| 6 | + |
| 7 | +var wrtc |
| 8 | + |
| 9 | +var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' |
| 10 | +var peerId = Buffer.from('01234567890123456789') |
| 11 | +var peerId2 = Buffer.from('12345678901234567890') |
| 12 | +var peerId3 = Buffer.from('23456789012345678901') |
| 13 | + |
| 14 | +function serverTest (t, serverType, serverFamily) { |
| 15 | + t.plan(10) |
| 16 | + |
| 17 | + var hostname = serverFamily === 'inet6' |
| 18 | + ? '[::1]' |
| 19 | + : '127.0.0.1' |
| 20 | + |
| 21 | + var opts = { |
| 22 | + serverType: serverType, |
| 23 | + peersCacheLength: 2 // LRU cache can only contain a max of 2 peers |
| 24 | + } |
| 25 | + |
| 26 | + common.createServer(t, opts, function (server) { |
| 27 | + // Not using announceUrl param from `common.createServer()` since we |
| 28 | + // want to control IPv4 vs IPv6. |
| 29 | + var port = server[serverType].address().port |
| 30 | + var announceUrl = serverType + '://' + hostname + ':' + port + '/announce' |
| 31 | + |
| 32 | + var client1 = new Client({ |
| 33 | + infoHash: infoHash, |
| 34 | + announce: [ announceUrl ], |
| 35 | + peerId: peerId, |
| 36 | + port: 6881, |
| 37 | + wrtc: wrtc |
| 38 | + }) |
| 39 | + if (serverType === 'ws') common.mockWebsocketTracker(client1) |
| 40 | + |
| 41 | + client1.start() |
| 42 | + |
| 43 | + client1.once('update', function (data) { |
| 44 | + var client2 = new Client({ |
| 45 | + infoHash: infoHash, |
| 46 | + announce: [ announceUrl ], |
| 47 | + peerId: peerId2, |
| 48 | + port: 6882, |
| 49 | + wrtc: wrtc |
| 50 | + }) |
| 51 | + if (serverType === 'ws') common.mockWebsocketTracker(client2) |
| 52 | + |
| 53 | + client2.start() |
| 54 | + |
| 55 | + client2.once('update', function (data) { |
| 56 | + server.getSwarm(infoHash, function (err, swarm) { |
| 57 | + t.error(err) |
| 58 | + |
| 59 | + t.equal(swarm.complete + swarm.incomplete, 2) |
| 60 | + |
| 61 | + // Ensure that first peer is evicted when a third one is added |
| 62 | + var evicted = false |
| 63 | + swarm.peers.once('evict', function (evictedPeer) { |
| 64 | + t.equal(evictedPeer.value.peerId, peerId.toString('hex')) |
| 65 | + t.equal(swarm.complete + swarm.incomplete, 2) |
| 66 | + evicted = true |
| 67 | + }) |
| 68 | + |
| 69 | + var client3 = new Client({ |
| 70 | + infoHash: infoHash, |
| 71 | + announce: [ announceUrl ], |
| 72 | + peerId: peerId3, |
| 73 | + port: 6880, |
| 74 | + wrtc: wrtc |
| 75 | + }) |
| 76 | + if (serverType === 'ws') common.mockWebsocketTracker(client3) |
| 77 | + |
| 78 | + client3.start() |
| 79 | + |
| 80 | + client3.once('update', function (data) { |
| 81 | + t.ok(evicted, 'client1 was evicted from server before client3 gets response') |
| 82 | + t.equal(swarm.complete + swarm.incomplete, 2) |
| 83 | + |
| 84 | + client1.destroy(function () { |
| 85 | + t.pass('client1 destroyed') |
| 86 | + }) |
| 87 | + |
| 88 | + client2.destroy(function () { |
| 89 | + t.pass('client3 destroyed') |
| 90 | + }) |
| 91 | + |
| 92 | + client3.destroy(function () { |
| 93 | + t.pass('client3 destroyed') |
| 94 | + }) |
| 95 | + |
| 96 | + server.close(function () { |
| 97 | + t.pass('server destroyed') |
| 98 | + }) |
| 99 | + }) |
| 100 | + }) |
| 101 | + }) |
| 102 | + }) |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +test('evict: ipv4 server', function (t) { |
| 107 | + serverTest(t, 'http', 'inet') |
| 108 | +}) |
| 109 | + |
| 110 | +test('evict: http ipv6 server', function (t) { |
| 111 | + serverTest(t, 'http', 'inet6') |
| 112 | +}) |
| 113 | + |
| 114 | +test('evict: udp server', function (t) { |
| 115 | + serverTest(t, 'udp', 'inet') |
| 116 | +}) |
| 117 | + |
| 118 | +test('evict: ws server', function (t) { |
| 119 | + wrtc = electronWebrtc() |
| 120 | + wrtc.electronDaemon.once('ready', function () { |
| 121 | + serverTest(t, 'ws', 'inet') |
| 122 | + }) |
| 123 | + t.once('end', function () { |
| 124 | + wrtc.close() |
| 125 | + }) |
| 126 | +}) |
0 commit comments