*Refactor test to use events/promises and not a timer

*Add tests for peerHasItem
This commit is contained in:
Derrick Hammer 2022-11-24 01:50:11 -05:00
parent 734a3640e3
commit 1910398d44
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 29 additions and 5 deletions

32
test.js
View File

@ -13,7 +13,7 @@ sodium.crypto_generichash(item1, b4a.from("item1"));
sodium.crypto_generichash(item2, b4a.from("item2")); sodium.crypto_generichash(item2, b4a.from("item2"));
test("Basic presence test / data propagation", (t) => { test("Basic presence test / data propagation", (t) => {
t.plan(6); t.plan(10);
const peer1 = createPeer(); const peer1 = createPeer();
const peer2 = createPeer(); const peer2 = createPeer();
@ -51,15 +51,39 @@ test("Basic presence test / data propagation", (t) => {
p1.removeListener("online", handleOnline); p1.removeListener("online", handleOnline);
p2.removeListener("online", handleOnline); p2.removeListener("online", handleOnline);
setTimeout(() => {
t.pass("Seeing everyone online"); t.pass("Seeing everyone online");
t.equals(true, p2.allCache.includes(peerData1[0])); let p1Resolve;
let p2Resolve;
let p1Promise = new Promise((resolve) => {
p1Resolve = resolve;
});
let p2Promise = new Promise((resolve) => {
p2Resolve = resolve;
});
p1.on("item-added", () => {
t.equals(true, p1.allCache.includes(peerData2[0])); t.equals(true, p1.allCache.includes(peerData2[0]));
p1Resolve();
});
p2.on("item-added", () => {
t.equals(true, p2.allCache.includes(peerData1[0]));
p2Resolve();
});
Promise.all([p1Promise, p2Promise]).then(() => {
t.equals(true, p1.peerHasItem(peer1.keyPair.publicKey, item1));
t.equals(true, p2.peerHasItem(peer1.keyPair.publicKey, item1));
t.equals(true, p1.peerHasItem(peer2.keyPair.publicKey, item2));
t.equals(true, p2.peerHasItem(peer2.keyPair.publicKey, item2));
hasFinished = true; hasFinished = true;
peer2._allConnections.get(peer1.keyPair.publicKey).end(); peer2._allConnections.get(peer1.keyPair.publicKey).end();
}, 1000); });
} }
} }