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

Ensure non-mocked message publishing during tests fails #5634

Merged
merged 3 commits into from
Apr 15, 2024

Conversation

AdamWill
Copy link
Contributor

@AdamWill AdamWill commented Apr 4, 2024

In working on #5630 I found that what happens when you don't properly mock out message publish attempts in tests isn't great. In CI, the test will hang for several minutes, then eventually proceed. Obviously we don't want to slow the tests down, and there is a 30 minute time limit on the test, so if you do this more than twice or so, the tests will time out. It's not very clear why the tests are timing out and failing - you have to guess that it's message publishing causing the problem.

In bcd, the test doesn't hang, it just silently proceeds (I guess the message gets published to the rabbitmq instance in the bcd environment, I haven't checked). So you will not notice that you got it wrong until you submit a PR.

Neither of those behaviors is great, so instead, let's mock out the 'real' message publishing function in fedora_messaging in a fixture that's automatically used by every test. We replace it with a mock that raises an exception with a hopefully- useful message. We have to mock _twisted_publish, not publish, because mock_sends also mocks _twisted_publish; if we mocked publish, we would break mock_sends. This way mock_sends gets to override us and work OK.

Unfortunately this usually causes a database rollback error so you have to scroll back through some confusing errors to find the 'real' problem, but it's still an improvement, I think.

@AdamWill AdamWill requested a review from a team as a code owner April 4, 2024 01:15
@AdamWill
Copy link
Contributor Author

AdamWill commented Apr 4, 2024

To try this out, you can just remove a usage of mock_sends in some test so the test will really try and publish the message, and run that test. You should see the exception is raised.

@AdamWill AdamWill force-pushed the tests-unsafe-publish-fail branch 2 times, most recently from 8ca3af1 to 99e7c1b Compare April 8, 2024 23:25
In working on fedora-infra#5630 I found that what happens when you don't
properly mock out message publish attempts in tests isn't great.
In CI, the test will hang for several minutes, then eventually
proceed. Obviously we don't want to slow the tests down, and
there is a 30 minute time limit on the test, so if you do this
more than twice or so, the tests will time out. It's not very
clear why the tests are timing out and failing - you have to
guess that it's message publishing causing the problem.

In bcd, the test doesn't hang, it just silently proceeds (I guess
the message gets published to the rabbitmq instance in the bcd
environment, I haven't checked). So you will not notice that you
got it wrong until you submit a PR.

Neither of those behaviors is great, so instead, let's mock out
the 'real' message publishing function in fedora_messaging in
a fixture that's automatically used by every test. We replace
it with a mock that raises an exception with a hopefully-
useful message. We have to mock _twisted_publish, not publish,
because mock_sends also mocks _twisted_publish; if we mocked
publish, we would break mock_sends. This way mock_sends gets
to override us and work OK.

Unfortunately this usually causes a database rollback error so
you have to scroll back through some confusing errors to find
the 'real' problem, but it's still an improvement, I think.

Fixes: fedora-infra#5633

Signed-off-by: Adam Williamson <awilliam@redhat.com>
@AdamWill AdamWill force-pushed the tests-unsafe-publish-fail branch from 99e7c1b to d066f40 Compare April 9, 2024 00:06
Copy link
Contributor

@mattiaverga mattiaverga left a comment

Choose a reason for hiding this comment

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

LGTM

@mergify mergify bot merged commit 3eb3204 into fedora-infra:develop Apr 15, 2024
38 checks passed
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