forked from cucumber/cucumber-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step definitions and hooks can now specify a timeout (milliseconds) a…
…fter which a is thrown if the stepdef/hook has not completed. Closes cucumber#343
- Loading branch information
1 parent
deae442
commit b1d8b18
Showing
16 changed files
with
235 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cucumber.runtime; | ||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class Timeout { | ||
public static <T> T timeout(Callback<T> callback, int timeoutMillis) throws Throwable { | ||
if (timeoutMillis == 0) { | ||
return callback.call(); | ||
} else { | ||
final Thread executionThread = Thread.currentThread(); | ||
final AtomicBoolean done = new AtomicBoolean(); | ||
ScheduledFuture<?> timer = Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() { | ||
@Override | ||
public void run() { | ||
if (!done.get()) { | ||
executionThread.interrupt(); | ||
} | ||
} | ||
}, timeoutMillis, TimeUnit.MILLISECONDS); | ||
try { | ||
T result = callback.call(); | ||
timer.cancel(true); | ||
return result; | ||
} catch (InterruptedException timeout) { | ||
throw new TimeoutException("Timed out after " + timeoutMillis + "ms."); | ||
} | ||
} | ||
} | ||
|
||
public interface Callback<T> { | ||
T call() throws Throwable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cucumber.runtime; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.concurrent.TimeoutException; | ||
|
||
import static java.lang.Thread.sleep; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
public class TimeoutTest { | ||
@Test | ||
public void doesnt_time_out_if_it_takes_too_long() throws Throwable { | ||
final Slow slow = new Slow(); | ||
String what = Timeout.timeout(new Timeout.Callback<String>() { | ||
@Override | ||
public String call() throws Throwable { | ||
return slow.slow(); | ||
} | ||
}, 50); | ||
assertEquals("slow", what); | ||
} | ||
|
||
@Test(expected = TimeoutException.class) | ||
public void times_out_if_it_takes_too_long() throws Throwable { | ||
final Slow slow = new Slow(); | ||
Timeout.timeout(new Timeout.Callback<String>() { | ||
@Override | ||
public String call() throws Throwable { | ||
return slow.slower(); | ||
} | ||
}, 50); | ||
fail(); | ||
} | ||
|
||
@Test(expected = TimeoutException.class) | ||
public void times_out_infinite_loop_if_it_takes_too_long() throws Throwable { | ||
final Slow slow = new Slow(); | ||
Timeout.timeout(new Timeout.Callback<Void>() { | ||
@Override | ||
public Void call() throws Throwable { | ||
slow.infinite(); | ||
return null; | ||
} | ||
}, 10); | ||
fail(); | ||
} | ||
|
||
public static class Slow { | ||
public String slow() throws InterruptedException { | ||
sleep(10); | ||
return "slow"; | ||
} | ||
|
||
public String slower() throws InterruptedException { | ||
sleep(100); | ||
return "slower"; | ||
} | ||
|
||
public void infinite() throws InterruptedException { | ||
while (true) { | ||
sleep(1); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.