Last active
November 8, 2017 05:04
-
-
Save nddipiazza/2ba54f5c09f2f51b50d10e4d0555f394 to your computer and use it in GitHub Desktop.
easier to see example of this
This file contains 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
package test; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Future; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
public class TestTimeout { | |
@Test | |
public void testTimeout() throws Exception { | |
try { | |
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); | |
final Future<Integer> handler = executor.submit(new Callable<Integer>() { | |
@Override | |
public Integer call() throws Exception { | |
Thread.sleep(8000L); | |
return 5; | |
} | |
}); | |
executor.schedule((Runnable) () -> handler.cancel(true), | |
5000, | |
TimeUnit.MILLISECONDS); | |
handler.get(); | |
} catch (Exception e) { | |
return; | |
} | |
Assert.fail("Should have timed out"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment