0

当 poll() 返回错误时,我正在尝试在我的代码中测试一个场景。但我不知道如何强制 poll() 返回错误。我试图让 poll() 无限期地阻塞并尝试向它发送一个 SIGINT 但这只会停止该过程。

有没有办法让 poll() 返回错误?

谢谢。

4

1 回答 1

0

有没有办法让 poll() 返回错误?

也许有一些错误,但存在一些潜在的错误。下面是一般的而非poll()具体的方法。


有时测试代码需要使用替代代码注入虚假错误。

这种通用方法的正确性高度依赖于代码和测试目标。

int poll_result = poll(&fds, nfds, timeout);
#if TEST1
  if (poll_result != -1) {
    // Avoid using rand()
    static unsigned seed = 0;
    seed = (16807 * seed) mod 2147483647;  // https://stackoverflow.com/a/9492699/2410359

    if (seed % 97 == 0) { // about 1% of the time 
      // adjust as desired,  e.g. EINVAL may not make sense here.
      int faults[] = { EFAULT, EINTR, EINVAL, ENOMEM };
      const unsigned n = sizeof faults / sizeof faults[0];
      errno =  faults[seed % n];
      poll_result = -1;
    }
  }
#endif
...

poll()ref 请参阅关于EAGAIN, EINTR.

当然,使用备用代码可能会隐藏只有真正代码才会出现的问题。

于 2017-06-29T18:25:43.253 回答