Karaf Startup 的 JUnitTest
JUnitTest for Karaf Startup
我想编写一个 JUnitTest,它可以确保我的 Karaf 服务器正常启动并且所有(需要的)Bundle 都已安装并处于活动状态。
为此我有一个测试,调用辅助方法 "assertBundleState" 确保给定的 Bundle 处于给定的状态。测试如下所示:
@Test (timeout=30000L)
public void testBundlesStarted() throws Exception {
assertBundleState("bundle.Name", BundleLifecycleState.Active);
... <other bundles in similar way>
}
这在过去工作得很好。从未达到超时。
不幸的是,我现在必须加载一个包,启动需要更长的时间。因此,在执行测试时,Bundles 尚未启动。因此测试失败。
我已经在 BeforeClass 方法中尝试过睡眠,但我对这个解决方案不是很满意。因为我不能确保给定的睡眠时间在每台机器上每次都足够。所以我正在寻找一些基于事件的解决方案来与 Karaf 交互。
@BeforeClass
public static void init() throws Exception {
Thread.sleep(120000L);
}
有没有人知道我可以如何以更好的方式解决它?
谢谢
听起来assertBundleState
可以成功判断Karaf服务器是否已经启动'is ready'。但问题是你不知道要等多久才能检查这个;如果你等待的时间不够长,你就有可能出现假阴性,如果你等待的时间太长,那么你的构建运行时间就会被错误地延长。
我假设您无法注册 Karaf 服务器在 'is ready' 时将调用的某种侦听器或回调挂钩。事实上,即使那是可能的,你仍然必须迎合失败的情况,即 Karaf 服务器没有启动的地方(因此永远无法调用监听器)。
因此,我认为您只需等待 Karaf 服务器可用,并隐藏 Thread.sleep
调用的丑陋之处以及等待时间过长可能浪费的时间。您可以使用 Awaitility 勾选这些方框。例如:
@Test
public void aTest() {
// run your test
// this will wait until Karaf is available for at most 10 seconds
// and will check every 100ms so if Karaf becomes available earlier
// than 10 seconds then the test will complete almost as soon as
// it becomes available but if Karaf does not become available
// within 10 seconds then the test will fail
await().atMost(10, SECONDS).until(karafIsAvailable());
// assert
// ...
}
private Callable<Boolean> karafIsAvailable() {
return new Callable<Boolean>() {
public Boolean call() throws Exception {
// return true if your condition has been met
// e.g. assertBundleState
return ...;
}
};
}
我想编写一个 JUnitTest,它可以确保我的 Karaf 服务器正常启动并且所有(需要的)Bundle 都已安装并处于活动状态。
为此我有一个测试,调用辅助方法 "assertBundleState" 确保给定的 Bundle 处于给定的状态。测试如下所示:
@Test (timeout=30000L)
public void testBundlesStarted() throws Exception {
assertBundleState("bundle.Name", BundleLifecycleState.Active);
... <other bundles in similar way>
}
这在过去工作得很好。从未达到超时。
不幸的是,我现在必须加载一个包,启动需要更长的时间。因此,在执行测试时,Bundles 尚未启动。因此测试失败。
我已经在 BeforeClass 方法中尝试过睡眠,但我对这个解决方案不是很满意。因为我不能确保给定的睡眠时间在每台机器上每次都足够。所以我正在寻找一些基于事件的解决方案来与 Karaf 交互。
@BeforeClass
public static void init() throws Exception {
Thread.sleep(120000L);
}
有没有人知道我可以如何以更好的方式解决它? 谢谢
听起来assertBundleState
可以成功判断Karaf服务器是否已经启动'is ready'。但问题是你不知道要等多久才能检查这个;如果你等待的时间不够长,你就有可能出现假阴性,如果你等待的时间太长,那么你的构建运行时间就会被错误地延长。
我假设您无法注册 Karaf 服务器在 'is ready' 时将调用的某种侦听器或回调挂钩。事实上,即使那是可能的,你仍然必须迎合失败的情况,即 Karaf 服务器没有启动的地方(因此永远无法调用监听器)。
因此,我认为您只需等待 Karaf 服务器可用,并隐藏 Thread.sleep
调用的丑陋之处以及等待时间过长可能浪费的时间。您可以使用 Awaitility 勾选这些方框。例如:
@Test
public void aTest() {
// run your test
// this will wait until Karaf is available for at most 10 seconds
// and will check every 100ms so if Karaf becomes available earlier
// than 10 seconds then the test will complete almost as soon as
// it becomes available but if Karaf does not become available
// within 10 seconds then the test will fail
await().atMost(10, SECONDS).until(karafIsAvailable());
// assert
// ...
}
private Callable<Boolean> karafIsAvailable() {
return new Callable<Boolean>() {
public Boolean call() throws Exception {
// return true if your condition has been met
// e.g. assertBundleState
return ...;
}
};
}