Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中测试IO问题?_Java_Unit Testing_Io - Fatal编程技术网

如何在Java中测试IO问题?

如何在Java中测试IO问题?,java,unit-testing,io,Java,Unit Testing,Io,我如何在IO性能非常差的情况下测试应用程序代码的行为,而不使用休眠的模拟流(因为它们会对中断做出反应) 例如,我想测试一个具有文件IO线程池的ConcurrentWrapper实用程序。它通过invokeAll()超时将每个操作提交给ExecutorService。我想确认的不仅是使用ConcurrentWrapper的调用在超时之前退出,而且它以某种方式使其内部ExecutorService的线程终止(以避免泄漏) 我需要在内部线程中模拟慢IO,但要忽略中断(就像真实IO一样) 一点澄清:像“

我如何在IO性能非常差的情况下测试应用程序代码的行为,而不使用休眠的模拟流(因为它们会对中断做出反应)

例如,我想测试一个具有文件IO线程池的
ConcurrentWrapper
实用程序。它通过
invokeAll()
超时将每个操作提交给
ExecutorService
。我想确认的不仅是使用
ConcurrentWrapper
的调用在超时之前退出,而且它以某种方式使其内部
ExecutorService
的线程终止(以避免泄漏)

我需要在内部线程中模拟慢IO,但要忽略中断(就像真实IO一样)


一点澄清:像“睡眠和吞咽
中断异常
”或“睡眠,捕捉
中断异常
并返回睡眠”这样的回答是不可接受的。我想测试我的代码是如何处理中断的,而这样的插装将无法通过自己处理中断来达到目的。

您可以以一种坚持在中断中睡眠的方式睡眠:

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) {
    }
}
long start=System.currentTimeMillis();
长端=开始+睡眠时间;
for(long now=start;now
对于超时测试,您实际上可以设置执行测试的最长时间,您可以包括注释超时:

@Test(timeout=100)
public void method_withTimeout() {
       while(true);
}

对于方法退出的测试部分,您可以使用提供获取结果超时的接口。

如果我正确理解您的问题,ReentrantLock可能会有所帮助

final ReentrantLock lock = new ReentrantLock();

Callable<Void> c = new Callable<Void>() {
  public void call() {
    lock.lock();

    try {

       if (Thread.currentThread().isInterrupted()) {
         ...
       }
    }
    finally {
      lock.unlock();
    }
  }
}

// Submit to the pool
Future<Void> future = executorService.submit(c);

// you might want to sleep a bit to give the pool a chance
// to pull off the queue.

// Issue a cancel
future.cancel();

// Now release the lock, which should let your
// callable continue onto to the interrupted check.
lock.unlock();
final ReentrantLock lock=new ReentrantLock();
可调用c=新的可调用(){
公开作废通知(){
lock.lock();
试一试{
如果(Thread.currentThread().isInterrupted()){
...
}
}
最后{
lock.unlock();
}
}
}
//提交到池中
Future=executorService.submit(c);
//你可能想睡一会儿,给游泳池一个机会
//停止排队。
//发出取消通知
future.cancel();
//现在释放锁,这将使您的
//可调用继续到中断的检查。
lock.unlock();

请注意,“lock”方法不会抛出任何InterruptedException(尽管有一个称为“lockInterruptibly”的方法),如果您查看该类的代码,它不会捕获和吞咽(正如您所说的,这不是您想要的)。

让它睡眠一段指定的时间,如果它很早醒来,回到睡眠状态?那么您想要一个阻塞(模拟慢速IO)的线程,然后抛出一个中断的异常,但继续阻塞(模拟继续执行的IO)?这没有多大意义。随着时间的推移,方法调用会阻塞、返回或抛出异常;它不可能做到三分之二。你能更清楚地说明你想要完成什么吗?我在第二段中描述了用例-为包装器/助手编写测试,限制调用的持续时间,不仅验证调用是否完成,但它也不会通过让任务仍在后台线程中运行而泄漏到内部池中。这是如何模拟慢速IO的?关于慢速IO,一个非最佳的可能性是用计算来代替发送数据段之间的睡眠。如果我不够清楚,请看一看更新后的问题。我想测试我的代码如何处理中断,因此插装必须完全忽略它们。无声吞咽还不够不引人注目。