Skip to content

Commit 8a0ac7b

Browse files
committed
Move BlockingTestRunnable to the public API space
This is used by both artifacts. It's simple API should be simple to have used outside of the project without having much expansion to the size.
1 parent 55c3ac2 commit 8a0ac7b

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.threadly.test.concurrent;
2+
3+
/**
4+
* An implementation of {@link TestRunnable} which will initially block the running thread with
5+
* {@link Object#wait()} when {@link #handleRunStart()} is invoked. The thread will remain blocked
6+
* until {@link #unblock()} is invoked.
7+
*
8+
* @since 1.0
9+
*/
10+
public class BlockingTestRunnable extends TestRunnable {
11+
private volatile boolean unblocked = false;
12+
13+
@Override
14+
public void handleRunStart() throws InterruptedException {
15+
synchronized (this) {
16+
while (! unblocked) {
17+
this.wait();
18+
}
19+
}
20+
}
21+
22+
/**
23+
* Check if the task has been unblocked yet.
24+
*
25+
* @return {@code true} if the thread has been unblocked.
26+
*/
27+
public boolean isUnblocked() {
28+
return unblocked;
29+
}
30+
31+
/**
32+
* Invoke to unblock any current or future executions for this {@link TestRunnable}. Once invoked
33+
* no future blocking will occur. In general this should be invoked at the end of every test
34+
* (fail or not) to avoid having left over blocked threads hanging around.
35+
*/
36+
public void unblock() {
37+
synchronized (this) {
38+
unblocked = true;
39+
40+
this.notifyAll();
41+
}
42+
}
43+
}

src/test/java/org/threadly/BlockingTestRunnable.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/test/java/org/threadly/test/concurrent/TestRunnableTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.junit.After;
66
import org.junit.Before;
77
import org.junit.Test;
8-
import org.threadly.BlockingTestRunnable;
98
import org.threadly.ThreadlyTester;
109
import org.threadly.test.concurrent.TestCondition.ConditionTimeoutException;
1110
import org.threadly.util.Clock;

0 commit comments

Comments
 (0)