Skip to content

Commit

Permalink
test: be more exact when waiting for peers (#866)
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
EvanHahn committed Sep 25, 2024
1 parent 063b446 commit 756c847
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions test-e2e/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,21 @@ export async function invite({
*/
export const waitForPeers = (managers, { waitForDeviceInfo = false } = {}) =>
new Promise((res) => {
const expectedCount = managers.length - 1
const deviceIds = new Set(managers.map((m) => m.deviceId))

const isDone = () =>
managers.every((manager) => {
const { peers } = manager[kRPC]
const connectedPeers = peers.filter(
({ status }) => status === 'connected'
)
return (
connectedPeers.length === expectedCount &&
(!waitForDeviceInfo || connectedPeers.every(({ name }) => !!name))
)
const unconnectedDeviceIds = new Set(deviceIds)
unconnectedDeviceIds.delete(manager.deviceId)
for (const peer of manager[kRPC].peers) {
if (
peer.status === 'connected' &&
(!waitForDeviceInfo || peer.name)
) {
unconnectedDeviceIds.delete(peer.deviceId)
}
}
return unconnectedDeviceIds.size === 0
})

if (isDone()) {
Expand Down

0 comments on commit 756c847

Please sign in to comment.