// Path: src/main/java/com/google/code/tempusfugit/condition/Conditions.java
// public static Condition isWaiting(Thread thread) {
//     return () -> (thread.getState() == TIMED_WAITING) || (thread.getState() == WAITING);
// }
// 
// Path: src/main/java/com/google/code/tempusfugit/condition/Conditions.java
// public static Condition not(Condition condition) {
//     return () -> !condition.isSatisfied();
// }
// 
// Path: src/main/java/com/google/code/tempusfugit/temporal/Duration.java
// public static Duration seconds(long seconds) {
//     validate(seconds, TimeUnit.SECONDS);
//     return new Duration(seconds, TimeUnit.SECONDS);
// }
// 
// Path: src/main/java/com/google/code/tempusfugit/temporal/Timeout.java
// public static Timeout timeout(Duration duration) {
//     return new Timeout(duration);
// }
// 
// Path: src/main/java/com/google/code/tempusfugit/temporal/WaitFor.java
// public static void waitOrTimeout(Condition condition, Timeout timeout) throws InterruptedException, TimeoutException {
//     waitOrTimeout(condition, timeout, new ThreadSleep(SLEEP_PERIOD));
// }


import static com.google.code.tempusfugit.temporal.WaitFor.waitOrTimeout;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.concurrent.TimeoutException;
import static com.google.code.tempusfugit.condition.Conditions.isWaiting;
import static com.google.code.tempusfugit.condition.Conditions.not;
import static com.google.code.tempusfugit.temporal.Duration.seconds;
import static com.google.code.tempusfugit.temporal.Timeout.timeout;

    @Test
    public void resetInterruptFlagThrowsException() throws InterruptedException {
        context.checking(new Expectations() {{
            oneOf(interruptible).call(); will(throwException(new InterruptedException()));
        }});
        assertThat(Thread.currentThread().isInterrupted(), is(false));
        ThreadUtils.resetInterruptFlagWhen(interruptible);
        assertThat(Thread.interrupted(), is(true));
    }

    @Test
    public void sleepInterrupted() throws InterruptedException, TimeoutException {
        InterruptedIndicatingThread thread = threadSleepsForever();
        thread.start();
        waitForStartup(thread);
        thread.interrupt();
        waitForShutdown(thread);
        assertThat(thread.getName() + " wasn't interrupted", thread.hasBeenInterrupted(), is(true));
    }

    private void waitForStartup(Thread thread) throws TimeoutException, InterruptedException {
        waitOrTimeout(isWaiting(thread), timeout(seconds(10)));
    }

    private InterruptedIndicatingThread threadSleepsForever() {
        return new InterruptedIndicatingThread(() -> ThreadUtils.sleep(seconds(10)), "sleeping-thread");
    }

    private void waitForShutdown(final Thread thread) throws TimeoutException, InterruptedException {
// Path: src/main/java/com/google/code/tempusfugit/condition/Conditions.java
// public static Condition isWaiting(Thread thread) {
//     return () -> (thread.getState() == TIMED_WAITING) || (thread.getState() == WAITING);
// }
// 
// Path: src/main/java/com/google/code/tempusfugit/condition/Conditions.java
// public static Condition not(Condition condition) {
//     return () -> !condition.isSatisfied();
// }
// 
// Path: src/main/java/com/google/code/tempusfugit/temporal/Duration.java
// public static Duration seconds(long seconds) {
//     validate(seconds, TimeUnit.SECONDS);
//     return new Duration(seconds, TimeUnit.SECONDS);
// }
// 
// Path: src/main/java/com/google/code/tempusfugit/temporal/Timeout.java
// public static Timeout timeout(Duration duration) {
//     return new Timeout(duration);
// }
// 
// Path: src/main/java/com/google/code/tempusfugit/temporal/WaitFor.java
// public static void waitOrTimeout(Condition condition, Timeout timeout) throws InterruptedException, TimeoutException {
//     waitOrTimeout(condition, timeout, new ThreadSleep(SLEEP_PERIOD));
// }


// Path: src/test/java/com/google/code/tempusfugit/concurrency/ThreadUtilsTest.java
import static com.google.code.tempusfugit.temporal.WaitFor.waitOrTimeout;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.concurrent.TimeoutException;
import static com.google.code.tempusfugit.condition.Conditions.isWaiting;
import static com.google.code.tempusfugit.condition.Conditions.not;
import static com.google.code.tempusfugit.temporal.Duration.seconds;
import static com.google.code.tempusfugit.temporal.Timeout.timeout;


    @Test
    public void resetInterruptFlagThrowsException() throws InterruptedException {
        context.checking(new Expectations() {{
            oneOf(interruptible).call(); will(throwException(new InterruptedException()));
        }});
        assertThat(Thread.currentThread().isInterrupted(), is(false));
        ThreadUtils.resetInterruptFlagWhen(interruptible);
        assertThat(Thread.interrupted(), is(true));
    }

    @Test
    public void sleepInterrupted() throws InterruptedException, TimeoutException {
        InterruptedIndicatingThread thread = threadSleepsForever();
        thread.start();
        waitForStartup(thread);
        thread.interrupt();
        waitForShutdown(thread);
        assertThat(thread.getName() + " wasn't interrupted", thread.hasBeenInterrupted(), is(true));
    }

    private void waitForStartup(Thread thread) throws TimeoutException, InterruptedException {
        waitOrTimeout(isWaiting(thread), timeout(seconds(10)));
    }

    private InterruptedIndicatingThread threadSleepsForever() {
        return new InterruptedIndicatingThread(() -> ThreadUtils.sleep(seconds(10)), "sleeping-thread");
    }

    private void waitForShutdown(final Thread thread) throws TimeoutException, InterruptedException {
        waitOrTimeout(not(isWaiting(thread)), timeout(seconds(10)));