diff options
author | Alex <alxmorais8@msn.com> | 2021-07-24 01:05:25 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-24 01:05:25 +0300 |
commit | 39bb33c3cf694cdee45378ea4b30c66c93576d2a (patch) | |
tree | 654d454bbcc6bba1deab2195ccf6bcbc2bbe4f60 /test | |
parent | 524618edde211a2ce2c9d1e40f68a091699442fd (diff) |
feat: add speed limit to client (#2062)
* Add speed limit to client
* Fix standard
* Update docs/api.md
* Add changes from PR feedback
Co-authored-by: Kadu Diógenes <kadu@fnix.com.br>
Co-authored-by: Ivan Gorbanev <ivang@van.work>
Co-authored-by: ultimate-tester <jordimueters@hotmail.com>
Co-authored-by: Julen Garcia Leunda <hicom150@gmail.com>
Co-authored-by: Niklas Johansson <niklas.y.johansson@se.abb.com>
Co-authored-by: ThaUnknown <kapi.skowronek@gmail.com>
Co-authored-by: Diego RodrÃguez Baquero <github@diegorbaquero.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/node/limit-download-upload.js | 82 | ||||
-rw-r--r-- | test/node/limit-methods.js | 88 |
2 files changed, 170 insertions, 0 deletions
diff --git a/test/node/limit-download-upload.js b/test/node/limit-download-upload.js new file mode 100644 index 0000000..0f595ba --- /dev/null +++ b/test/node/limit-download-upload.js @@ -0,0 +1,82 @@ +const fixtures = require('webtorrent-fixtures') +const test = require('tape') +const WebTorrent = require('../../') +const MemoryChunkStore = require('memory-chunk-store') + +const DOWNLOAD_SPEED_LIMIT = 200 * 1000 // 200 KB/s +const UPLOAD_SPEED_LIMIT = 200 * 1000 // 200 KB/s + +function testSpeed (t, downloaderOpts, uploaderOpts, cb) { + const client1 = new WebTorrent({ dht: false, tracker: false, ...downloaderOpts }) + const client2 = new WebTorrent({ dht: false, tracker: false, ...uploaderOpts }) + + client1.on('error', err => { t.fail(err) }) + client1.on('warning', err => { t.fail(err) }) + + client2.on('error', err => { t.fail(err) }) + client2.on('warning', err => { t.fail(err) }) + + const downloadSpeeds = [] + const uploadSpeeds = [] + + // Start seeding + client2.seed(fixtures.leaves.content, { + name: 'Leaves of Grass by Walt Whitman.epub', + announce: [] + }, torrent => { + torrent.on('upload', () => { + uploadSpeeds.push(torrent.uploadSpeed) + }) + }) + + client2.on('listening', () => { + // Start downloading + const torrent = client1.add(fixtures.leaves.parsedTorrent.infoHash, { store: MemoryChunkStore }) + + // Manually connect peers + torrent.addPeer(`127.0.0.1:${client2.address().port}`) + + torrent.on('download', () => { + downloadSpeeds.push(torrent.downloadSpeed) + }) + + torrent.on('done', () => { + cb(downloadSpeeds, uploadSpeeds) + + client1.destroy(err => { t.error(err, 'client 1 destroyed') }) + client2.destroy(err => { t.error(err, 'client 2 destroyed') }) + }) + }) +} + +test('Limit download speed by constructor when tcp connection', t => { + t.plan(3) + + testSpeed(t, { downloadLimit: DOWNLOAD_SPEED_LIMIT }, {}, downloadSpeeds => { + t.ok(downloadSpeeds.every(downloadSpeed => downloadSpeed <= DOWNLOAD_SPEED_LIMIT)) + }) +}) + +test('Limit upload speed by constructor when tcp connection', t => { + t.plan(3) + + testSpeed(t, {}, { uploadLimit: UPLOAD_SPEED_LIMIT }, (_, uploadSpeeds) => { + t.ok(uploadSpeeds.every(uploadSpeed => uploadSpeed <= UPLOAD_SPEED_LIMIT)) + }) +}) + +test('Limit download speed by constructor when utp connection', t => { + t.plan(3) + + testSpeed(t, { utp: true, downloadLimit: DOWNLOAD_SPEED_LIMIT }, { utp: true }, downloadSpeeds => { + t.ok(downloadSpeeds.every(downloadSpeed => downloadSpeed <= DOWNLOAD_SPEED_LIMIT)) + }) +}) + +test('Limit upload speed by constructor when utp connection', t => { + t.plan(3) + + testSpeed(t, { utp: true }, { utp: true, uploadLimit: UPLOAD_SPEED_LIMIT }, (_, uploadSpeeds) => { + t.ok(uploadSpeeds.every(uploadSpeed => uploadSpeed <= UPLOAD_SPEED_LIMIT)) + }) +}) diff --git a/test/node/limit-methods.js b/test/node/limit-methods.js new file mode 100644 index 0000000..58ad573 --- /dev/null +++ b/test/node/limit-methods.js @@ -0,0 +1,88 @@ +const fixtures = require('webtorrent-fixtures') +const test = require('tape') +const WebTorrent = require('../../') +const MemoryChunkStore = require('memory-chunk-store') + +const DOWNLOAD_SPEED_LIMIT = 200 * 1000 // 200 KB/s +const UPLOAD_SPEED_LIMIT = 200 * 1000 // 200 KB/s + +function testSpeed (t, downloaderOpts, uploaderOpts, cb) { + const { downloadLimit, ...restDownloaderOpts } = downloaderOpts + const { uploadLimit, ...restUploaderOpts } = uploaderOpts + + const client1 = new WebTorrent({ dht: false, tracker: false, ...restDownloaderOpts }) + const client2 = new WebTorrent({ dht: false, tracker: false, ...restUploaderOpts }) + + if (downloadLimit) client1.throttleDownload(downloadLimit) + if (uploadLimit) client2.throttleUpload(uploadLimit) + + client1.on('error', err => { t.fail(err) }) + client1.on('warning', err => { t.fail(err) }) + + client2.on('error', err => { t.fail(err) }) + client2.on('warning', err => { t.fail(err) }) + + const downloadSpeeds = [] + const uploadSpeeds = [] + + // Start seeding + client2.seed(fixtures.leaves.content, { + name: 'Leaves of Grass by Walt Whitman.epub', + announce: [] + }, torrent => { + torrent.on('upload', () => { + uploadSpeeds.push(torrent.uploadSpeed) + }) + }) + + client2.on('listening', () => { + // Start downloading + const torrent = client1.add(fixtures.leaves.parsedTorrent.infoHash, { store: MemoryChunkStore }) + + // Manually connect peers + torrent.addPeer(`127.0.0.1:${client2.address().port}`) + + torrent.on('download', () => { + downloadSpeeds.push(torrent.downloadSpeed) + }) + + torrent.on('done', () => { + cb(downloadSpeeds, uploadSpeeds) + + client1.destroy(err => { t.error(err, 'client 1 destroyed') }) + client2.destroy(err => { t.error(err, 'client 2 destroyed') }) + }) + }) +} + +test('Limit download speed by methods when tcp connection', t => { + t.plan(3) + + testSpeed(t, { downloadLimit: DOWNLOAD_SPEED_LIMIT }, {}, downloadSpeeds => { + t.ok(downloadSpeeds.every(downloadSpeed => downloadSpeed <= DOWNLOAD_SPEED_LIMIT)) + }) +}) + +test('Limit upload speed by methods when tcp connection', t => { + t.plan(3) + + testSpeed(t, {}, { uploadLimit: UPLOAD_SPEED_LIMIT }, (_, uploadSpeeds) => { + t.ok(uploadSpeeds.every(uploadSpeed => uploadSpeed <= UPLOAD_SPEED_LIMIT)) + }) +}) + +test('Limit download speed by methods when utp connection', t => { + t.plan(3) + + testSpeed(t, { utp: true, downloadLimit: DOWNLOAD_SPEED_LIMIT }, { utp: true }, downloadSpeeds => { + t.ok(downloadSpeeds.every(downloadSpeed => downloadSpeed <= DOWNLOAD_SPEED_LIMIT)) + }) +}) + +test('Limit upload speed by methods when utp connection', t => { + t.plan(3) + + testSpeed(t, { utp: true }, { utp: true, uploadLimit: UPLOAD_SPEED_LIMIT }, (_, uploadSpeeds) => { + t.ok(uploadSpeeds.every(uploadSpeed => uploadSpeed <= UPLOAD_SPEED_LIMIT)) + }) +}) |