Use try-with-resources with MockLogAppender#1595
Merged
Merged
Conversation
Collaborator
|
Can one of the admins verify this patch? |
Collaborator
|
✅ Gradle Wrapper Validation success c753dca7220e943d21550ebe73e779baa8d114fc |
Collaborator
|
✅ Gradle Precommit success c753dca7220e943d21550ebe73e779baa8d114fc |
Collaborator
|
❌ Gradle Check failure c753dca7220e943d21550ebe73e779baa8d114fc |
Member
Author
|
start gradle check |
Member
Author
|
Test failed with a pretty generic timeout and cannot be reproduced: |
Collaborator
|
✅ Gradle Check success c753dca7220e943d21550ebe73e779baa8d114fc |
c753dca to
85b8ada
Compare
Collaborator
|
❌ Gradle Check failure 85b8adac39cdfac639db4e7fe21bd6b20398e23e |
Member
Author
|
start gradle check |
Collaborator
|
✅ Gradle Check success 85b8adac39cdfac639db4e7fe21bd6b20398e23e |
85b8ada to
bae1339
Compare
Collaborator
|
❌ Gradle Check failure bae1339936c4137ab02aa4a1c8390154092938ed |
mch2
requested changes
Dec 21, 2021
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
mch2
approved these changes
Dec 21, 2021
andrross
added a commit
to andrross/OpenSearch
that referenced
this pull request
Dec 21, 2021
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>
adnapibar
pushed a commit
that referenced
this pull request
Dec 22, 2021
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
OpenSearchTestCasehas an@Aftermethod 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.