Skip to content

Commit

Permalink
Allow overriding clock so tests do not sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Axel 'fREW' Schmidt committed Nov 9, 2015
1 parent 1bcdc1d commit 19e94f6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
17 changes: 12 additions & 5 deletions lib/DBIx/Class/QueryLog.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ has passthrough => (
default => 0
);

has __time => (
is => 'ro',
default => sub { \&Time::HiRes::time },
);

sub _time { shift->__time->() }

before 'add_to_log' => sub {
my ($self, $thing) = @_;

Expand Down Expand Up @@ -173,7 +180,7 @@ sub txn_begin {
$self->next::method(@_) if $self->passthrough;
$self->current_transaction(
$self->transaction_class()->new({
start_time => Time::HiRes::time
start_time => $self->_time,
})
);
}
Expand All @@ -190,7 +197,7 @@ sub txn_commit {
$self->next::method(@_) if $self->passthrough;
if(defined($self->current_transaction)) {
my $txn = $self->current_transaction;
$txn->end_time(Time::HiRes::time);
$txn->end_time($self->_time);
$txn->committed(1);
$txn->rolledback(0);
$self->add_to_log($txn);
Expand All @@ -212,7 +219,7 @@ sub txn_rollback {
$self->next::method(@_) if $self->passthrough;
if(defined($self->current_transaction)) {
my $txn = $self->current_transaction;
$txn->end_time(Time::HiRes::time);
$txn->end_time($self->_time);
$txn->committed(0);
$txn->rolledback(1);
$self->add_to_log($txn);
Expand All @@ -236,7 +243,7 @@ sub query_start {
$self->next::method($sql, @params) if $self->passthrough;
$self->current_query(
$self->query_class()->new({
start_time => Time::HiRes::time,
start_time => $self->_time,
sql => $sql,
params => \@params,
})
Expand All @@ -255,7 +262,7 @@ sub query_end {
$self->next::method(@_) if $self->passthrough;
if(defined($self->current_query)) {
my $q = $self->current_query;
$q->end_time(Time::HiRes::time);
$q->end_time($self->_time);
$q->bucket($self->bucket);
if(defined($self->current_transaction)) {
$self->current_transaction->add_to_queries($q);
Expand Down
10 changes: 7 additions & 3 deletions t/02-analyzer.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ BEGIN {
}
require_ok('DBIx::Class::QueryLog::Analyzer');

my $ql = DBIx::Class::QueryLog->new;
my $time = 0;

my $ql = DBIx::Class::QueryLog->new(
__time => sub { $time },
);
ok($ql->isa('DBIx::Class::QueryLog'), 'new');

$ql->query_start('SELECT * from foo');
Expand All @@ -22,7 +26,7 @@ $ql->query_start('SELECT * from foo');
$ql->query_end('SELECT * from foo');

$ql->query_start('SELECT * from bar');
sleep(1);
$time += 1;
$ql->query_end('SELECT * from bar');

$ql->txn_commit;
Expand All @@ -47,4 +51,4 @@ cmp_ok($analyzed->{$keys[1]}->{'count'}, '==', 2, '2 executions');

ok($analyzed->{$keys[0]}->{'time_elapsed'}, 'Total time');
cmp_ok(scalar(@{$analyzed->{$keys[0]}->{'queries'}}), '==', 1, '1 stored queries');
cmp_ok(scalar(@{$analyzed->{$keys[1]}->{'queries'}}), '==', 2, '2 stored queries');
cmp_ok(scalar(@{$analyzed->{$keys[1]}->{'queries'}}), '==', 2, '2 stored queries');
10 changes: 7 additions & 3 deletions t/04-fastslow.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ use DBIx::Class::QueryLog::Analyzer;
use DBIx::Class::QueryLog::Query;
use DBIx::Class::QueryLog::Transaction;

my $ql = DBIx::Class::QueryLog->new;
my $time = 0;

my $ql = DBIx::Class::QueryLog->new(
__time => sub { $time },
);
$ql->query_start('SELECT * from foo', 'fast');
$ql->query_end('SELECT * from foo', 'fast');

$ql->query_start('SELECT * from foo2', 'fast');
$ql->query_end('SELECT * from foo2', 'fast');

$ql->query_start('SELECT * from foo', 'slow');
sleep(3);
$time += 3;
$ql->query_end('SELECT * from foo', 'slow');

$ql->txn_begin;
$ql->query_start('SELECT * from foo', 'medium');
sleep(2);
$time += 2;
$ql->query_end('SELECT * from foo', 'medium');
$ql->txn_commit;

Expand Down

0 comments on commit 19e94f6

Please sign in to comment.