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 flaky sequencer unit tests #187

Merged
merged 1 commit into from
Sep 10, 2020
Merged

Conversation

plamut
Copy link
Contributor

@plamut plamut commented Sep 10, 2020

Fixes #178.

Patching the client under test should be done on an instance used in a test, not on the instance's class - patching the latter can cause all other instances of the same class to share the patched method, possibly interfering with the patched method's call count.

This can probably be shipped along the new major version, as it does not interfere with the changes made there.

PR checklist

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Patching the client under test should be done on an instance used in
a test, not on the instance's class - patching the latter can cause
all other instances of the same class to share the patched method,
possibly interfering with the patched method's call count.
@plamut plamut added the testing label Sep 10, 2020
@plamut plamut requested a review from pradn September 10, 2020 14:37
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Sep 10, 2020
Copy link
Contributor

@pradn pradn left a comment

Choose a reason for hiding this comment

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

Ah! This makes sense. I assumed the patch would be reset between tests, but I guess there's no reason why that should be. Thanks for the fix!

@pradn pradn merged commit bc85451 into googleapis:master Sep 10, 2020
@plamut
Copy link
Contributor Author

plamut commented Sep 10, 2020

I assumed the patch would be reset between tests, but I guess there's no reason why that should be.

Actually, the patch is reset between the tests (that's its purpose after all), but the problem is that patching a method on a class affects all of its instances.

The issue here was that some of the other tests create their own Thread-PubSubBatchCommitter threads that invoke the _commit_sequencers() method, but if that method is patched on the class, that mocked method is shared between tests. That occasionally messed up the call count when some of those Thread-PubSubBatchCommitter threads were still running.

@plamut plamut deleted the iss-178 branch September 10, 2020 15:40
@pradn
Copy link
Contributor

pradn commented Sep 10, 2020

I see: patching a class is modifying "global state", which leads to conflicts when multiple threads are using that class-level patched method. Thank you for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement. testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flaky tests
2 participants