Skip to content

Commit 756c847

Browse files
authored
test: be more exact when waiting for peers (#866)
`waitForPeers`, a test helper, currently checks that the *number* of connected peers is expected. This has two problems: - If the number of connected peers is *more than needed*, it will never resolve. - If the *number* of connected peers is correct but the actual peers are different, the promise could resolve prematurely. This snippet highlights the problem: ```javascript const managers = await createManagers(3, t) const [a, b, c] = managers connectPeers(managers) await waitForPeers([a, c]) ``` I think this is a useful change on its own, but will also make [an upcoming change][0] easier. [0]: #865
1 parent 063b446 commit 756c847

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

test-e2e/utils.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,21 @@ export async function invite({
127127
*/
128128
export const waitForPeers = (managers, { waitForDeviceInfo = false } = {}) =>
129129
new Promise((res) => {
130-
const expectedCount = managers.length - 1
130+
const deviceIds = new Set(managers.map((m) => m.deviceId))
131+
131132
const isDone = () =>
132133
managers.every((manager) => {
133-
const { peers } = manager[kRPC]
134-
const connectedPeers = peers.filter(
135-
({ status }) => status === 'connected'
136-
)
137-
return (
138-
connectedPeers.length === expectedCount &&
139-
(!waitForDeviceInfo || connectedPeers.every(({ name }) => !!name))
140-
)
134+
const unconnectedDeviceIds = new Set(deviceIds)
135+
unconnectedDeviceIds.delete(manager.deviceId)
136+
for (const peer of manager[kRPC].peers) {
137+
if (
138+
peer.status === 'connected' &&
139+
(!waitForDeviceInfo || peer.name)
140+
) {
141+
unconnectedDeviceIds.delete(peer.deviceId)
142+
}
143+
}
144+
return unconnectedDeviceIds.size === 0
141145
})
142146

143147
if (isDone()) {

0 commit comments

Comments
 (0)