如何在 IO 性能非常差的情况下测试应用程序代码的行为,而不使用 hibernate 的模拟流(因为它们会对中断使用react)?
例如,我想测试一个 ConcurrentWrapper
实用程序,该实用程序具有用于文件 IO 的线程池。它使用 invokeAll()
将每个操作提交给 ExecutorService
,并设置超时。我不仅想确认 ConcurrentWrapper
的调用在超时之前退出,而且还想确认它以某种方式使其内部 ExecutorService
的线程终止(以避免泄漏)。
我需要以某种方式模拟内部线程中的慢速 IO,但要以忽略中断的方式(就像真正的 IO 那样)。
一点澄清:没有像“ sleep 并吞下 InterruptedException
”或“ sleep ,捕获 InterruptedException
然后回去 sleep ”这样的答案是可以接受的。我想测试我的代码如何处理中断,而这样的检测会通过处理它们本身来达到目的。
最佳答案
您可以以一种在中断时坚持 sleep 的方式 sleep :
long start = System.currentTimeMillis();
long end = start + sleepTime;
for (long now = start; now < end; now = System.currentTimeMillis()) {
try {
Thread.sleep(end - now);
} catch (InterruptedException ignored) {
}
}
关于java - 如何测试Java中的IO问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10883907/