|
51 | 51 | import java.util.List; |
52 | 52 | import java.util.Random; |
53 | 53 | import java.util.Set; |
| 54 | +import java.util.concurrent.Callable; |
54 | 55 | import java.util.concurrent.CountDownLatch; |
55 | 56 | import java.util.concurrent.CyclicBarrier; |
56 | 57 | import java.util.concurrent.ExecutionException; |
57 | 58 | import java.util.concurrent.ExecutorService; |
58 | 59 | import java.util.concurrent.Executors; |
59 | 60 | import java.util.concurrent.Future; |
| 61 | +import java.util.concurrent.FutureTask; |
60 | 62 | import java.util.concurrent.Semaphore; |
61 | 63 | import java.util.concurrent.ThreadLocalRandom; |
62 | 64 | import java.util.concurrent.TimeUnit; |
|
76 | 78 | import static org.junit.jupiter.api.Assertions.assertEquals; |
77 | 79 | import static org.junit.jupiter.api.Assertions.assertFalse; |
78 | 80 | import static org.junit.jupiter.api.Assertions.assertNotSame; |
79 | | -import static org.junit.jupiter.api.Assertions.assertNull; |
80 | 81 | import static org.junit.jupiter.api.Assertions.assertSame; |
81 | 82 | import static org.junit.jupiter.api.Assertions.assertThrows; |
82 | 83 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -2881,43 +2882,54 @@ public void testSliceBytesInArrayMultipleThreads() throws Exception { |
2881 | 2882 |
|
2882 | 2883 | static void testBytesInArrayMultipleThreads( |
2883 | 2884 | 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 | + } |
2901 | 2898 |
|
2902 | | - assertArrayEquals(expectedBytes, array); |
| 2899 | + byte[] array = new byte[8]; |
| 2900 | + buf.readBytes(array); |
2903 | 2901 |
|
2904 | | - Arrays.fill(array, (byte) 0); |
2905 | | - buf.getBytes(0, array); |
2906 | | - assertArrayEquals(expectedBytes, array); |
| 2902 | + assertArrayEquals(expectedBytes, array); |
2907 | 2903 |
|
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); |
2915 | 2907 | } |
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); |
2917 | 2932 | } |
2918 | | - latch.await(10, TimeUnit.SECONDS); |
2919 | | - barrier.await(5, TimeUnit.SECONDS); |
2920 | | - assertNull(cause.get()); |
2921 | 2933 | } |
2922 | 2934 |
|
2923 | 2935 | public static Stream<Arguments> setCharSequenceCombinations() { |
|
0 commit comments