Java JUnit测试“;超时“;尽管执行速度很快?
我有几个测试用例,JUnit告诉我,当整个测试运行只持续几秒钟时,超时时间为10000ms。以下是输出:Java JUnit测试“;超时“;尽管执行速度很快?,java,junit,junit4,maven-surefire-plugin,Java,Junit,Junit4,Maven Surefire Plugin,我有几个测试用例,JUnit告诉我,当整个测试运行只持续几秒钟时,超时时间为10000ms。以下是输出: Tests run: 3, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 2.528 sec <<< FAILURE! closeTest1(com.w2ogroup.analytics.sibyl.transport.impl.http.server.HttpServerTransportTests) Time el
Tests run: 3, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 2.528 sec <<< FAILURE!
closeTest1(com.w2ogroup.analytics.sibyl.transport.impl.http.server.HttpServerTransportTests) Time elapsed: 1.654 sec <<< ERROR!
java.lang.Exception: test timed out after 10000 milliseconds
closeTest2(com.w2ogroup.analytics.sibyl.transport.impl.http.server.HttpServerTransportTests) Time elapsed: 0.672 sec <<< ERROR!
java.lang.Exception: test timed out after 50000 milliseconds
Results :
Tests in error:
HttpServerTransportTests » test timed out after 10000 milliseconds
HttpServerTransportTests » test timed out after 50000 milliseconds
Tests run: 3, Failures: 0, Errors: 2, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.383s
[INFO] Finished at: Sun Jun 09 19:00:09 PDT 2013
[INFO] Final Memory: 9M/129M
[INFO] ------------------------------------------------------------------------
测试使用@Test(timeout=/*number*/)
运行。以下是其中一个测试用例的签名:
@Test(timeout = 10000)
public void closeTest1() throws IOException, InterruptedException {
/* Test goes here */
}
编辑:以下是surefire日志的全部内容:
-------------------------------------------------------------------------------
Test set: com.w2ogroup.analytics.sibyl.transport.impl.http.server.HttpServerTransportTests
-------------------------------------------------------------------------------
Tests run: 3, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 3.136 sec <<< FAILURE!
closeTest1(com.w2ogroup.analytics.sibyl.transport.impl.http.server.HttpServerTransportTests) Time elapsed: 2.218 sec <<< ERROR!
java.lang.Exception: test timed out after 10000 milliseconds
closeTest2(com.w2ogroup.analytics.sibyl.transport.impl.http.server.HttpServerTransportTests) Time elapsed: 0.661 sec <<< ERROR!
java.lang.Exception: test timed out after 50000 milliseconds
-------------------------------------------------------------------------------
测试集:com.w2ogroup.analytics.sibyl.transport.impl.http.server.HttpServerTransportTests
-------------------------------------------------------------------------------
测试运行:3,失败:0,错误:2,跳过:0,经过的时间:3.136秒JUnit检测到测试已超时。这通常是由测试框架调用运行测试的ExecutorService
引起的
有没有可能是您的一个失败测试本身引发了此异常,而JUnit将其报告为测试超时?您是否更改了为这些测试定义的超时
@Test (timeout=10000)
或
看起来连接挂起了
org.eclipse.jetty.server.HttpConnection$Input.blockForContent(HttpConnection.java:588)
几秒钟后,测试并没有真正结束
尝试将注意力集中在HttpConnection错误上(如果需要进一步帮助,请添加代码)
有关此问题,请参见示例:
这是JUnit的一个问题。事实上,如果出现
中断异常
,则会出现“测试超时”消息:
public class FooTest {
@Test(timeout = 10000)
public void timeoutTest() throws Exception {
throw new InterruptedException("hello");
}
}
结果:
java.lang.Exception: test timed out after 10000 milliseconds
至少可以说,这是令人困惑的。即使您使用了。因此,在您的示例中,您正在抛出一个InterruptedException
Caused by: java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:996)
...
这会导致错误的超时异常
这是4.11(及之前版本)中的一个错误,但在4.12-SNAPSHOT中它确实正常工作,它会产生:
java.lang.InterruptedException: hello
at xxx.xxx.xxx.FooTest.timeoutTest(FooTest.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
...
因此,我将尝试使用4.12-SNAPSHOT,如果它起作用,您可以继续使用它(使用您自己的私有副本),或者将新的规则和类复制到您的代码中
然后,当4.12出现时,您可以恢复。顺便说一句,不知道什么时候会这样。给我们看看stacktraces…@StephenC更新了这个问题。很有趣。。。但我指的是包含java.lang.Exception
的stacktrace。它应该在surefire报告中。@StephenC derp。更新了问题,问得好。我正在使用@Test(timeout=10000)
。我更新了问题,这是很好的信息+1.我的代码肯定不会抛出TimeoutException
,我也不认为我的测试是错误的。不过,我正在关闭流并中断NIO线程;您知道为什么这些会抛出TimeoutException
?我将在一个try
/catch
中包装我的测试来测试这个假设。TimeoutException
由ExecutorService
使用,后者由nio
类使用。所以我猜nio
类在内部触发了TimeoutException。
java.lang.Exception: test timed out after 10000 milliseconds
Caused by: java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:996)
...
java.lang.InterruptedException: hello
at xxx.xxx.xxx.FooTest.timeoutTest(FooTest.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
...