È possibile scrivere il proprio modulo
con il blackjack e le prostitute
per prendere in giro gettimeofday. Con alcune modifiche di Test :: MockTime ho scritto:
#!/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 {
my ($time1, $time2) = @_;
if (! defined $time1 || ! defined $time2) {
croak('Incorrect usage');
}
@fixed = ($time1, $time2);
}
sub throttle {
my $self = shift @_;
return $accel unless @_;
my $new = shift @_;
$new or croak('Can not set throttle to zero');
$accel = $new;
}
sub restore {
@fixed =();
}
1;
penso che abbia un sacco di bug e functionallity incompleta, lavoro in questa direzione
Vuoi qualcosa come 'sub set_fixed_time_of_day'? – Suic
Stavo pensando a quello che voglio. Voglio essere in grado di fermare il tempo nel mio script di test. Dopo aver eseguito 'my $ ts = CORE :: time(); set_fixed_time ($ ts); '(o qualcosa del genere)' time() 'e' Time :: HiRes :: gettimeofday() 'restituiranno gli stessi valori fino a quando eseguirò esplicitamente' $ ts ++; set_fixed_time ($ ts); ' – bessarabov