Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix race condition if multiple invites are sent #863

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/member-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class MemberApi extends TypedEmitter {
* @param {import('./roles.js').RoleIdForNewInvite} opts.roleId
* @param {string} [opts.roleName]
* @param {string} [opts.roleDescription]
* @param {Buffer} [opts.__testOnlyInviteId] Hard-code the invite ID. Only for tests.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love having a special test-only parameter here but I couldn't think of a more elegant solution.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷

* @returns {Promise<(
* typeof InviteResponse_Decision.ACCEPT |
* typeof InviteResponse_Decision.REJECT |
Expand All @@ -97,7 +98,12 @@ export class MemberApi extends TypedEmitter {
*/
async invite(
deviceId,
{ roleId, roleName = ROLES[roleId]?.name, roleDescription }
{
roleId,
roleName = ROLES[roleId]?.name,
roleDescription,
__testOnlyInviteId,
}
) {
assert(isRoleIdForNewInvite(roleId), 'Invalid role ID for new invite')
assert(
Expand All @@ -120,7 +126,7 @@ export class MemberApi extends TypedEmitter {

abortSignal.throwIfAborted()

const inviteId = crypto.randomBytes(32)
const inviteId = __testOnlyInviteId || crypto.randomBytes(32)
const projectId = projectKeyToId(this.#projectKey)
const projectInviteId = projectKeyToProjectInviteId(this.#projectKey)
const project = await this.#dataTypes.project.getByDocId(projectId)
Expand Down
15 changes: 11 additions & 4 deletions test-e2e/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { fork } from 'node:child_process'
import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import * as v8 from 'node:v8'
import { pEvent } from 'p-event'

import { MapeoManager, roles } from '../src/index.js'
import { kManagerReplicate, kRPC } from '../src/mapeo-manager.js'
import { once } from 'node:events'
import { generate } from '@mapeo/mock-data'
import { valueOf } from '../src/utils.js'
import { randomInt } from 'node:crypto'
import { randomBytes, randomInt } from 'node:crypto'
import { temporaryFile, temporaryDirectory } from 'tempy'
import fsPromises from 'node:fs/promises'
import { kSyncState } from '../src/sync/sync-api.js'
Expand Down Expand Up @@ -100,18 +100,25 @@ export async function invite({
const promises = []

for (const invitee of invitees) {
const inviteId = randomBytes(32)
promises.push(
invitorProject.$member.invite(invitee.deviceId, {
roleId,
roleName,
__testOnlyInviteId: inviteId,
})
)
promises.push(
once(invitee.invite, 'invite-received').then(async ([invite]) => {
(async () => {
const invite = await pEvent(
invitee.invite,
'invite-received',
(invite) => Buffer.from(invite.inviteId, 'hex').equals(inviteId)
)
await (reject
? invitee.invite.reject(invite)
: invitee.invite.accept(invite))
})
})()
)
}

Expand Down
Loading