Skip to content

Commit

Permalink
a possible hack to workaround libwww-perl#101
Browse files Browse the repository at this point in the history
  • Loading branch information
eserte committed Mar 27, 2021
1 parent 8468a30 commit e045b2c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
37 changes: 37 additions & 0 deletions t/lib/FakeTime.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package FakeTime;

use strict;
use warnings;

require Exporter;
our @ISA = 'Exporter';
our @EXPORT_OK = 'time';

sub import {
my $pkg = shift;
return unless @_;
my $sym = shift;
my $where = 'CORE::GLOBAL';
$pkg->export($where, $sym, @_);
}

sub time () {
if (defined $FakeTime::time) {
#require Carp; Carp::carp("Faked time: $FakeTime::time");
$FakeTime::time;
} else {
CORE::time();
}
}

sub freeze {
$FakeTime::time = CORE::time();
}

sub unfreeze {
undef $FakeTime::time;
}

1;

__END__
5 changes: 5 additions & 0 deletions t/response.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use warnings;
use Test::More;
plan tests => 68;

use lib 't/lib';
use FakeTime 'time'; # must come before the other HTTP::* uses

use HTTP::Date;
use HTTP::Request;
use HTTP::Response;
Expand Down Expand Up @@ -126,6 +129,7 @@ is($r->base, "http://www.sn.no/2/;a=/foo/bar");
is($#warn, 0);
like($warn[0], qr/Undefined argument to parse\(\)/);
}
FakeTime::freeze();
is($r2->code, undef);
is($r2->message, undef);
is($r2->protocol, undef);
Expand All @@ -148,6 +152,7 @@ is($r2->freshness_lifetime(heuristic_expiry => 0), undef);
is($r2->freshness_lifetime(heuristic_expiry => 1), 86400);
ok($r2->is_fresh(time => time));
ok($r2->fresh_until(time => time + 10));
FakeTime::unfreeze();

$r2->client_date(1);
cmp_ok(abs(time - $r2->current_age), '<', 10); # Allow 10s for slow boxes
Expand Down

0 comments on commit e045b2c

Please sign in to comment.