-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Use try-with-resources with MockLogAppender #1595
Conversation
Can one of the admins verify this patch? |
✅ Gradle Wrapper Validation success c753dca7220e943d21550ebe73e779baa8d114fc |
✅ Gradle Precommit success c753dca7220e943d21550ebe73e779baa8d114fc |
❌ Gradle Check failure c753dca7220e943d21550ebe73e779baa8d114fc |
start gradle check |
Test failed with a pretty generic timeout and cannot be reproduced:
|
✅ Gradle Check success c753dca7220e943d21550ebe73e779baa8d114fc |
c753dca
to
85b8ada
Compare
❌ Gradle Check failure 85b8adac39cdfac639db4e7fe21bd6b20398e23e |
start gradle check |
✅ Gradle Check success 85b8adac39cdfac639db4e7fe21bd6b20398e23e |
85b8ada
to
bae1339
Compare
❌ Gradle Check failure bae1339936c4137ab02aa4a1c8390154092938ed |
...er/src/internalClusterTest/java/org/opensearch/action/admin/indices/rollover/RolloverIT.java
Show resolved
Hide resolved
server/src/test/java/org/opensearch/discovery/HandshakingTransportAddressConnectorTests.java
Outdated
Show resolved
Hide resolved
I previously added a helper that started a MockLogAppender to ensure it was never added to a Logger before it was started. I subsequently found the opposite case in RolloverIT.java where the appender was stopped before it was closed, therefore creating a race where a concurrently running test in the same JVM could cause a logging failure. This seems like a really easy mistake to make when writing a test or introduce when refactoring a test. I've made a change to use try-with-resources to ensure that proper setup and teardown is done. This should make it much harder to introduce this particular test bug in the future. Unfortunately, it did involve touching a lot of files. The changes here are purely structural to leverage try-with-resources; no testing logic has been changed. Signed-off-by: Andrew Ross <andrross@amazon.com>
bae1339
to
61cc582
Compare
I previously added a helper that started a MockLogAppender to ensure it was never added to a Logger before it was started. I subsequently found the opposite case in RolloverIT.java where the appender was stopped before it was closed, therefore creating a race where a concurrently running test in the same JVM could cause a logging failure. This seems like a really easy mistake to make when writing a test or introduce when refactoring a test. I've made a change to use try-with-resources to ensure that proper setup and teardown is done. This should make it much harder to introduce this particular test bug in the future. Unfortunately, it did involve touching a lot of files. The changes here are purely structural to leverage try-with-resources; no testing logic has been changed. Signed-off-by: Andrew Ross <andrross@amazon.com>
I previously added a helper that started a MockLogAppender to ensure it was never added to a Logger before it was started. I subsequently found the opposite case in RolloverIT.java where the appender was stopped before it was closed, therefore creating a race where a concurrently running test in the same JVM could cause a logging failure. This seems like a really easy mistake to make when writing a test or introduce when refactoring a test. I've made a change to use try-with-resources to ensure that proper setup and teardown is done. This should make it much harder to introduce this particular test bug in the future. Unfortunately, it did involve touching a lot of files. The changes here are purely structural to leverage try-with-resources; no testing logic has been changed. Signed-off-by: Andrew Ross <andrross@amazon.com>
The context of this change is that some unit and integration tests make assertions about
the logging behavior of the code under test by adding a MockLogAppender to the
static (i.e. global) logging system to collect logging events. On top of this, the base
test class
OpenSearchTestCase
has an@After
method that asserts that the loggingsystem itself did not emit any errors indicating a problem with the logging setup. A
problem occurs if a MockLogAppender is added to a logger before it is started or is
stopped before it is removed. A non-started appender can cause the logger to emit and
fail therefore fail a test. The specific error I've observed is "Attempted to append to non-started
appender mock". When running a single test in isolation, there will not be a problem
regardless of the start/add/remove/stop order of the appender, because there is nothing
else going on inside the JVM to trigger a logging event. However, if tests are being run
concurrently inside a JVM, then if a non-started appender is added to a logger then there
exists a race condition that if a particular logging event were to fire then it could cause an
error before the mock appender was started. These errors are quite difficult to chase down
as the tests that interfere with each other are otherwise entirely unrelated. The change here
is to handle the lifecycle management of MockLogAppender via try-with-resources so it
is handled correctly be default and it is not so easy to introduce a potential race condition.
Concretely, this is what the code looked like before:
and this is what it looks like after the change in this PR:
The try-with-resources construct does the start/add/remove/stop logic in the correct order
and the developer doesn't need to think about it.
I previously added a helper that started a MockLogAppender to ensure it
was never added to a Logger before it was started. I subsequently found
the opposite case in RolloverIT.java where the appender was stopped
before it was closed, therefore creating a race where a concurrently
running test in the same JVM could cause a logging failure. This seems
like a really easy mistake to make when writing a test or introduce when
refactoring a test. I've made a change to use try-with-resources to
ensure that proper setup and teardown is done. This should make it much
harder to introduce this particular test bug in the future.
Unfortunately, it did involve touching a lot of files. The changes here
are purely structural to leverage try-with-resources; no testing logic
has been changed.
Description
Fixes a potential race conditions in tests that lead to intermittent failures
Issues Resolved
None
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.