| 1 |
package Test2::Harness::Event; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/confess/; |
| 8 |
use Time::HiRes qw/time/; |
| 9 |
use Test2::Harness::Util::JSON qw/encode_json/; |
| 10 |
|
| 11 |
use Importer 'Test2::Util::Facets2Legacy' => ':ALL'; |
| 12 |
|
| 13 |
BEGIN { |
| 14 |
require Test2::Event; |
| 15 |
our @ISA = ('Test2::Event'); |
| 16 |
|
| 17 |
# Currently the base class for events does not have init(), that may change |
| 18 |
if (Test2::Event->can('init')) { |
| 19 |
*INIT_EVENT = sub() { 1 } |
| 20 |
} |
| 21 |
else { |
| 22 |
*INIT_EVENT = sub() { 0 } |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
use Test2::Harness::Util::HashBase qw{ |
| 27 |
<facet_data |
| 28 |
<stream_id |
| 29 |
<event_id |
| 30 |
<run_id |
| 31 |
<job_id |
| 32 |
<job_try |
| 33 |
<stamp |
| 34 |
+json |
| 35 |
processed |
| 36 |
}; |
| 37 |
|
| 38 |
sub trace { $_[0]->{+FACET_DATA}->{trace} } |
| 39 |
sub set_trace { confess "'trace' is a read only attribute" } |
| 40 |
|
| 41 |
sub init { |
| 42 |
my $self = shift; |
| 43 |
|
| 44 |
$self->Test2::Event::init() if INIT_EVENT; |
| 45 |
|
| 46 |
my $data = $self->{+FACET_DATA} || confess "'facet_data' is a required attribute"; |
| 47 |
|
| 48 |
for my $field (RUN_ID(), JOB_ID(), JOB_TRY(), EVENT_ID()) { |
| 49 |
my $v1 = $self->{$field}; |
| 50 |
my $v2 = $data->{harness}->{$field}; |
| 51 |
|
| 52 |
my $d1 = defined($v1); |
| 53 |
my $d2 = defined($v2); |
| 54 |
|
| 55 |
confess "'$field' is a required attribute" |
| 56 |
unless $d1 || $d2 || ($field eq +JOB_TRY && !$self->{+JOB_ID}); |
| 57 |
|
| 58 |
confess "'$field' has different values between attribute and facet data" |
| 59 |
if $d1 && $d2 && $v1 ne $v2; |
| 60 |
|
| 61 |
$self->{$field} = $data->{harness}->{$field} = $v1 // $v2; |
| 62 |
} |
| 63 |
|
| 64 |
delete $data->{facet_data}; |
| 65 |
|
| 66 |
# Original trace wins. |
| 67 |
if (my $trace = delete $self->{+TRACE}) { |
| 68 |
$self->{+FACET_DATA}->{trace} //= $trace; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
sub as_json { $_[0]->{+JSON} //= encode_json($_[0]) } |
| 73 |
|
| 74 |
sub TO_JSON { |
| 75 |
my $out = {%{$_[0]}}; |
| 76 |
|
| 77 |
$out->{+FACET_DATA} = { %{$out->{+FACET_DATA}} }; |
| 78 |
delete $out->{+FACET_DATA}->{harness_job_watcher}; |
| 79 |
delete $out->{+FACET_DATA}->{harness}->{closed_by}; |
| 80 |
delete $out->{+JSON}; |
| 81 |
delete $out->{+PROCESSED}; |
| 82 |
|
| 83 |
return $out; |
| 84 |
} |
| 85 |
|
| 86 |
1; |
| 87 |
|
| 88 |
__END__ |
| 89 |
|
| 90 |
=pod |
| 91 |
|
| 92 |
=encoding UTF-8 |
| 93 |
|
| 94 |
=head1 NAME |
| 95 |
|
| 96 |
Test2::Harness::Event - Subclass of Test2::Event used by Test2::Harness under |
| 97 |
the hood. |
| 98 |
|
| 99 |
=head1 DESCRIPTION |
| 100 |
|
| 101 |
Test2 tests produce a sequence of events objects L<Test2::Event>. This is a |
| 102 |
subclass of those events for use in L<Test2::Harness>. Event non-test tests |
| 103 |
which produce TAP output will have the output parsed into these types of |
| 104 |
events. |
| 105 |
|
| 106 |
=head1 SYNOPSIS |
| 107 |
|
| 108 |
In normal usage you will never need to create one of these events yourself. This |
| 109 |
documentation assumes you are operating on an existing event C<$event> that the |
| 110 |
harness exposed to you via a plugin or similar. |
| 111 |
|
| 112 |
my $facet_data = $event->facet_data; |
| 113 |
my $run_id = $event->run_id; |
| 114 |
my $job_id = $event->job_id; |
| 115 |
my $job_try = $event->job_try; |
| 116 |
my $event_id = $event->event_id; |
| 117 |
|
| 118 |
=head1 METHODS |
| 119 |
|
| 120 |
See L<Test2::Event> for methods provided by the base class. |
| 121 |
|
| 122 |
=over 4 |
| 123 |
|
| 124 |
=item $hashref = $event->TO_JSON |
| 125 |
|
| 126 |
Used for json serialization. |
| 127 |
|
| 128 |
=item $json_string = $event->as_json |
| 129 |
|
| 130 |
This will return a json representation of the event. Note that this is a lossy |
| 131 |
conversion with some harness specific state removed by design. This may even be |
| 132 |
a cached copy of the json string that was decoded to produce the original |
| 133 |
object. If the string was not cached before it will be cached for all future |
| 134 |
calls ignoring any state change to the event. |
| 135 |
|
| 136 |
The lossy/cached conversion is intended so that events get passed through the |
| 137 |
harness pipeline without modifications from one step translating to another. If |
| 138 |
you need something extra to go through you need to either replace the event or |
| 139 |
create an additional one. |
| 140 |
|
| 141 |
=item $string = $event->event_id |
| 142 |
|
| 143 |
Usually a UUID, but not always! |
| 144 |
|
| 145 |
=item i$hashref = $event->facet_data |
| 146 |
|
| 147 |
Get the event facet data, this is the meat of the event that hold all the |
| 148 |
state. |
| 149 |
|
| 150 |
=item $string = $event->job_id |
| 151 |
|
| 152 |
Usually a UUID, but not always! |
| 153 |
|
| 154 |
=item $int = $event->job_try |
| 155 |
|
| 156 |
Integer, 0 or greater. Some jobs are run additional times if they fail, this |
| 157 |
says which attempt the event is for. The counter starts at 0. |
| 158 |
|
| 159 |
=item $bool = $event->processed |
| 160 |
|
| 161 |
This will be true if the event has been process by the harness. Note that this |
| 162 |
attribute is not serialized by C<TO_JSON> or C<as_json>. |
| 163 |
|
| 164 |
=item $string = $event->run_id |
| 165 |
|
| 166 |
The run id. This is usually a UUID, but not always! |
| 167 |
|
| 168 |
=item $ts = $event->stamp |
| 169 |
|
| 170 |
A unix timestamp for when the event was created. |
| 171 |
|
| 172 |
=item $id = $event->stream_id |
| 173 |
|
| 174 |
This is an implementation detail of L<Test2::Formatter::Stream>, do not rely on |
| 175 |
it. This is used to prevent parsing errors when stream output is nested in |
| 176 |
other stream output, which can happen if you are writing tests for the stream |
| 177 |
formatter itself. |
| 178 |
|
| 179 |
=item $trace = $event->trace |
| 180 |
|
| 181 |
This is a shortcut for C<< $event->facet_data->{trace} >>. The trace data is |
| 182 |
essential and used everywhere. |
| 183 |
|
| 184 |
=back |
| 185 |
|
| 186 |
=head1 SOURCE |
| 187 |
|
| 188 |
The source code repository for Test2-Harness can be found at |
| 189 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 190 |
|
| 191 |
=head1 MAINTAINERS |
| 192 |
|
| 193 |
=over 4 |
| 194 |
|
| 195 |
=item Chad Granum E<lt>[email protected]<gt> |
| 196 |
|
| 197 |
=back |
| 198 |
|
| 199 |
=head1 AUTHORS |
| 200 |
|
| 201 |
=over 4 |
| 202 |
|
| 203 |
=item Chad Granum E<lt>[email protected]<gt> |
| 204 |
|
| 205 |
=back |
| 206 |
|
| 207 |
=head1 COPYRIGHT |
| 208 |
|
| 209 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 210 |
|
| 211 |
This program is free software; you can redistribute it and/or |
| 212 |
modify it under the same terms as Perl itself. |
| 213 |
|
| 214 |
See F<http://dev.perl.org/licenses/> |
| 215 |
|
| 216 |
=cut |