Skip to content

Commit fa59bfb

Browse files
authored
Stabilize AbstractByteBufTest.testBytesInArrayMultipleThreads (#16370)
Motivation: In busy CI environments these tests could time out (the timeout was 5 seconds) and cause build breakages. Modification: - Add a barrier for the start of the worker threads. - Make sure every thread do the same number of iterations; the total number of iterations remain the same, but now threads work on these without coordination. - Capture worker thread exceptions. - Increase the timeout to 30 seconds. Result: Tests should be more stable now.
1 parent eeaa0ee commit fa59bfb

1 file changed

Lines changed: 45 additions & 33 deletions

File tree

buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@
5151
import java.util.List;
5252
import java.util.Random;
5353
import java.util.Set;
54+
import java.util.concurrent.Callable;
5455
import java.util.concurrent.CountDownLatch;
5556
import java.util.concurrent.CyclicBarrier;
5657
import java.util.concurrent.ExecutionException;
5758
import java.util.concurrent.ExecutorService;
5859
import java.util.concurrent.Executors;
5960
import java.util.concurrent.Future;
61+
import java.util.concurrent.FutureTask;
6062
import java.util.concurrent.Semaphore;
6163
import java.util.concurrent.ThreadLocalRandom;
6264
import java.util.concurrent.TimeUnit;
@@ -76,7 +78,6 @@
7678
import static org.junit.jupiter.api.Assertions.assertEquals;
7779
import static org.junit.jupiter.api.Assertions.assertFalse;
7880
import static org.junit.jupiter.api.Assertions.assertNotSame;
79-
import static org.junit.jupiter.api.Assertions.assertNull;
8081
import static org.junit.jupiter.api.Assertions.assertSame;
8182
import static org.junit.jupiter.api.Assertions.assertThrows;
8283
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -2881,43 +2882,54 @@ public void testSliceBytesInArrayMultipleThreads() throws Exception {
28812882

28822883
static void testBytesInArrayMultipleThreads(
28832884
final ByteBuf buffer, final byte[] expectedBytes, final boolean slice) throws Exception {
2884-
final AtomicReference<Throwable> cause = new AtomicReference<Throwable>();
2885-
final CountDownLatch latch = new CountDownLatch(60000);
2886-
final CyclicBarrier barrier = new CyclicBarrier(11);
2887-
for (int i = 0; i < 10; i++) {
2888-
new Thread(new Runnable() {
2889-
@Override
2890-
public void run() {
2891-
while (cause.get() == null && latch.getCount() > 0) {
2892-
ByteBuf buf;
2893-
if (slice) {
2894-
buf = buffer.slice();
2895-
} else {
2896-
buf = buffer.duplicate();
2897-
}
2898-
2899-
byte[] array = new byte[8];
2900-
buf.readBytes(array);
2885+
final CyclicBarrier startBarrier = new CyclicBarrier(10);
2886+
final CyclicBarrier endBarrier = new CyclicBarrier(11);
2887+
Callable<Void> callable = new Callable<Void>() {
2888+
@Override
2889+
public Void call() throws Exception {
2890+
startBarrier.await();
2891+
for (int i = 0; i < 6000; i++) {
2892+
ByteBuf buf;
2893+
if (slice) {
2894+
buf = buffer.slice();
2895+
} else {
2896+
buf = buffer.duplicate();
2897+
}
29012898

2902-
assertArrayEquals(expectedBytes, array);
2899+
byte[] array = new byte[8];
2900+
buf.readBytes(array);
29032901

2904-
Arrays.fill(array, (byte) 0);
2905-
buf.getBytes(0, array);
2906-
assertArrayEquals(expectedBytes, array);
2902+
assertArrayEquals(expectedBytes, array);
29072903

2908-
latch.countDown();
2909-
}
2910-
try {
2911-
barrier.await();
2912-
} catch (Exception e) {
2913-
// ignore
2914-
}
2904+
Arrays.fill(array, (byte) 0);
2905+
buf.getBytes(0, array);
2906+
assertArrayEquals(expectedBytes, array);
29152907
}
2916-
}).start();
2908+
endBarrier.await();
2909+
return null;
2910+
}
2911+
};
2912+
List<FutureTask<Void>> tasks = new ArrayList<>();
2913+
for (int i = 0; i < 10; i++) {
2914+
FutureTask<Void> task = new FutureTask<>(callable);
2915+
new Thread(task).start();
2916+
tasks.add(task);
2917+
}
2918+
try {
2919+
endBarrier.await(30, TimeUnit.SECONDS);
2920+
} catch (Exception e) {
2921+
for (FutureTask<Void> task : tasks) {
2922+
try {
2923+
task.get(100, TimeUnit.MILLISECONDS);
2924+
} catch (Exception ex) {
2925+
e.addSuppressed(ex);
2926+
}
2927+
}
2928+
throw e;
2929+
}
2930+
for (FutureTask<Void> task : tasks) {
2931+
task.get(1, TimeUnit.SECONDS);
29172932
}
2918-
latch.await(10, TimeUnit.SECONDS);
2919-
barrier.await(5, TimeUnit.SECONDS);
2920-
assertNull(cause.get());
29212933
}
29222934

29232935
public static Stream<Arguments> setCharSequenceCombinations() {

0 commit comments

Comments
 (0)