Make MockLogAppender itself Releasable (#108526)

Existing uses of MockLogAppender first construct an appender, then call
capturing on the instance in a try-with-resources block. This commit
adds a new method, capture, which creates an appender and sets up the
capture the the same time. The intent is that this will replace the
existing capturing calls, but there are too many to change in one PR.
This commit is contained in:
Ryan Ernst 2024-05-13 07:50:58 -07:00 committed by GitHub
parent 05d728e3ef
commit b02d06c2d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 88 additions and 68 deletions

View file

@ -206,16 +206,16 @@ public class SpawnerNoBootstrapTests extends LuceneTestCase {
String stdoutLoggerName = "test_plugin-controller-stdout";
String stderrLoggerName = "test_plugin-controller-stderr";
MockLogAppender appender = new MockLogAppender();
Loggers.setLevel(LogManager.getLogger(stdoutLoggerName), Level.TRACE);
Loggers.setLevel(LogManager.getLogger(stderrLoggerName), Level.TRACE);
CountDownLatch messagesLoggedLatch = new CountDownLatch(2);
if (expectSpawn) {
appender.addExpectation(new ExpectedStreamMessage(stdoutLoggerName, "I am alive", messagesLoggedLatch));
appender.addExpectation(new ExpectedStreamMessage(stderrLoggerName, "I am an error", messagesLoggedLatch));
}
try (var ignore = appender.capturing(stdoutLoggerName, stderrLoggerName)) {
try (var appender = MockLogAppender.capture(stdoutLoggerName, stderrLoggerName)) {
if (expectSpawn) {
appender.addExpectation(new ExpectedStreamMessage(stdoutLoggerName, "I am alive", messagesLoggedLatch));
appender.addExpectation(new ExpectedStreamMessage(stderrLoggerName, "I am an error", messagesLoggedLatch));
}
Spawner spawner = new Spawner();
spawner.spawnNativeControllers(environment);