*Refactor test to use events/promises and not a timer
*Add tests for peerHasItem
This commit is contained in:
parent
734a3640e3
commit
1910398d44
32
test.js
32
test.js
|
@ -13,7 +13,7 @@ sodium.crypto_generichash(item1, b4a.from("item1"));
|
|||
sodium.crypto_generichash(item2, b4a.from("item2"));
|
||||
|
||||
test("Basic presence test / data propagation", (t) => {
|
||||
t.plan(6);
|
||||
t.plan(10);
|
||||
|
||||
const peer1 = createPeer();
|
||||
const peer2 = createPeer();
|
||||
|
@ -51,15 +51,39 @@ test("Basic presence test / data propagation", (t) => {
|
|||
p1.removeListener("online", handleOnline);
|
||||
p2.removeListener("online", handleOnline);
|
||||
|
||||
setTimeout(() => {
|
||||
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]));
|
||||
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;
|
||||
|
||||
peer2._allConnections.get(peer1.keyPair.publicKey).end();
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue