I'm writing a library with a C (not C++) interface that contains an event loop, call it processEvents
. This should be called in a loop, and invokes user-defined callbacks when something has happened. The "something" in this case is triggered by an RPC response that is received in a different thread, and added to an event queue which is consumed by processEvents
on the main thread.
So from the point of view of the user of my library, the usage looks like this:
function myCallback(void *userData) {
// ...
}
int main() {
setCallback(&myCallback, NULL);
requestCallback();
while (true) {
processEvents(); /* Eventually calls myCallback, but not immediately. */
doSomeOtherStuff();
}
}
Now I want to test, using Google Test and Google Mock, that the callback is indeed called.
I've used MockFunction<void()>
to intercept the actual callback; this is called by a C-style static function that casts the void *userData
to a MockFunction<void()> *
and calls it. This works fine.
The trouble is: the callback isn't necessarily happen on the first call of processEvents
; all I know is that it happens eventually if we keep calling processEvents
in a loop.
So I guess I need something like this:
while (!testing::Mock::AllExpectationsSatisfied() && !timedOut()) {
processEvents();
}
But this fictional AllExpectationsSatisfied
doesn't seem to exist. The closest I can find is VerifyAndClearExpectations
, but it makes the test fail immediately if the expectations aren't met on the first try (and clears them, to boot).
Of course I make this loop run for a full second or so, which would make the test green, but also make it needlessly slow.
Does anyone know a better solution?