From f6b5543ee25ef30346738f8f1e85b41bde648a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Wed, 22 Nov 2023 14:36:20 +0100 Subject: [PATCH] Reduce test dependencies (#162) --- package.json | 4 +--- test/chaos.js | 13 +++++-------- test/firewall.js | 2 +- test/helpers/index.js | 3 +++ test/retry-timer.js | 2 +- test/swarm.js | 2 +- 6 files changed, 12 insertions(+), 14 deletions(-) create mode 100644 test/helpers/index.js diff --git a/package.json b/package.json index 4604ea2..8f0f3c6 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,7 @@ }, "devDependencies": { "brittle": "^3.0.2", - "hypercore-crypto": "^2.3.0", - "math-random-seed": "^2.0.0", - "nonsynchronous": "^1.2.0", + "hypercore-crypto": "^3.4.0", "standard": "^17.0.0" }, "scripts": { diff --git a/test/chaos.js b/test/chaos.js index be47986..173b18b 100644 --- a/test/chaos.js +++ b/test/chaos.js @@ -1,8 +1,7 @@ const test = require('brittle') const crypto = require('hypercore-crypto') -const random = require('math-random-seed') -const { timeout } = require('nonsynchronous') const createTestnet = require('hyperdht/testnet') +const { timeout } = require('./helpers') const Hyperswarm = require('..') @@ -18,7 +17,6 @@ test('chaos - recovers after random disconnections (takes ~60s)', async (t) => { const { bootstrap } = await createTestnet(3, t.teardown) - const SEED = 'hyperswarm v3' const NUM_SWARMS = 10 const NUM_TOPICS = 15 const NUM_FORCE_DISCONNECTS = 30 @@ -31,7 +29,6 @@ test('chaos - recovers after random disconnections (takes ~60s)', async (t) => { const topics = [] const connections = [] const peersBySwarm = new Map() - const rand = random(SEED) for (let i = 0; i < NUM_SWARMS; i++) { const swarm = new Hyperswarm({ bootstrap, backoffs: BACKOFFS, jitter: 0 }) @@ -60,10 +57,10 @@ test('chaos - recovers after random disconnections (takes ~60s)', async (t) => { } for (const topic of topics) { - const numSwarms = Math.round(rand() * NUM_SWARMS) + const numSwarms = Math.round(Math.random() * NUM_SWARMS) const topicSwarms = [] for (let i = 0; i < numSwarms; i++) { - topicSwarms.push(swarms[Math.floor(rand() * NUM_SWARMS)]) + topicSwarms.push(swarms[Math.floor(Math.random() * NUM_SWARMS)]) } for (const swarm of topicSwarms) { const peers = peersBySwarm.get(swarm) @@ -80,10 +77,10 @@ test('chaos - recovers after random disconnections (takes ~60s)', async (t) => { // Randomly destroy connections during the chaos period. for (let i = 0; i < NUM_FORCE_DISCONNECTS; i++) { - const timeout = Math.floor(rand() * CHAOS_DURATION) // Leave a lot of room at the end for reestablishing connections (timeouts) + const timeout = Math.floor(Math.random() * CHAOS_DURATION) // Leave a lot of room at the end for reestablishing connections (timeouts) setTimeout(() => { if (!connections.length) return - const idx = Math.floor(rand() * connections.length) + const idx = Math.floor(Math.random() * connections.length) const conn = connections[idx] conn.destroy() }, timeout) diff --git a/test/firewall.js b/test/firewall.js index e2551d9..9a4486c 100644 --- a/test/firewall.js +++ b/test/firewall.js @@ -1,6 +1,6 @@ const test = require('brittle') -const { timeout } = require('nonsynchronous') const createTestnet = require('hyperdht/testnet') +const { timeout } = require('./helpers') const Hyperswarm = require('..') diff --git a/test/helpers/index.js b/test/helpers/index.js new file mode 100644 index 0000000..459ab63 --- /dev/null +++ b/test/helpers/index.js @@ -0,0 +1,3 @@ +exports.timeout = function timeout (ms) { + return new Promise((resolve) => setTimeout(resolve, ms)) +} diff --git a/test/retry-timer.js b/test/retry-timer.js index fce6bfd..7b5fc02 100644 --- a/test/retry-timer.js +++ b/test/retry-timer.js @@ -1,6 +1,6 @@ const test = require('brittle') const crypto = require('hypercore-crypto') -const { timeout } = require('nonsynchronous') +const { timeout } = require('./helpers') const RetryTimer = require('../lib/retry-timer') const PeerInfo = require('../lib/peer-info') diff --git a/test/swarm.js b/test/swarm.js index 6e500cf..7c00a39 100644 --- a/test/swarm.js +++ b/test/swarm.js @@ -1,6 +1,6 @@ const test = require('brittle') -const { timeout } = require('nonsynchronous') const createTestnet = require('hyperdht/testnet') +const { timeout } = require('./helpers') const Hyperswarm = require('..')