GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Stabilize AbstractByteBufTest.testToStringMultipleThreads (#16380) · netty/netty@36395eb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src/test/java/io/netty/buffer
    • AbstractByteBufTest.java
    • PooledByteBufAllocatorTest.java
  • common/src/main/java/io/netty/util/internal
    • ThrowableUtil.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java
+24-10Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.netty.util.CharsetUtil;
2121
import io.netty.util.IllegalReferenceCountException;
2222
import io.netty.util.internal.PlatformDependent;
23+
import io.netty.util.internal.ThrowableUtil;
2324
import org.junit.jupiter.api.AfterEach;
2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
@@ -2293,7 +2294,7 @@ public void testToString() {
22932294
}
22942295

22952296
@Test
2296-
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
2297+
@Timeout(30)
22972298
public void testToStringMultipleThreads() throws Throwable {
22982299
buffer.clear();
22992300
buffer.writeBytes("Hello, World!".getBytes(CharsetUtil.ISO_8859_1));
@@ -2303,19 +2304,23 @@ public void testToStringMultipleThreads() throws Throwable {
23032304
static void testToStringMultipleThreads0(final ByteBuf buffer) throws Throwable {
23042305
final String expected = buffer.toString(CharsetUtil.ISO_8859_1);
23052306

2306-
final AtomicInteger counter = new AtomicInteger(30000);
2307+
final CyclicBarrier startBarrier = new CyclicBarrier(10);
23072308
final AtomicReference<Throwable> errorRef = new AtomicReference<Throwable>();
23082309
List<Thread> threads = new ArrayList<Thread>();
23092310
for (int i = 0; i < 10; i++) {
23102311
Thread thread = new Thread(new Runnable() {
23112312
@Override
23122313
public void run() {
23132314
try {
2314-
while (errorRef.get() == null && counter.decrementAndGet() > 0) {
2315+
startBarrier.await(10, TimeUnit.SECONDS);
2316+
int counter = 3000;
2317+
while (errorRef.get() == null && counter-- > 0) {
23152318
assertEquals(expected, buffer.toString(CharsetUtil.ISO_8859_1));
23162319
}
23172320
} catch (Throwable cause) {
2318-
errorRef.compareAndSet(null, cause);
2321+
if (!errorRef.compareAndSet(null, cause)) {
2322+
ThrowableUtil.addSuppressed(errorRef.get(), cause);
2323+
}
23192324
}
23202325
}
23212326
});
@@ -2325,13 +2330,22 @@ public void run() {
23252330
thread.start();
23262331
}
23272332

2328-
for (Thread thread : threads) {
2329-
thread.join();
2330-
}
2333+
try {
2334+
for (Thread thread : threads) {
2335+
thread.join();
2336+
}
23312337

2332-
Throwable error = errorRef.get();
2333-
if (error != null) {
2334-
throw error;
2338+
Throwable error = errorRef.get();
2339+
if (error != null) {
2340+
throw error;
2341+
}
2342+
} catch (Throwable e) {
2343+
for (Thread thread : threads) {
2344+
if (thread.isAlive()) {
2345+
ThrowableUtil.interruptAndAttachAsyncStackTrace(thread, e);
2346+
}
2347+
}
2348+
throw e;
23352349
}
23362350
}
23372351

Collapse file

‎buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java
+3-11Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.netty.util.concurrent.FastThreadLocalThread;
2121
import io.netty.util.internal.PlatformDependent;
2222
import io.netty.util.internal.SystemPropertyUtil;
23+
import io.netty.util.internal.ThrowableUtil;
2324
import org.junit.jupiter.api.Test;
2425
import org.junit.jupiter.api.Timeout;
2526

@@ -534,7 +535,7 @@ public void run() {
534535
try {
535536
cacheLatch.await();
536537
} catch (InterruptedException e) {
537-
attachAsyncStackTrace(e, t);
538+
ThrowableUtil.interruptAndAttachAsyncStackTrace(t, e);
538539
throw e;
539540
}
540541

@@ -546,22 +547,13 @@ public void destroy() throws Exception {
546547
task.get();
547548
t.join();
548549
} catch (InterruptedException e) {
549-
attachAsyncStackTrace(e, t);
550+
ThrowableUtil.interruptAndAttachAsyncStackTrace(t, e);
550551
throw e;
551552
}
552553
}
553554
};
554555
}
555556

556-
private static void attachAsyncStackTrace(Exception e, Thread thread) {
557-
StackTraceElement[] stackTrace = thread.getStackTrace();
558-
InterruptedException asyncIE = new InterruptedException(
559-
"Asynchronous interruption: " + thread);
560-
thread.interrupt();
561-
asyncIE.setStackTrace(stackTrace);
562-
e.addSuppressed(asyncIE);
563-
}
564-
565557
private interface ThreadCache {
566558
void destroy() throws Exception;
567559
}
Collapse file

‎common/src/main/java/io/netty/util/internal/ThrowableUtil.java‎

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/ThrowableUtil.java
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,19 @@ public static void addSuppressed(Throwable target, List<Throwable> suppressed) {
8080
public static Throwable[] getSuppressed(Throwable source) {
8181
return source.getSuppressed();
8282
}
83+
84+
/**
85+
* Capture the stack trace of the given thread, interrupt it, and attach the stack trace as a suppressed exception
86+
* to the given cause.
87+
* @param thread The thread to interrupt.
88+
* @param cause The cause to attach a stack trace to.
89+
*/
90+
public static void interruptAndAttachAsyncStackTrace(Thread thread, Throwable cause) {
91+
StackTraceElement[] stackTrace = thread.getStackTrace();
92+
InterruptedException asyncIE = new InterruptedException(
93+
"Asynchronous interruption: " + thread);
94+
thread.interrupt();
95+
asyncIE.setStackTrace(stackTrace);
96+
addSuppressed(cause, asyncIE);
97+
}
8398
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.