Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example timeout #45

Merged
merged 1 commit into from
Mar 11, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/j8spec/Example.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package j8spec;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
Expand All @@ -21,6 +22,8 @@ static final class Builder {
private List<Hook> afterAllHooks = emptyList();
private UnsafeBlock block;
private Class<? extends Throwable> expectedException;
private long timeout;
private TimeUnit timeoutUnit;
private Rank rank;

Builder containerDescriptions(List<String> containerDescriptions) {
Expand Down Expand Up @@ -73,6 +76,12 @@ Builder ignored() {
return this;
}

Builder timeout(long timeout, TimeUnit unit) {
this.timeout = timeout;
this.timeoutUnit = unit;
return this;
}

Example build() {
return new Example(
containerDescriptions,
Expand All @@ -83,6 +92,8 @@ Example build() {
afterAllHooks,
block,
expectedException,
timeout,
timeoutUnit,
rank
);
}
Expand All @@ -102,6 +113,8 @@ private static void tryToExecute(List<Hook> hooks) throws Throwable {
private final List<Hook> afterAllHooks;
private final UnsafeBlock block;
private final Class<? extends Throwable> expectedException;
private final long timeout;
private final TimeUnit timeoutUnit;
private final Rank rank;

private Example(
Expand All @@ -113,6 +126,8 @@ private Example(
List<Hook> afterAllHooks,
UnsafeBlock block,
Class<? extends Throwable> expectedException,
long timeout,
TimeUnit timeoutUnit,
Rank rank
) {
this.containerDescriptions = unmodifiableList(containerDescriptions);
Expand All @@ -123,6 +138,8 @@ private Example(
this.afterAllHooks = afterAllHooks;
this.block = block;
this.expectedException = expectedException;
this.timeout = timeout;
this.timeoutUnit = timeoutUnit;
this.rank = rank;
}

Expand Down Expand Up @@ -187,4 +204,16 @@ public Class<? extends Throwable> expected() {
public boolean isExpectedToThrowAnException() {
return expectedException != null;
}

public boolean shouldFailOnTimeout() {
return timeout != 0;
}

public long timeout() {
return timeout;
}

public TimeUnit timeoutUnit() {
return timeoutUnit;
}
}
3 changes: 2 additions & 1 deletion src/main/java/j8spec/ExampleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ BlockDefinitionVisitor example(ExampleConfiguration config, UnsafeBlock block) {
.afterEachHooks(asHooks(afterEachBlocks))
.afterAllHooks(asHooks(afterAllBlocks))
.block(block)
.expectedException(config.expectedException());
.expectedException(config.expectedException())
.timeout(config.timeout(), config.timeoutUnit());
}

examples.add(builder.build());
Expand Down
38 changes: 36 additions & 2 deletions src/main/java/j8spec/ExampleConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package j8spec;

import java.util.concurrent.TimeUnit;

import static j8spec.BlockExecutionFlag.DEFAULT;

/**
Expand All @@ -13,6 +15,8 @@ public static final class Builder {
private String description;
private BlockExecutionFlag executionFlag = DEFAULT;
private Class<? extends Throwable> expectedException;
private int timeout;
private TimeUnit timeoutUnit;

Builder description(String description) {
this.description = description;
Expand All @@ -31,6 +35,20 @@ public Builder expected(Class<? extends Throwable> expectedException) {
return this;
}

/**
* Specifies the time to wait before timing out the example.
*
* @param timeout the maximum time to wait
* @param unit the time unit of the {@code timeout} argument
* @return this
* @since 3.0.0
*/
public Builder timeout(int timeout, TimeUnit unit) {
this.timeout = timeout;
this.timeoutUnit = unit;
return this;
}

Builder executionFlag(BlockExecutionFlag executionFlag) {
this.executionFlag = executionFlag;
return this;
Expand All @@ -40,23 +58,31 @@ ExampleConfiguration build() {
return new ExampleConfiguration(
description,
executionFlag,
expectedException
expectedException,
timeout,
timeoutUnit
);
}
}

private final String description;
private final BlockExecutionFlag executionFlag;
private final Class<? extends Throwable> expectedException;
private final long timeout;
private final TimeUnit timeoutUnit;

private ExampleConfiguration(
String description,
BlockExecutionFlag executionFlag,
Class<? extends Throwable> expectedException
Class<? extends Throwable> expectedException,
long timeout,
TimeUnit timeoutUnit
) {
this.description = description;
this.executionFlag = executionFlag;
this.expectedException = expectedException;
this.timeout = timeout;
this.timeoutUnit = timeoutUnit;
}

String description() {
Expand All @@ -70,4 +96,12 @@ Class<? extends Throwable> expectedException() {
BlockExecutionFlag executionFlag() {
return executionFlag;
}

long timeout() {
return timeout;
}

TimeUnit timeoutUnit() {
return timeoutUnit;
}
}
7 changes: 7 additions & 0 deletions src/main/java/j8spec/junit/ExampleStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import j8spec.Example;
import org.junit.internal.runners.statements.ExpectException;
import org.junit.internal.runners.statements.FailOnTimeout;
import org.junit.runners.model.Statement;

final class ExampleStatement extends Statement {
Expand All @@ -15,6 +16,12 @@ public static Statement newStatement(Example example) {
statement = new ExpectException(statement, example.expected());
}

if (example.shouldFailOnTimeout()) {
statement = FailOnTimeout.builder()
.withTimeout(example.timeout(), example.timeoutUnit())
.build(statement);
}

return statement;
}

Expand Down
30 changes: 27 additions & 3 deletions src/test/java/j8spec/junit/J8SpecRunnerTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package j8spec.junit;

import j8spec.annotation.DefinedOrder;
import j8spec.Example;
import j8spec.UnsafeBlock;
import j8spec.annotation.DefinedOrder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestTimedOutException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static j8spec.J8Spec.*;
import static j8spec.J8Spec.describe;
import static j8spec.J8Spec.it;
import static j8spec.J8Spec.xit;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

public class J8SpecRunnerTest {

Expand All @@ -38,6 +45,8 @@ public static class SampleSpec {{
it(BLOCK_4, c -> c.expected(CustomException.class), newBlock(BLOCK_4));

describe("describe A", () -> it("block A.1", () -> {}));

it("block 5", c -> c.timeout(500, MILLISECONDS), () -> Thread.sleep(1000));
}}

private static Map<String, UnsafeBlock> blocks;
Expand Down Expand Up @@ -238,4 +247,19 @@ public void notifies_failure_when_different_exception_occurs() throws Throwable
assertThat(listener.getDescription(), is(runner.describeChild(examples.get(3))));
assertThat(listener.getException(), instanceOf(Exception.class));
}

@Test
public void notifies_failure_when_example_times_out() throws InitializationError {
J8SpecRunner runner = new J8SpecRunner(SampleSpec.class);
List<Example> examples = runner.getChildren();

RunNotifier runNotifier = new RunNotifier();
RunListenerHelper listener = new RunListenerHelper();
runNotifier.addListener(listener);

runner.runChild(examples.get(5), runNotifier);

assertThat(listener.getDescription(), is(runner.describeChild(examples.get(5))));
assertThat(listener.getException(), instanceOf(TestTimedOutException.class));
}
}