powerman has asked for the wisdom of the Perl Monks concerning the following question:

In my module Cache::Sliding there is the test cache.t which suffer from race condition - you can see examples of such race on CPAN Testers page (just check any "failed" item).

Question is, how to implement test for this module without race conditions? In test I should check is item was expired/not expired from cache after X seconds. But I can't guarantee when my code actually will be executed - depending on machine load instead of checking cache after N seconds defined in test code actual operation may be done in N+M seconds. And I don't see how to work around this. :(

Update: A friend of mine said it may have sense to check how this issue solved in other modules, like EV itself. I've checked it, and, know what - it's very funny: EV::timer test.

  • Comment on How to write time-related test without race conditions?

Replies are listed 'Best First'.
Re: How to write time-related test without race conditions?
by Anonymous Monk on May 14, 2012 at 19:53 UTC
      Because this module designed to be used in event-based applications.

        Hmm, so is EV::timer blocking like sleep/select? Does it enforce order of timers?

        Maybe you can check if timer went into overtime (M) and do Test::More::skip() instead of is(), or simply rewrite your is() check to notice overtime?