gpt4 book ai didi

java - 需要在等待函数上下文中实现 Thread.sleep(100) 来评估 sleep 时间?

转载 作者:行者123 更新时间:2023-12-02 10:51:34 25 4
gpt4 key购买 nike

以下测试用例对于 sleep 100 失败,但如果 sleep 增加,则测试通过。但由于这是一种懒惰的方法,我想在一个函数中实现它来评估 sleep 时间。这样它在不同环境下无需静态 sleep 值即可正常工作。

    public void testDeRegistration() throws Exception {
storeEntity(EntityType.PROCESS, "summarize4");
Process mockProcess = getStore().get(EntityType.PROCESS, "summarize4");
mockProcess.setParallel(2);
Date startTime = EntityUtil.getStartTime(mockProcess, cluster);
ExecutionInstance instance1 = new ProcessExecutionInstance(mockProcess, new DateTime(startTime), cluster);
// Schedule 3 instances.
SchedulerService.JobScheduleRequestBuilder request = (SchedulerService.JobScheduleRequestBuilder)
*emphasized text* scheduler.createRequestBuilder(handler, instance1.getId());
request.setInstance(instance1);
scheduler.register(request.build());
ExecutionInstance instance2 = new ProcessExecutionInstance(mockProcess,
new DateTime(startTime.getTime() + 60000), cluster);
SchedulerService.JobScheduleRequestBuilder request2 = (SchedulerService.JobScheduleRequestBuilder)
scheduler.createRequestBuilder(handler, instance2.getId());
request2.setInstance(instance2);
scheduler.register(request2.build());
ExecutionInstance instance3 = new ProcessExecutionInstance(mockProcess,
new DateTime(startTime.getTime() + 120000), cluster);
SchedulerService.JobScheduleRequestBuilder request3 = (SchedulerService.JobScheduleRequestBuilder)
scheduler.createRequestBuilder(handler, instance3.getId());
request3.setInstance(instance3);
scheduler.register(request3.build());

// Abort third instance
stateStore.putExecutionInstance(new InstanceState(instance3));
scheduler.unregister(handler, instance3.getId());

Thread.sleep(100);
Assert.assertEquals(((MockDAGEngine) mockDagEngine).getTotalRuns(instance1), new Integer(1));
Assert.assertEquals(((MockDAGEngine) mockDagEngine).getTotalRuns(instance2), new Integer(1));
// Second instance should not run.
Assert.assertEquals(((MockDAGEngine) mockDagEngine).getTotalRuns(instance3), null);
}

最佳答案

尝试awaitility

Awaitility.await().atMost(500, TimeUnit.MILLISECONDS)
.until(() -> Objects.equals(((MockDAGEngine) mockDagEngine).getTotalRuns(instance1), 1))

第二个实例也是如此

关于java - 需要在等待函数上下文中实现 Thread.sleep(100) 来评估 sleep 时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52148299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com