Skip to content

Instantly share code, notes, and snippets.

@preaction
Created June 9, 2014 20:07
Show Gist options
  • Save preaction/c7c8f659da8b4b515fa9 to your computer and use it in GitHub Desktop.
Save preaction/c7c8f659da8b4b515fa9 to your computer and use it in GitHub Desktop.
Test that AnyEvent timer events happen when advertised
use Test::MockTime qw( set_absolute_time );
# Use the Pure-Perl loop so we can do janky things with time
use AnyEvent::Loop;
# set_absolute_time will update what time AnyEvent thinks it is
# Then you can run AnyEvent::Loop::one_event to run the event loop
# until something happens.
#
# AnyEvent::Loop::one_event will call _update_clock to get the current time. If
# an event is ready to be processed, it will be. If it is not ready to be
# processed, the loop will sleep (using select) until an IO event happens
# (which it won't), or until a timer is ready (it will not actually call the
# timer, you have to run one_event again for that to happen).
#
# All this is to say, choose your current time very carefully so that you don't
# end up waiting around all day...
#
# This thing absolutely relies on the internal operation of AnyEvent::Loop,
# so if that changes, I can't guarantee this will work. Building a proper
# AnyEvent::Impl::Test pure-Perl event loop might be a better long-term
# solution...
*AnyEvent::Loop::_update_clock = sub {
# time() is overridden by Test::MockTime
$AnyEvent::Loop::NOW = $AnyEvent::Loop::MNOW = time;
};
use AnyEvent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment