Skip to content

Commit

Permalink
[tests] Add a function for testing a code with a time out.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Mar 8, 2020
1 parent bde249d commit 1c0439c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 19 deletions.
Expand Up @@ -992,7 +992,7 @@ public static void assertException(Class<? extends Throwable> expected, Code cod
fail("Expecting exception of type " + expected.getName());
} catch (Throwable ex) {
if (!expected.isAssignableFrom(ex.getClass())) {
fail("Expecting exception of type " + expected.getName());
fail("Expecting exception of type " + expected.getName() + ", but got " + ex.getClass().getName(), ex);
}
}
}
Expand Down
Expand Up @@ -45,20 +45,69 @@ private TestTimeout() {
* @since 0.10
*/
public static TimeOutHandler startTimeOut(boolean enable) {
final TimeOutHandler handler = new TimeOutHandler();
return startTimeOut(enable, null);
}

/** Start a time out on the operation.
*
* @return the time out manager.
* @since 0.9
*/
public static TimeOutHandler startTimeOut() {
return startTimeOut(true, null);
}

/** Start a time out on the operation.
*
* @param enable programmatic flag for enabling the time out.
* @param predicate the condition for stopping the timeout loop.
* @return the time out manager.
* @since 0.11
*/
public static TimeOutHandler startTimeOut(boolean enable, Predicate predicate) {
final TimeOutHandler handler = newTimeOut(predicate);
if (enable) {
handler.start();
handler.startAsync();
}
return handler;
}

/** Start a time out on the operation.
*
* @return the time out manager.
* @since 0.9
* @param predicate the condition for stopping the timeout loop.
* @since 0.11
*/
public static TimeOutHandler startTimeOut() {
return startTimeOut(true);
public static TimeOutHandler startTimeOut(Predicate predicate) {
return startTimeOut(true, predicate);
}

/** Create a timeout object.
*
* @param predicate the condition for stopping the timeout loop.
* @return the time out object.
* @since 0.11
*/
public static TimeOutHandler newTimeOut(Predicate predicate) {
return new TimeOutHandler(predicate);
}

/** An object for managing the time out of operations.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.11
*/
@FunctionalInterface
public interface Predicate {

/** Test something in order to stop the timeout loop.
*
* @return {@code true} to stop the timeout loop.
*/
boolean test();
}

/** An object for managing the time out of operations.
Expand All @@ -69,7 +118,7 @@ public static TimeOutHandler startTimeOut() {
* @mavenartifactid $ArtifactId$
* @since 0.9
*/
public static class TimeOutHandler {
public static class TimeOutHandler implements Runnable {

private static final int TIME_OUT = 10000;

Expand All @@ -81,35 +130,57 @@ public static class TimeOutHandler {

private final AtomicBoolean timeout = new AtomicBoolean();

private final Predicate predicate;

/** Constructor.
*
* @param predicate the condition for stopping the timeout loop.
* @since 0.11
*/
TimeOutHandler() {
//
TimeOutHandler(Predicate predicate) {
this.predicate = predicate;
}

/** Start the time out process.
*/
void start() {
public void startAsync() {
this.threadToBreak = Thread.currentThread();
this.thread = new Thread() {
@Override
public void run() {
final long endTime = System.currentTimeMillis() + TIME_OUT;
while (TimeOutHandler.this.continueLoop.get()
&& System.currentTimeMillis() <= endTime) {
Thread.yield();
}
if (TimeOutHandler.this.continueLoop.get()) {
TimeOutHandler.this.timeout.set(true);
TimeOutHandler.this.stop();
}
TimeOutHandler.this.run();
}
};
this.thread.setDaemon(true);
this.thread.setName("Test TimeOut Manager");
this.thread.start();
}

@Override
public void run() {
final long endTime = System.currentTimeMillis() + TIME_OUT;
while (TimeOutHandler.this.continueLoop.get()
&& System.currentTimeMillis() <= endTime) {
Thread.yield();
if (TimeOutHandler.this.predicate != null) {
TimeOutHandler.this.continueLoop.set(!TimeOutHandler.this.predicate.test());
}
}
if (TimeOutHandler.this.continueLoop.get()) {
TimeOutHandler.this.timeout.set(true);
TimeOutHandler.this.stop();
}
}

/** Replies if the process has stopped on a time out.
*
* @return {@code true} if time out.
* @since 0.11
*/
public boolean isTimeout() {
return this.timeout.get();
}

/** Stop the time out process.
*/
@SuppressWarnings("deprecation")
Expand Down

0 comments on commit 1c0439c

Please sign in to comment.