jeudi 3 novembre 2016

Controlling time of QTimer?

I'm considering rewriting some existing C code using C++ and Qt. In the C code poll is used to check for input from several sources, and each source has a timeout value and some specific behaviour tied to it. For instance

  • after 5 minutes silence on intput0 we assume the other side is dead and we try to re-establish contact
  • we expect clients to connect to socket0
  • once a client has connected to socket0 a call to accept is made (socket1), a client gets one minute to make a request before socket1 is closed down

This sort of thing isn't too difficult test, by using LD_PRELOAD and "hiding" the involved system functions (poll, gettimeofday) one can (somewhat) easily control passage of time and trigger timeouts.

Moving to Qt I was planning on wrapping the filedescriptors in instances of QSocketNotifier and connect the activated(int) signal to a suitable slot. That basically provides a Qt-flavoured poll -- except there are no timeouts!

For timeouts I'm looking at coupling a QTimer to each QSocketNotifier. My experimentation shows this to work, and be rather elegant. However, it's not pleasurable to test. I can't really have unit tests that sit and wait 5 minutes for a timeout.

How do I go about making the timeouts in this solution testable?

(Or maybe I'm just thinking about this the wrong way, and should do something completely different.)

Aucun commentaire:

Enregistrer un commentaire