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@da03917

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
+23-10Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,7 +2290,7 @@ public void testToString() {
22902290
}
22912291

22922292
@Test
2293-
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
2293+
@Timeout(30)
22942294
public void testToStringMultipleThreads() throws Throwable {
22952295
buffer.clear();
22962296
buffer.writeBytes("Hello, World!".getBytes(CharsetUtil.ISO_8859_1));
@@ -2300,19 +2300,23 @@ public void testToStringMultipleThreads() throws Throwable {
23002300
static void testToStringMultipleThreads0(final ByteBuf buffer) throws Throwable {
23012301
final String expected = buffer.toString(CharsetUtil.ISO_8859_1);
23022302

2303-
final AtomicInteger counter = new AtomicInteger(30000);
2303+
final CyclicBarrier startBarrier = new CyclicBarrier(10);
23042304
final AtomicReference<Throwable> errorRef = new AtomicReference<Throwable>();
23052305
List<Thread> threads = new ArrayList<Thread>();
23062306
for (int i = 0; i < 10; i++) {
23072307
Thread thread = new Thread(new Runnable() {
23082308
@Override
23092309
public void run() {
23102310
try {
2311-
while (errorRef.get() == null && counter.decrementAndGet() > 0) {
2311+
startBarrier.await(10, TimeUnit.SECONDS);
2312+
int counter = 3000;
2313+
while (errorRef.get() == null && counter-- > 0) {
23122314
assertEquals(expected, buffer.toString(CharsetUtil.ISO_8859_1));
23132315
}
23142316
} catch (Throwable cause) {
2315-
errorRef.compareAndSet(null, cause);
2317+
if (!errorRef.compareAndSet(null, cause)) {
2318+
ThrowableUtil.addSuppressed(errorRef.get(), cause);
2319+
}
23162320
}
23172321
}
23182322
});
@@ -2322,13 +2326,22 @@ public void run() {
23222326
thread.start();
23232327
}
23242328

2325-
for (Thread thread : threads) {
2326-
thread.join();
2327-
}
2329+
try {
2330+
for (Thread thread : threads) {
2331+
thread.join();
2332+
}
23282333

2329-
Throwable error = errorRef.get();
2330-
if (error != null) {
2331-
throw error;
2334+
Throwable error = errorRef.get();
2335+
if (error != null) {
2336+
throw error;
2337+
}
2338+
} catch (Throwable e) {
2339+
for (Thread thread : threads) {
2340+
if (thread.isAlive()) {
2341+
ThrowableUtil.interruptAndAttachAsyncStackTrace(thread, e);
2342+
}
2343+
}
2344+
throw e;
23322345
}
23332346
}
23342347

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
@@ -84,4 +84,19 @@ public static Throwable[] getSuppressed(Throwable source) {
8484
}
8585
return source.getSuppressed();
8686
}
87+
88+
/**
89+
* Capture the stack trace of the given thread, interrupt it, and attach the stack trace as a suppressed exception
90+
* to the given cause.
91+
* @param thread The thread to interrupt.
92+
* @param cause The cause to attach a stack trace to.
93+
*/
94+
public static void interruptAndAttachAsyncStackTrace(Thread thread, Throwable cause) {
95+
StackTraceElement[] stackTrace = thread.getStackTrace();
96+
InterruptedException asyncIE = new InterruptedException(
97+
"Asynchronous interruption: " + thread);
98+
thread.interrupt();
99+
asyncIE.setStackTrace(stackTrace);
100+
addSuppressed(cause, asyncIE);
101+
}
87102
}

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.