Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/io/threadforge/ThreadScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -1217,12 +1217,14 @@ public void run() {
Thread runner = Thread.currentThread();
if (!task.beginExecution(runner)) {
task.markExecutionFinished(runner);
Thread.interrupted();
return;
}
try {
super.run();
} finally {
task.markExecutionFinished(runner);
Thread.interrupted();
}
}
}
Expand Down
118 changes: 118 additions & 0 deletions src/test/java/io/threadforge/ChannelInterruptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -86,6 +90,47 @@ public Integer call() {
}
}

@Test
void cancelledChannelWaitDoesNotLeakInterruptToNextTask() throws Exception {
InterruptRetainingExecutor executor = new InterruptRetainingExecutor();
Channel<Integer> cancelledChannel = Channel.bounded(1);
CountDownLatch cancelledStarted = new CountDownLatch(1);
Channel<Integer> deadlineChannel = Channel.bounded(1);
CountDownLatch deadlineStarted = new CountDownLatch(1);
try {
Scheduler scheduler = Scheduler.from(executor);
try (ThreadScope first = ThreadScope.open().withScheduler(scheduler)) {
Task<Integer> cancelled = first.submit(new Callable<Integer>() {
@Override
public Integer call() {
cancelledStarted.countDown();
return cancelledChannel.receive();
}
});
assertTrue(cancelledStarted.await(1L, TimeUnit.SECONDS));
assertTrue(cancelled.cancel());
}
cancelledChannel.close();

try (ThreadScope second = ThreadScope.open()
.withScheduler(scheduler)
.withDeadline(Duration.ofMillis(50))) {
Task<Integer> deadlineTask = second.submit(new Callable<Integer>() {
@Override
public Integer call() {
deadlineStarted.countDown();
return deadlineChannel.receive();
}
});
assertTrue(deadlineStarted.await(1L, TimeUnit.SECONDS));
assertThrows(ScopeTimeoutException.class, () -> second.await(deadlineTask));
}
} finally {
deadlineChannel.close();
executor.shutdownNow();
}
}

@Test
void closedChannelDrainsBufferBeforeIteratorEnds() {
Channel<Integer> channel = Channel.bounded(3);
Expand All @@ -101,4 +146,77 @@ void closedChannelDrainsBufferBeforeIteratorEnds() {
assertEquals(Arrays.asList(2), remaining);
assertThrows(ChannelClosedException.class, channel::receive);
}

private static final class InterruptRetainingExecutor extends AbstractExecutorService {
private final LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
private final AtomicBoolean shutdown = new AtomicBoolean();
private final AtomicBoolean terminated = new AtomicBoolean();
private final AtomicReference<Thread> worker = new AtomicReference<Thread>();

private InterruptRetainingExecutor() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
worker.set(Thread.currentThread());
try {
while (!shutdown.get()) {
Runnable command = queue.poll();
if (command != null) {
command.run();
} else {
Thread.yield();
}
}
} finally {
terminated.set(true);
}
}
}, "threadforge-test-interrupt-retaining");
thread.setDaemon(true);
thread.start();
}

@Override
public void execute(Runnable command) {
if (shutdown.get()) {
throw new RejectedExecutionException("executor is shut down");
}
queue.offer(command);
}

@Override
public void shutdown() {
shutdown.set(true);
}

@Override
public java.util.List<Runnable> shutdownNow() {
shutdown.set(true);
Thread thread = worker.get();
if (thread != null) {
thread.interrupt();
}
return new java.util.ArrayList<Runnable>(queue);
}

@Override
public boolean isShutdown() {
return shutdown.get();
}

@Override
public boolean isTerminated() {
return terminated.get();
}

@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
long deadline = System.nanoTime() + unit.toNanos(timeout);
while (!terminated.get() && System.nanoTime() < deadline) {
Thread.yield();
}
return terminated.get();
}

}
}
Loading