Skip to content

Instantly share code, notes, and snippets.

@abyx
Created Mar 24, 2012
Embed
What would you like to do?
public void betterTest() throws Exception {
// Do some setup
// ...
final Exchanger<Exception> exchanger = new Exchanger<Exception>();
new Thread() {
@Override
public void run() {
Exception thrown = null;
try {
int x = 0;
// Check a few things that change x
// ...
assertTrue("An error", x > 3); // This fails!
} catch (Exception e) {
thrown = e;
}
try {
exchanger.exchange(thrown);
} catch (InterruptedException ignored) {}
}
}.start();
// Wait for other thread
Exception exception = exchanger.exchange(null);
if (exception != null) {
throw exception;
}
}
public void buggieTest() throws Exception {
// Do some setup
// ...
new Thread() {
@Override
public void run() {
int x = 0;
// Check a few things that change x
// ...
assertTrue("An error", x > 3); // This fails!
}
}.start();
// Wait for other thread
Thread.sleep(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment