0

I am trying to test a scenario in my code when poll() returns an error. But I don't know how to force poll() to return an error. I tried to make poll() block indefinitely and try to send it a SIGINT but that simply stops the process.

Is there a way to get poll() to return an error?

Thanks.

siri
  • 123
  • 1
  • 13

1 Answers1

0

Is there a way to get poll() to return an error?

Perhaps with some errors, but a number of potential errors exist. A general, not poll() specific, approach follows.


Sometimes test code needs to inject a fake error with alternate code.

The correctness of this general approach is highly dependent code and test goals.

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 See notes section about EAGAIN, EINTR.

Of course using alternate code may hide a problem that occurs only with true code.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256