Start MockLogAppender before adding to static context (#1587)

I observed a test failure with the message
'Attempted to append to non-started appender mock' from an assertion in
`OpenSearchTestCase::after`. I believe this indicates that a
MockLogAppender (which is named "mock") was added as an appender to the
static logging context and some other test in the same JVM happened to
cause a logging statement to hit that appender and cause an error, which
then caused an unrelated test to fail (because they share static state
with the logger). Almost all usages of MockLogAppender start it
immediately after creation. I found a few that did not and fixed those.
I also made a static helper in MockLogAppender to start it upon
creation.

Signed-off-by: Andrew Ross <andrross@amazon.com>
This commit is contained in:
Andrew Ross 2021-11-19 12:37:46 -06:00 committed by GitHub
parent bcfb57c06a
commit d467283475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 9 deletions

View File

@ -53,11 +53,10 @@ public class OpenSearchLoggingHandlerIT extends OpenSearchNetty4IntegTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
appender = new MockLogAppender(); appender = MockLogAppender.createStarted();
Loggers.addAppender(LogManager.getLogger(OpenSearchLoggingHandler.class), appender); Loggers.addAppender(LogManager.getLogger(OpenSearchLoggingHandler.class), appender);
Loggers.addAppender(LogManager.getLogger(TransportLogger.class), appender); Loggers.addAppender(LogManager.getLogger(TransportLogger.class), appender);
Loggers.addAppender(LogManager.getLogger(TcpTransport.class), appender); Loggers.addAppender(LogManager.getLogger(TcpTransport.class), appender);
appender.start();
} }
public void tearDown() throws Exception { public void tearDown() throws Exception {

View File

@ -54,10 +54,9 @@ public class NioTransportLoggingIT extends NioIntegTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
appender = new MockLogAppender(); appender = MockLogAppender.createStarted();
Loggers.addAppender(LogManager.getLogger(TransportLogger.class), appender); Loggers.addAppender(LogManager.getLogger(TransportLogger.class), appender);
Loggers.addAppender(LogManager.getLogger(TcpTransport.class), appender); Loggers.addAppender(LogManager.getLogger(TcpTransport.class), appender);
appender.start();
} }
public void tearDown() throws Exception { public void tearDown() throws Exception {

View File

@ -143,10 +143,9 @@ public class SettingsFilterTests extends OpenSearchTestCase {
private void assertExpectedLogMessages(Consumer<Logger> consumer, MockLogAppender.LoggingExpectation... expectations) private void assertExpectedLogMessages(Consumer<Logger> consumer, MockLogAppender.LoggingExpectation... expectations)
throws IllegalAccessException { throws IllegalAccessException {
Logger testLogger = LogManager.getLogger("org.opensearch.test"); Logger testLogger = LogManager.getLogger("org.opensearch.test");
MockLogAppender appender = new MockLogAppender(); MockLogAppender appender = MockLogAppender.createStarted();
Loggers.addAppender(testLogger, appender); Loggers.addAppender(testLogger, appender);
try { try {
appender.start();
Arrays.stream(expectations).forEach(appender::addExpectation); Arrays.stream(expectations).forEach(appender::addExpectation);
consumer.accept(testLogger); consumer.accept(testLogger);
appender.assertAllExpectationsMatched(); appender.assertAllExpectationsMatched();

View File

@ -56,9 +56,8 @@ public class TransportLoggerTests extends OpenSearchTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
appender = new MockLogAppender(); appender = MockLogAppender.createStarted();
Loggers.addAppender(LogManager.getLogger(TransportLogger.class), appender); Loggers.addAppender(LogManager.getLogger(TransportLogger.class), appender);
appender.start();
} }
public void tearDown() throws Exception { public void tearDown() throws Exception {

View File

@ -51,7 +51,19 @@ public class MockLogAppender extends AbstractAppender {
private static final String COMMON_PREFIX = System.getProperty("opensearch.logger.prefix", "org.opensearch."); private static final String COMMON_PREFIX = System.getProperty("opensearch.logger.prefix", "org.opensearch.");
private List<LoggingExpectation> expectations; private final List<LoggingExpectation> expectations;
/**
* Creates and starts a MockLogAppender. Generally preferred over using the constructor
* directly because adding an unstarted appender to the static logging context can cause
* difficult-to-identify errors in the tests and this method makes it impossible to do
* that.
*/
public static MockLogAppender createStarted() throws IllegalAccessException {
final MockLogAppender appender = new MockLogAppender();
appender.start();
return appender;
}
public MockLogAppender() throws IllegalAccessException { public MockLogAppender() throws IllegalAccessException {
super("mock", RegexFilter.createFilter(".*(\n.*)*", new String[0], false, null, null), null); super("mock", RegexFilter.createFilter(".*(\n.*)*", new String[0], false, null, null), null);