本文整理汇总了Java中java.util.TimerTask.run方法的典型用法代码示例。如果您正苦于以下问题:Java TimerTask.run方法的具体用法?Java TimerTask.run怎么用?Java TimerTask.run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.TimerTask
的用法示例。
在下文中一共展示了TimerTask.run方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testDelayedCondensedExecution
import java.util.TimerTask; //导入方法依赖的package包/类
@Test
public void testDelayedCondensedExecution() throws Exception {
CondensedQueryExecutor uut = new CondensedQueryExecutor(104, callback, () -> true,
mockTimer);
// not yet scheduled anything
verify(mockTimer, never()).schedule(anyObject(), anyLong());
uut.trigger();
// scheduled once
verify(mockTimer).schedule(anyObject(), eq(104L));
uut.trigger();
uut.trigger();
uut.trigger();
uut.trigger();
// still scheduled only once
verify(mockTimer).schedule(anyObject(), eq(104L));
TimerTask taskArg = task.getValue();
taskArg.run();
// executing must noch change anything for scheduling
verify(mockTimer).schedule(anyObject(), eq(104L));
verifyNoMoreInteractions(mockTimer);
uut.trigger();
// a second call is scheduled
verify(mockTimer, times(2)).schedule(anyObject(), eq(104L));
uut.trigger();
uut.trigger();
uut.trigger();
// no change: second call is scheduled
verify(mockTimer, times(2)).schedule(anyObject(), eq(104L));
}
示例2: testNodeHealthScript
import java.util.TimerTask; //导入方法依赖的package包/类
@Test
public void testNodeHealthScript() throws Exception {
String errorScript = "echo ERROR\n echo \"Tracker not healthy\"";
String normalScript = "echo \"I am all fine\"";
String timeOutScript =
Shell.WINDOWS ? "@echo off\nping -n 4 127.0.0.1 >nul\necho \"I am fine\""
: "sleep 4\necho \"I am fine\"";
Configuration conf = new Configuration();
writeNodeHealthScriptFile(normalScript, true);
NodeHealthScriptRunner nodeHealthScriptRunner = new NodeHealthScriptRunner(
nodeHealthscriptFile.getAbsolutePath(),
500, 1000, new String[] {});
nodeHealthScriptRunner.init(conf);
TimerTask timerTask = nodeHealthScriptRunner.getTimerTask();
timerTask.run();
// Normal Script runs successfully
Assert.assertTrue("Node health status reported unhealthy",
nodeHealthScriptRunner.isHealthy());
Assert.assertEquals("", nodeHealthScriptRunner.getHealthReport());
// Error script.
writeNodeHealthScriptFile(errorScript, true);
// Run timer
timerTask.run();
Assert.assertFalse("Node health status reported healthy",
nodeHealthScriptRunner.isHealthy());
Assert.assertTrue(
nodeHealthScriptRunner.getHealthReport().contains("ERROR"));
// Healthy script.
writeNodeHealthScriptFile(normalScript, true);
timerTask.run();
Assert.assertTrue("Node health status reported unhealthy",
nodeHealthScriptRunner.isHealthy());
Assert.assertEquals("", nodeHealthScriptRunner.getHealthReport());
// Timeout script.
writeNodeHealthScriptFile(timeOutScript, true);
timerTask.run();
Assert.assertFalse("Node health status reported healthy even after timeout",
nodeHealthScriptRunner.isHealthy());
Assert.assertEquals(
NodeHealthScriptRunner.NODE_HEALTH_SCRIPT_TIMED_OUT_MSG,
nodeHealthScriptRunner.getHealthReport());
}