Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

test: fix flaky sequencer unit tests#187

Merged
pradn merged 1 commit into
googleapis:masterfrom
plamut:iss-178
Sep 10, 2020
Merged

test: fix flaky sequencer unit tests#187
pradn merged 1 commit into
googleapis:masterfrom
plamut:iss-178

Conversation

@plamut

@plamut plamut commented Sep 10, 2020

Copy link
Copy Markdown
Contributor

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

@pradn pradn left a comment

Copy link
Copy Markdown
Contributor

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

plamut commented Sep 10, 2020

Copy link
Copy Markdown
Contributor Author

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

pradn commented Sep 10, 2020

Copy link
Copy Markdown
Contributor

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 subscribe to this conversation on GitHub. Already have an account? Sign in.

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