public FileCleanTaskResponse poll(int num, long timeout, TimeUnit unit) { FileCleanTaskResponse taskResponse = new FileCleanTaskResponse(); try { List<FileCleanTask> cleanTasks = cleanTaskLoopQueue.poll(num,timeout,unit); int cleanTaskscount=cleanTasks.size(); log.info("cleanTaskLoopQueue poll:"+cleanTaskscount); taskResponse.getCleanTasks().addAll(cleanTasks); } catch (InterruptedException e) { Thread.currentThread().interrupt(); log.warn("take clean task error", e); } return taskResponse; } 写UT
时间: 2024-04-28 10:20:43 浏览: 45
以下是可能的UT示例:
@Test
public void testPoll() {
// 创建几个FileCleanTask对象
FileCleanTask task1 = new FileCleanTask();
FileCleanTask task2 = new FileCleanTask();
FileCleanTask task3 = new FileCleanTask();
// 将它们添加到cleanTaskLoopQueue中
cleanTaskLoopQueue.add(task1);
cleanTaskLoopQueue.add(task2);
cleanTaskLoopQueue.add(task3);
// 测试poll方法
FileCleanTaskResponse response = poll(2, 5, TimeUnit.SECONDS);
// 验证返回结果是否正确
assertEquals(2, response.getCleanTasks().size());
assertTrue(response.getCleanTasks().contains(task1));
assertTrue(response.getCleanTasks().contains(task2));
assertFalse(response.getCleanTasks().contains(task3));
}
这个测试用例的思路是:创建几个FileCleanTask对象,并将它们添加到cleanTaskLoopQueue中。然后调用poll方法,请求返回2个任务,并等待5秒钟。最后验证返回结果是否正确:结果应该包含2个任务,并且这些任务应该是task1和task2。
阅读全文