Skip to content

Commit

Permalink
Add an option to turn off event storage.
Browse files Browse the repository at this point in the history
Also test that things work with event storage off.  Not terribly well tested right now.

For #198
  • Loading branch information
schwern committed May 25, 2012
1 parent 92d3823 commit af577ec
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
50 changes: 35 additions & 15 deletions lib/TB2/History.pm
Expand Up @@ -62,6 +62,28 @@ It is a L<TB2::EventHandler>.
Creates a new, unique History object.
new() takes the following options.
=head3 store_events
If true, $history will keep a complete record of all test events
accessable via L<events> and L<results>. This will cause memory usage
to grow over the life of the test.
If false, $history will discard events and only keep a summary of
events. L<events> and L<results> will throw an exception if called.
Defaults to true (which will change in a moment).
=cut

has store_events =>
is => 'ro',
isa => 'Bool',
default => 1
;


=head2 Misc
=head3 object_id
Expand All @@ -80,7 +102,6 @@ Unless otherwise stated, these are all accessor methods of the form:
my $value = $history->method; # get
$history->method($value); # set
=head2 Events
=head3 events
Expand All @@ -92,7 +113,7 @@ An array ref of all events seen.
=cut

sub event_storage_class {
return "TB2::History::EventStorage";
return $_[0]->store_events ? "TB2::History::EventStorage" : "TB2::History::NoEventStorage";
}

has event_storage =>
Expand Down Expand Up @@ -123,23 +144,22 @@ Get the count of events that have been seen.
=cut

sub event_count {
my $self = shift;
my $events = $self->events;
return scalar @$events;
}
has event_count =>
is => 'rw',
isa => 'TB2::Positive_Int',
default => 0;

sub result_count {
my $self = shift;
my $results = $self->results;
return scalar @$results;
}
has result_count =>
is => 'rw',
isa => 'TB2::Positive_Int',
default => 0;

sub handle_event {
my $self = shift;
my $event = shift;

$self->event_storage->events_push($event);
$self->event_count( $self->event_count + 1 );

return;
}
Expand Down Expand Up @@ -237,12 +257,12 @@ sub handle_result {
my $self = shift;
my $result = shift;

$self->result_count( $self->result_count + 1 );
$self->counter( $self->counter + 1 );

$self->event_storage->events_push($result);

$self->_update_statistics($result);

$self->handle_event($result);

return;
}

Expand Down
15 changes: 15 additions & 0 deletions t/History/History.t
Expand Up @@ -61,5 +61,20 @@ my $Fail = TB2::Result->new_result(
isnt $history1->object_id, $history2->object_id, "history object_ids are unique";
}

note "Turn off event storage";
{
my $history = $CLASS->new(
store_events => 0
);

$history->accept_event( $Pass ) for 1..3;
is $history->result_count, 3;
is $history->event_count, 3;

ok !eval { $history->events; 1 };
ok !eval { $history->results; 1 };

ok !eval { $history->store_events(1) }, "can't turn on storage for an existing object";
}

done_testing;

0 comments on commit af577ec

Please sign in to comment.