Skip to content

Instantly share code, notes, and snippets.

@abyx
Created March 24, 2012 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abyx/2182545 to your computer and use it in GitHub Desktop.
Save abyx/2182545 to your computer and use it in GitHub Desktop.
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