Created
October 17, 2018 17:39
-
-
Save Bill/44d3476e52603a3d6e3882089fea6ff1 to your computer and use it in GitHub Desktop.
Java Future Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Before; | |
import org.junit.Test; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.FutureTask; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.TimeoutException; | |
public class FutureTest { | |
private Callable<Integer> f1 = () -> { | |
System.out.println("+"); | |
Thread.sleep(1000); | |
System.out.println("-"); | |
return 1; | |
}; | |
private FutureTask<Integer> t; | |
@Before | |
public void before() { | |
t = new FutureTask<>(f1); | |
} | |
@Test | |
public void testShouldTimeOut() throws Exception { | |
t.run(); | |
t.get(100, TimeUnit.MILLISECONDS); | |
throw new AssertionError("should have timed out but didn't"); | |
} | |
@Test | |
public void testShouldNotTimeOut() throws Exception { | |
try { | |
t.run(); | |
t.get(2000, TimeUnit.MILLISECONDS); | |
} catch (TimeoutException e) { | |
throw new AssertionError("should not have timed out but did"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment