Jenkins 是否等待当前测试启动的线程终止以启动另一个测试?
Does Jenkins wait for the threads started by the current test to terminate in order to start another test?
我有一个代码可以将 运行s 线程调度为 TimerTask
-s。
Jenkins 控制它的测试顺序 运行s-- 在一个测试单元 returns 之前不会 运行 另一个。
但是 - Jenkins 是否以类似的方式控制线程启动测试?
我们的测试失败了,这是我能想到的唯一原因。
另一方面,integration/build 环境有严格的控制也不足为奇——启动一个测试,
等到处理器清除它启动的每个线程,然后继续下一个测试。
TIA
JUnit 测试用例在主线程终止时终止。如果您将 TimerTasks 安排为 运行,您的测试将不会等待它们完成,除非您成功完成,这可能是您的问题。您可以让主线程等待,例如使用闩锁:
public class TimerThreadTest {
final static int NUM_THREADS = 5;
final CountDownLatch latch = new CountDownLatch(NUM_THREADS);
@Test
public void threadTest() throws InterruptedException {
System.out.println("Starting");
Timer timer = new Timer(true);
for (int i = 0; i < NUM_THREADS; i++){
final int threadNo = i;
TimerTask tt = new TimerTask() {
@Override
public void run() {
latch.countDown();
System.out.println("thread " + threadNo + " done");
}
};
timer.schedule(tt, 1000);
}
latch.await();
System.out.println("Main thread done");
}
}
我有一个代码可以将 运行s 线程调度为 TimerTask
-s。
Jenkins 控制它的测试顺序 运行s-- 在一个测试单元 returns 之前不会 运行 另一个。
但是 - Jenkins 是否以类似的方式控制线程启动测试?
我们的测试失败了,这是我能想到的唯一原因。
另一方面,integration/build 环境有严格的控制也不足为奇——启动一个测试, 等到处理器清除它启动的每个线程,然后继续下一个测试。
TIA
JUnit 测试用例在主线程终止时终止。如果您将 TimerTasks 安排为 运行,您的测试将不会等待它们完成,除非您成功完成,这可能是您的问题。您可以让主线程等待,例如使用闩锁:
public class TimerThreadTest {
final static int NUM_THREADS = 5;
final CountDownLatch latch = new CountDownLatch(NUM_THREADS);
@Test
public void threadTest() throws InterruptedException {
System.out.println("Starting");
Timer timer = new Timer(true);
for (int i = 0; i < NUM_THREADS; i++){
final int threadNo = i;
TimerTask tt = new TimerTask() {
@Override
public void run() {
latch.countDown();
System.out.println("thread " + threadNo + " done");
}
};
timer.schedule(tt, 1000);
}
latch.await();
System.out.println("Main thread done");
}
}