dongjinleekr commented on a change in pull request #7898: URL: https://github.com/apache/kafka/pull/7898#discussion_r500882458
########## File path: core/src/test/scala/unit/kafka/utils/LogCaptureContext.scala ########## @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package unit.kafka.utils + +import java.util.concurrent.{CountDownLatch, TimeUnit} + +import org.apache.logging.log4j.Level +import org.apache.logging.log4j.core.{LogEvent, LoggerContext} +import org.apache.logging.log4j.test.appender.ListAppender + +import scala.jdk.CollectionConverters._ + +class LogCaptureContext(listAppender: ListAppender, prevLevelMap: Map[String, Level]) extends AutoCloseable { + + def setLatch(size: Int): Unit = { + this.listAppender.countDownLatch = new CountDownLatch(size) + } + + @throws[InterruptedException] + def await(l: Long, timeUnit: TimeUnit): Unit = { + this.listAppender.countDownLatch.await(l, timeUnit) + } + + def getMessages: Seq[LogEvent] = listAppender.getEvents.asScala.toSeq Review comment: Good proposal. Actually, it is the first approach I have taken. However, while running the tests repeatedly, I found that the log messages are not forwarded in designated timeout properly, and the tests go so flaky. Each test runs correctly when I run them individually, but 3 ~ 5 tests were randomly failed when I run them in bulk, with `./gradlew :streams:test`. After numerous trial and error, I found that the current approach is a little bit verbose but makes the test suites sustainable. It is the background of this API design. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org