-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not stop MockLogAppender instances
When JUnit tests run concurrently they are mostly isolated but they do share static state. Tests generally don't rely on static state, with the notable exception of when they manipulate the logger to test the logging behavior of the code under test. This leads to problems if say test 1 stops a test log appender, then test 2 causes a logging message which attempts to append to that logging appender. The logging system will generate an error which leads to spurious failures in test 2. I previously attempted to avoid these problems by ensuring that test log appenders are removed before they are stopped. However, that doesn't completely eliminate race conditions as the list of appenders maintained by the logging system is essentially a copy-on-write array list. That means test 2 can get a reference of the appender list, then test 1 can remove and stop an appended, but test 2 still has a reference to a version of the list that contained the now stopped appender. I have now come to the conclusion that the easiest way to fix this problem (without mucking with the implementation details or concurrency properties of the logging system itself) is to not stop the test appenders. They maintain no external state so they can be safely removed without being stopped and they will just be garbage collected and everyone will be happy. Signed-off-by: Andrew Ross <andrross@amazon.com>
- Loading branch information
Showing
4 changed files
with
51 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
43 changes: 43 additions & 0 deletions
43
test/framework/src/main/java/org/opensearch/test/AbstractTestAppender.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.test; | ||
|
||
import org.apache.logging.log4j.core.Filter; | ||
import org.apache.logging.log4j.core.Layout; | ||
import org.apache.logging.log4j.core.appender.AbstractAppender; | ||
import org.apache.logging.log4j.core.config.Property; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Extension of {@link AbstractAppender} that provides a no-op stop() | ||
* implementation to avoid interference issues between tests running | ||
* concurrently. | ||
*/ | ||
public abstract class AbstractTestAppender extends AbstractAppender { | ||
|
||
protected AbstractTestAppender( | ||
String name, | ||
Filter filter, | ||
Layout<? extends Serializable> layout, | ||
boolean ignoreExceptions, | ||
Property[] properties | ||
) { | ||
super(name, filter, layout, ignoreExceptions, properties); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
// Do nothing. This feels wrong but there is nothing for this logger | ||
// to clean up or flush, and calling the parent stop() will change its | ||
// lifecycle state which can lead to race conditions in the static | ||
// logger state where in flight logger messages try to append to a | ||
// stopped appender and cause spurious test failures. | ||
} | ||
} |
This file contains 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