請考慮使用此測試類,如果運行主過程不會在5分鐘內完成,如果運行測試,則立即成功。有沒有推薦的方法來驗證Executor
行爲?我希望測試也能在5分鐘內完成。使用線程進行JUnit測試 - 測試執行程序行爲
我所遇到的具體問題是,創造一個ScheduledExecutorService
通過Executors.html#newScheduledThreadPool(int),調度未來,然後取消,今後將不會終止底層Executor
作爲默認RemoveOnCancelPolicy是等待取消未來終止之前被調度。我正在通過公開使用ScheduledThreadPoolExecutor
來解決此問題,但我希望將此封裝在我的實現中。
public class TestExecutor {
@Test public void executorThatIsNotShutdown() {
main(null);
}
public static void main(final String[] args) {
final ScheduledExecutorService ex = Executors.newScheduledThreadPool(1);
ex.schedule(new Runnable() {
@Override
public void run() {
}
}, 5, TimeUnit.MINUTES);
ex.shutdown();
System.out.println(ex.toString());
}
}
我在等待執行程序終止時並不感興趣,我試圖測試它是否有效。這是非常困難的,因爲JUnit跑步者似乎爲我殺死了Executor。 –