*Reformat

This commit is contained in:
Derrick Hammer 2022-11-27 03:37:02 -05:00
parent fcfdd51908
commit bebdc7d0d4
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 47 additions and 47 deletions

76
test.js
View File

@ -1,36 +1,36 @@
const test = require('tape')
const Hyperswarm = require('hyperswarm')
const sodium = require('sodium-universal')
const b4a = require('b4a')
const { default: DHTFlood } = require('./')
const crypto = require('crypto')
const test = require("tape");
const Hyperswarm = require("hyperswarm");
const sodium = require("sodium-universal");
const b4a = require("b4a");
const { default: DHTFlood } = require("./");
const crypto = require("crypto");
const topicName = crypto.randomBytes(10)
const topicName = crypto.randomBytes(10);
test('Broadcast through several peers', (t) => {
const peer1 = createPeer()
const peer2 = createPeer()
const peer3 = createPeer()
t.plan(2)
test("Broadcast through several peers", (t) => {
const peer1 = createPeer();
const peer2 = createPeer();
const peer3 = createPeer();
t.plan(2);
Promise.all([peer1, peer2, peer3]).then((peers) => {
const peer1 = peers.shift()
const peer2 = peers.shift()
const peer3 = peers.shift()
const peer1 = peers.shift();
const peer2 = peers.shift();
const peer3 = peers.shift();
const flood1 = new DHTFlood({ swarm: peer1 })
const flood2 = new DHTFlood({ swarm: peer2 })
const flood3 = new DHTFlood({ swarm: peer3 })
const data = Buffer.from('Hello World')
const flood1 = new DHTFlood({ swarm: peer1 });
const flood2 = new DHTFlood({ swarm: peer2 });
const flood3 = new DHTFlood({ swarm: peer3 });
const data = Buffer.from("Hello World");
flood1.on('message', () => t.error('Got own message'))
flood1.on("message", () => t.error("Got own message"));
flood2.on('message', (message) => {
t.deepEquals(message, data, 'Data got broadcast')
})
flood3.on('message', (message) => {
t.deepEquals(message, data, 'Data got broadcast')
})
flood2.on("message", (message) => {
t.deepEquals(message, data, "Data got broadcast");
});
flood3.on("message", (message) => {
t.deepEquals(message, data, "Data got broadcast");
});
function maybeFlood() {
if (peer1.peers.size === 2) {
@ -42,20 +42,20 @@ test('Broadcast through several peers', (t) => {
peer1.on("connection", maybeFlood);
t.teardown(() => {
[peer1, peer2, peer3].forEach((item) => item.destroy())
})
})
})
[peer1, peer2, peer3].forEach((item) => item.destroy());
});
});
});
async function createPeer () {
const swarm = new Hyperswarm()
await swarm.dht.ready()
await swarm.listen()
async function createPeer() {
const swarm = new Hyperswarm();
await swarm.dht.ready();
await swarm.listen();
const topic = b4a.allocUnsafe(32)
sodium.crypto_generichash(topic, b4a.from(topicName))
const topic = b4a.allocUnsafe(32);
sodium.crypto_generichash(topic, b4a.from(topicName));
swarm.join(topic)
swarm.join(topic);
return swarm
return swarm;
}