Package net.jodah.concurrentunit
Class Waiter
- java.lang.Object
-
- net.jodah.concurrentunit.Waiter
-
public class Waiter extends java.lang.Object
Waits on a test, carrying out assertions, until being resumed.
-
-
Field Summary
Fields Modifier and Type Field Description private ReentrantCircuit
circuit
private java.lang.Throwable
failure
private java.util.concurrent.atomic.AtomicInteger
remainingResumes
private static java.lang.String
TIMEOUT_MESSAGE
-
Constructor Summary
Constructors Constructor Description Waiter()
Creates a new Waiter.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
assertEquals(java.lang.Object expected, java.lang.Object actual)
Asserts that theexpected
values equals theactual
valuevoid
assertFalse(boolean condition)
Asserts that thecondition
is false.void
assertNotNull(java.lang.Object object)
Asserts that theobject
is not null.void
assertNull(java.lang.Object object)
Asserts that theobject
is null.<T> void
assertThat(T actual, org.hamcrest.Matcher<? super T> matcher)
Asserts thatactual
satisfies the condition specified bymatcher
.void
assertTrue(boolean condition)
Asserts that thecondition
is true.void
await()
Waits untilresume()
is called, or the test is failed.void
await(long delay)
Waits until thedelay
has elapsed,resume()
is called, or the test is failed.void
await(long delay, int expectedResumes)
void
await(long delay, java.util.concurrent.TimeUnit timeUnit)
Waits until thedelay
has elapsed,resume()
is called, or the test is failed.void
await(long delay, java.util.concurrent.TimeUnit timeUnit, int expectedResumes)
void
fail()
Fails the current test.void
fail(java.lang.String reason)
Fails the current test for the givenreason
.void
fail(java.lang.Throwable reason)
Fails the current test with the givenreason
, sets the number of expected resumes to 0, and throws thereason
as anAssertionError
in the main test thread and in the current thread.private java.lang.String
format(java.lang.Object expected, java.lang.Object actual)
void
resume()
Resumes the waiter when the expected number ofresume()
calls have occurred.void
rethrow(java.lang.Throwable failure)
Rethrows thefailure
in the main test thread and in the current thread.private static void
sneakyThrow(java.lang.Throwable t)
private static <T extends java.lang.Throwable>
voidsneakyThrow2(java.lang.Throwable t)
-
-
-
Field Detail
-
TIMEOUT_MESSAGE
private static final java.lang.String TIMEOUT_MESSAGE
- See Also:
- Constant Field Values
-
remainingResumes
private java.util.concurrent.atomic.AtomicInteger remainingResumes
-
circuit
private final ReentrantCircuit circuit
-
failure
private volatile java.lang.Throwable failure
-
-
Method Detail
-
assertEquals
public void assertEquals(java.lang.Object expected, java.lang.Object actual)
Asserts that theexpected
values equals theactual
value- Throws:
java.lang.AssertionError
- when the assertion fails
-
assertFalse
public void assertFalse(boolean condition)
Asserts that thecondition
is false.- Throws:
java.lang.AssertionError
- when the assertion fails
-
assertNotNull
public void assertNotNull(java.lang.Object object)
Asserts that theobject
is not null.- Throws:
java.lang.AssertionError
- when the assertion fails
-
assertNull
public void assertNull(java.lang.Object object)
Asserts that theobject
is null.- Throws:
java.lang.AssertionError
- when the assertion fails
-
assertTrue
public void assertTrue(boolean condition)
Asserts that thecondition
is true.- Throws:
java.lang.AssertionError
- when the assertion fails
-
assertThat
public <T> void assertThat(T actual, org.hamcrest.Matcher<? super T> matcher)
Asserts thatactual
satisfies the condition specified bymatcher
.- Throws:
java.lang.AssertionError
- when the assertion fails
-
await
public void await() throws java.util.concurrent.TimeoutException, java.lang.InterruptedException
Waits untilresume()
is called, or the test is failed.- Throws:
java.util.concurrent.TimeoutException
- if the operation times out while waitingjava.lang.InterruptedException
- if the operations is interrupted while waitingjava.lang.AssertionError
- if any assertion fails while waiting
-
await
public void await(long delay) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException
Waits until thedelay
has elapsed,resume()
is called, or the test is failed.- Parameters:
delay
- Delay to wait in milliseconds- Throws:
java.util.concurrent.TimeoutException
- if the operation times out while waitingjava.lang.InterruptedException
- if the operations is interrupted while waitingjava.lang.AssertionError
- if any assertion fails while waiting
-
await
public void await(long delay, java.util.concurrent.TimeUnit timeUnit) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException
Waits until thedelay
has elapsed,resume()
is called, or the test is failed.- Parameters:
delay
- Delay to wait fortimeUnit
- TimeUnit to delay for- Throws:
java.util.concurrent.TimeoutException
- if the operation times out while waitingjava.lang.InterruptedException
- if the operations is interrupted while waitingjava.lang.AssertionError
- if any assertion fails while waiting
-
await
public void await(long delay, int expectedResumes) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException
- Parameters:
delay
- Delay to wait for in millisecondsexpectedResumes
- Number of timesresume()
is expected to be called before the awaiting thread is resumed- Throws:
java.util.concurrent.TimeoutException
- if the operation times out while waitingjava.lang.InterruptedException
- if the operations is interrupted while waitingjava.lang.AssertionError
- if any assertion fails while waiting
-
await
public void await(long delay, java.util.concurrent.TimeUnit timeUnit, int expectedResumes) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException
- Parameters:
delay
- Delay to wait fortimeUnit
- TimeUnit to delay forexpectedResumes
- Number of timesresume()
is expected to be called before the awaiting thread is resumed- Throws:
java.util.concurrent.TimeoutException
- if the operation times out while waitingjava.lang.InterruptedException
- if the operations is interrupted while waitingjava.lang.AssertionError
- if any assertion fails while waiting
-
resume
public void resume()
Resumes the waiter when the expected number ofresume()
calls have occurred.
-
fail
public void fail()
Fails the current test.- Throws:
java.lang.AssertionError
-
fail
public void fail(java.lang.String reason)
Fails the current test for the givenreason
.- Throws:
java.lang.AssertionError
-
fail
public void fail(java.lang.Throwable reason)
Fails the current test with the givenreason
, sets the number of expected resumes to 0, and throws thereason
as anAssertionError
in the main test thread and in the current thread.- Throws:
java.lang.AssertionError
- wrapping thereason
-
rethrow
public void rethrow(java.lang.Throwable failure)
Rethrows thefailure
in the main test thread and in the current thread. Differs fromfail(Throwable)
which wraps a failure in an AssertionError before throwing.- Throws:
java.lang.Throwable
- thefailure
-
sneakyThrow
private static void sneakyThrow(java.lang.Throwable t)
-
sneakyThrow2
private static <T extends java.lang.Throwable> void sneakyThrow2(java.lang.Throwable t) throws T extends java.lang.Throwable
- Throws:
T extends java.lang.Throwable
-
format
private java.lang.String format(java.lang.Object expected, java.lang.Object actual)
-
-