Skip to content

Commit

Permalink
Fix occasionally ConcurrentModificationException in Netty tests
Browse files Browse the repository at this point in the history
Due to using stream() on a log appender while the appender is still running and possibly adding more logs.
  • Loading branch information
jduo committed Dec 11, 2023
1 parent e8840dd commit a7d2267
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.junit.Assert.assertTrue;

import java.util.Collections;
import java.util.stream.Collectors;

import org.apache.arrow.memory.ArrowBuf;
Expand All @@ -40,6 +41,7 @@ public class TestNettyAllocator {
@Test
public void testMemoryUsage() {
ListAppender<ILoggingEvent> memoryLogsAppender = new ListAppender<>();
memoryLogsAppender.list = Collections.synchronizedList(memoryLogsAppender.list);
Logger logger = (Logger) LoggerFactory.getLogger("arrow.allocator");
try {
logger.setLevel(Level.TRACE);
Expand All @@ -52,13 +54,17 @@ public void testMemoryUsage() {
boolean result = false;
long startTime = System.currentTimeMillis();
while ((System.currentTimeMillis() - startTime) < 10000) { // 10 seconds maximum for time to read logs
result = memoryLogsAppender.list.stream()
.anyMatch(
log -> log.toString().contains("Memory Usage: \n") &&
log.toString().contains("Large buffers outstanding: ") &&
log.toString().contains("Normal buffers outstanding: ") &&
log.getLevel().equals(Level.TRACE)
);
// Lock on the list backing the appender since a background thread might try to add more logs
// while stream() is iterating over list elements. This would throw a flakey ConcurrentModificationException.
synchronized (memoryLogsAppender.list) {
result = memoryLogsAppender.list.stream()
.anyMatch(
log -> log.toString().contains("Memory Usage: \n") &&
log.toString().contains("Large buffers outstanding: ") &&
log.toString().contains("Normal buffers outstanding: ") &&
log.getLevel().equals(Level.TRACE)
);
}
if (result) {
break;
}
Expand Down

0 comments on commit a7d2267

Please sign in to comment.