coherence-community / oracle-bedrock

Oracle Bedrock
Other
55 stars 31 forks source link

Introduce Concurrently.assertThat(...) #336

Closed brianoliver closed 8 years ago

brianoliver commented 8 years ago

As a developer of multi-threaded applications, often I'd like to concurrently assert that some condition holds (ie: an invariant) why one or more concurrent operations are occurring.

For example:

try (Concurrent.Assertion assertion = Concurrently.assertThat(...));
{
    // do some work

    assertion.check();   // will throw AssertionError if the concurrent assertion has failed in the background

    // do some other work

    assertion.check();   // will throw AssertionError if the concurrent assertion has failed in the background
}

Additionally, I'd like a Concurrent.Assertion to attempt to interrupt (fail-fast) the creating thread when an AssertionError occurs. For example:

try (Concurrent.Assertion assertion = Concurrently.assertThat(..., FailFast.enabled()));
{
    // do some work
}
catch (InterruptedException e)
{
    // e.getSuppressed() array will contain the AssertionError! (as the assertion is no longer in scope)

    // check that the InterruptedException wasn't caused by an AssertionError, if it was, rethrow the first
    Concurrent.Assertion.check(e);
}