Skip to content

Instantly share code, notes, and snippets.

@dfox
Created April 27, 2011 15:37
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 dfox/944494 to your computer and use it in GitHub Desktop.
Save dfox/944494 to your computer and use it in GitHub Desktop.
A unit test for a contrived asynchronous example class
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
public class AsynchronousTaskTest implements Callback {
private CountDownLatch latch;
public void completed() {
latch.countDown();
}
@Test
public void testTask() throws InterruptedException {
latch = new CountDownLatch(1);
AsynchronousTask task = new AsynchronousTask(this);
task.start();
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals(42, task.getResult());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment