testing - How to mock Perl module Time::HiRes -
there great perl module time::hires. heavily use in library , want write tests. have found 2 cpan modules mocks perl time()
function, both of them don't support time::hires:
how can mock time::hires sub gettimeofday()
?
ps want fix tests module time::eta. use ugly hack sleep
"mock", sometimes works , not.
you can write own module with blackjack , hookers mock gettimeofday. modifications of test::mocktime wrote:
#!/usr/bin/perl package mymocktime; use strict; use warnings; use exporter qw( import ); use time::hires (); use carp; our @fixed = (); our $accel = 1; our $otime = time::hires::gettimeofday; our @export_ok = qw( set_fixed_time_of_day gettimeofday restore throttle ); sub gettimeofday() { if ( @fixed ) { return wantarray ? @fixed : "$fixed[0].$fixed[1]"; } else { return $otime + ( ( time::hires::gettimeofday - $otime ) * $accel ); } } sub set_fixed_time_of_day { ( $time1, $time2 ) = @_; if ( ! defined $time1 || ! defined $time2 ) { croak('incorrect usage'); } @fixed = ( $time1, $time2 ); } sub throttle { $self = shift @_; return $accel unless @_; $new = shift @_; $new or croak('can not set throttle zero'); $accel = $new; } sub restore { @fixed = (); } 1;
i think has lot of bugs , incomplete functionallity, work in direction
Comments
Post a Comment