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

Conversation

EvanHahn
Copy link
Contributor

We have a test helper, invite, that invites people to projects. It works like this:

  1. Tell the invitee(s) to accept any invite they receive.
  2. Send the invite.

This is fine for most of our tests, where only one invite will be sent to each manager. But we have at least one test where multiple invites are sent.

That can cause a race condition. Imagine the following scenario where invite is called twice:

  1. Tell the invitee to accept any invite they receive (listener 1).
  2. Tell the invitee to accept any invite they receive (listener 2).
  3. Send the first invite.
  4. Both listeners fire, sending one valid "accept" response and one that gets dropped.
  5. Send the second invite. There are no listeners, and the test hangs.

This commit changes invite. Now, it works like this:

  1. Generate an invite ID.
  2. Tell the invitee(s) to accept any invite with that ID.
  3. Send the invite.

As far as I know, this isn't a problem today. But an upcoming test change causes this to happen much more reliably, so I think this is worth fixing. (I think it's worth fixing even without that upcoming change. Also, I tested this with those changes and it does, indeed, fix hung tests.)

We have a test helper, `invite`, that invites people to projects. It
works like this:

1. Tell the invitee(s) to accept any invite they receive.
2. Send the invite.

This is fine for most of our tests, where only one invite will be sent
to each manager. But we have [at least one test][0] where multiple
invites are sent.

That can cause a race condition. Imagine the following scenario where
`invite` is called twice:

1. Tell the invitee to accept any invite they receive (listener 1).
2. Tell the invitee to accept any invite they receive (listener 2).
3. Send the first invite.
4. Both listeners fire, sending one valid "accept" response and one that
   gets dropped.
4. Send the second invite. There are no listeners, and the test hangs.

This commit changes `invite`. Now, it works like this:

1. Generate an invite ID.
2. Tell the invitee(s) to accept any invite with that ID.
3. Send the invite.

As far as I know, this isn't a problem today. But an [upcoming test
change][1] causes this to happen much more reliably, so I think this is
worth fixing. (I think it's worth fixing even without that upcoming
change. Also, I tested this with those changes and it does, indeed, fix
hung tests.)

[0]: https://github.com/digidem/comapeo-core/blob/d2e5590aa749b690e5c07c3b64791db5e403ee29/test-e2e/sync.js#L530
[1]: #856
@@ -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.

🤷

@EvanHahn EvanHahn merged commit dfaceb2 into main Sep 26, 2024
6 checks passed
@EvanHahn EvanHahn deleted the fix-test-invite-race-conditions branch September 26, 2024 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants