| 1 |
package Test2::Formatter::Stream; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/croak confess/; |
| 8 |
use Time::HiRes qw/time/; |
| 9 |
use IO::Handle; |
| 10 |
use File::Spec(); |
| 11 |
use List::Util qw/first/; |
| 12 |
|
| 13 |
use Test2::Harness::Util::UUID qw/gen_uuid/; |
| 14 |
use Test2::Harness::Util::JSON qw/JSON JSON_IS_XS/; |
| 15 |
use Test2::Harness::Util qw/hub_truth apply_encoding/; |
| 16 |
|
| 17 |
use Test2::Util qw/get_tid ipc_separator/; |
| 18 |
|
| 19 |
use parent qw/Test2::Formatter/; |
| 20 |
use Test2::Util::HashBase qw/-io _encoding _no_header _no_numbers _no_diag -stream_id -tb -tb_handles -dir -_pid -_tid -_fh <job_id -ugids/; |
| 21 |
|
| 22 |
BEGIN { |
| 23 |
my $J = JSON->new; |
| 24 |
$J->indent(0); |
| 25 |
$J->convert_blessed(1); |
| 26 |
$J->allow_blessed(1); |
| 27 |
$J->utf8(1); |
| 28 |
|
| 29 |
require constant; |
| 30 |
constant->import(ENCODER => $J); |
| 31 |
|
| 32 |
if (JSON_IS_XS) { |
| 33 |
require JSON::PP; |
| 34 |
my $JPP = JSON::PP->new; |
| 35 |
$JPP->indent(0); |
| 36 |
$JPP->convert_blessed(1); |
| 37 |
$JPP->allow_blessed(1); |
| 38 |
$JPP->utf8(1); |
| 39 |
|
| 40 |
constant->import(ENCODER_PP => $JPP); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
my ($ROOT_TID, $ROOT_PID, $ROOT_DIR, $ROOT_JOB_ID, $ROOT_UGIDS); |
| 45 |
sub import { |
| 46 |
my $class = shift; |
| 47 |
my %params = @_; |
| 48 |
|
| 49 |
confess "$class no longer accept the 'file' argument, it now takes a 'dir' argument" |
| 50 |
if exists $params{file}; |
| 51 |
|
| 52 |
$class->SUPER::import(); |
| 53 |
|
| 54 |
$ROOT_PID = $$; |
| 55 |
$ROOT_TID = get_tid(); |
| 56 |
$ROOT_DIR = $params{dir} if $params{dir}; |
| 57 |
$ROOT_JOB_ID = $params{job_id} if $params{job_id}; |
| 58 |
$ROOT_UGIDS = [$<, $>, $(, $)]; |
| 59 |
|
| 60 |
if ($ROOT_DIR && ! -d $ROOT_DIR) { |
| 61 |
mkdir($ROOT_DIR) or die "Could not make root dir: $!"; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
sub hide_buffered { 0 } |
| 66 |
|
| 67 |
sub fh { |
| 68 |
my $self = shift; |
| 69 |
|
| 70 |
my $dir = $self->{+DIR} or return undef; |
| 71 |
|
| 72 |
my $pid = $self->{+_PID}; |
| 73 |
my $tid = $self->{+_TID}; |
| 74 |
|
| 75 |
if ($pid && $pid != $$) { |
| 76 |
delete $self->{+_PID}; |
| 77 |
delete $self->{+_FH}; |
| 78 |
} |
| 79 |
|
| 80 |
if ($tid && $tid != get_tid()) { |
| 81 |
delete $self->{+_TID}; |
| 82 |
delete $self->{+_FH}; |
| 83 |
} |
| 84 |
|
| 85 |
return $self->{+_FH} if $self->{+_FH}; |
| 86 |
|
| 87 |
$self->{+STREAM_ID} = 1; |
| 88 |
|
| 89 |
$pid = $self->{+_PID} = $$; |
| 90 |
$tid = $self->{+_TID} = get_tid(); |
| 91 |
|
| 92 |
my $file = File::Spec->catfile($dir, join(ipc_separator() => 'events', $pid, $tid) . ".jsonl"); |
| 93 |
|
| 94 |
my @now = ($<, $>, $(, $)); |
| 95 |
local ($<, $>, $(, $)) = @{$self->{+UGIDS}} if $self->{+UGIDS} && first { $self->{+UGIDS}->[$_] ne $now[$_] } 0 .. $#now; |
| 96 |
|
| 97 |
mkdir($dir) or die "Could not make dir '$dir': $!" unless -d $dir; |
| 98 |
confess "File '$file' already exists!" if -f $file; |
| 99 |
open(my $fh, '>', $file) or die "Could not open file: $file"; |
| 100 |
$fh->autoflush(1); |
| 101 |
|
| 102 |
# Do not apply encoding to the UTF8 output, we let the utf8 formatter |
| 103 |
# handle that. This means do not apply encoding to $self->{+_FH}. |
| 104 |
|
| 105 |
return $self->{+_FH} = $fh; |
| 106 |
} |
| 107 |
|
| 108 |
sub init { |
| 109 |
my $self = shift; |
| 110 |
|
| 111 |
$self->{+STREAM_ID} = 1; |
| 112 |
$self->{+UGIDS} //= [$<, $>, $(, $)]; |
| 113 |
|
| 114 |
# To create necessary directories as soon as possible |
| 115 |
$self->fh(); |
| 116 |
|
| 117 |
for (@{$self->{+IO}}) { |
| 118 |
$_->autoflush(1); |
| 119 |
} |
| 120 |
|
| 121 |
STDOUT->autoflush(1); |
| 122 |
STDERR->autoflush(1); |
| 123 |
|
| 124 |
if ($INC{'Test2/API.pm'}) { |
| 125 |
Test2::API::test2_stdout()->autoflush(1); |
| 126 |
Test2::API::test2_stderr()->autoflush(1); |
| 127 |
} |
| 128 |
|
| 129 |
if ($self->{check_tb}) { |
| 130 |
require Test::Builder::Formatter; |
| 131 |
$self->{+TB} = Test::Builder::Formatter->new(); |
| 132 |
$self->{+TB_HANDLES} = [@{$self->{+TB}->handles}]; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
sub new_root { |
| 137 |
my $class = shift; |
| 138 |
my %params = @_; |
| 139 |
|
| 140 |
$ROOT_PID = $$ unless defined $ROOT_PID; |
| 141 |
$ROOT_TID = get_tid() unless defined $ROOT_TID; |
| 142 |
|
| 143 |
confess "new_root called from child process!" |
| 144 |
if $ROOT_PID != $$; |
| 145 |
|
| 146 |
confess "new_root called from child thread!" |
| 147 |
if $ROOT_TID != get_tid(); |
| 148 |
|
| 149 |
require Test2::API; |
| 150 |
my $io = $params{+IO} = [Test2::API::test2_stdout(), Test2::API::test2_stderr()]; |
| 151 |
$_->autoflush(1) for @$io; |
| 152 |
|
| 153 |
confess "T2_STREAM_FILE is no longer used, see T2_STREAM_DIR" |
| 154 |
if exists $ENV{T2_STREAM_FILE}; |
| 155 |
|
| 156 |
$params{+DIR} ||= $ENV{T2_STREAM_DIR} || $ROOT_DIR; |
| 157 |
$params{+JOB_ID} ||= $ENV{T2_STREAM_JOB_ID} || $ROOT_JOB_ID || 1; |
| 158 |
|
| 159 |
# DO NOT REOPEN THEM! |
| 160 |
delete $ENV{T2_FORMATTER} if $ENV{T2_FORMATTER} && $ENV{T2_FORMATTER} eq 'Stream'; |
| 161 |
delete $ENV{T2_STREAM_DIR}; |
| 162 |
delete $ENV{T2_STREAM_JOB_ID}; |
| 163 |
$ROOT_DIR = undef; |
| 164 |
|
| 165 |
$params{check_tb} = 1 if $INC{'Test/Builder.pm'}; |
| 166 |
|
| 167 |
$params{+UGIDS} = $ROOT_UGIDS if $ROOT_UGIDS; |
| 168 |
|
| 169 |
return $class->new(%params); |
| 170 |
} |
| 171 |
|
| 172 |
sub record { |
| 173 |
my $self = shift; |
| 174 |
my ($facets, $num) = @_; |
| 175 |
|
| 176 |
my $stamp = time; |
| 177 |
my $times = [times]; |
| 178 |
|
| 179 |
my @sync = @{$self->{+IO}}; |
| 180 |
my $leader = 0; |
| 181 |
|
| 182 |
my $fh = $self->fh; |
| 183 |
unless($fh) { |
| 184 |
$leader = 1; |
| 185 |
$fh = shift @sync; |
| 186 |
} |
| 187 |
|
| 188 |
if ($facets->{control}->{halt}) { |
| 189 |
my $reason = $facets->{control}->{details} || ""; |
| 190 |
|
| 191 |
if ($leader) { |
| 192 |
print $fh "\nBail out! $reason\n"; |
| 193 |
} |
| 194 |
else { |
| 195 |
open(my $bh, '>', File::Spec->catfile($self->{+DIR}, 'bail')) or die "Could not create bail file: $!"; |
| 196 |
print $bh $reason; |
| 197 |
close($bh); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
my $tid = get_tid(); |
| 202 |
my $id = $self->{+STREAM_ID}++; |
| 203 |
|
| 204 |
my $json; |
| 205 |
{ |
| 206 |
no warnings 'once'; |
| 207 |
local *UNIVERSAL::TO_JSON = sub { "$_[0]" }; |
| 208 |
|
| 209 |
my $event_id = $facets->{about}->{uuid} ||= gen_uuid(); |
| 210 |
|
| 211 |
if (JSON_IS_XS) { |
| 212 |
for my $encoder (ENCODER, ENCODER_PP) { |
| 213 |
local $@; |
| 214 |
my $ok = eval { |
| 215 |
$json = $encoder->encode( |
| 216 |
{ |
| 217 |
stamp => $stamp, |
| 218 |
times => $times, |
| 219 |
stream_id => $id, |
| 220 |
tid => $tid, |
| 221 |
pid => $$, |
| 222 |
event_id => $event_id, |
| 223 |
facet_data => $facets, |
| 224 |
assert_count => $self->{+_NO_NUMBERS} ? undef : $num, |
| 225 |
} |
| 226 |
); |
| 227 |
1; |
| 228 |
}; |
| 229 |
my $err = $@; |
| 230 |
last if $ok; |
| 231 |
|
| 232 |
# Intercept bug in JSON::XS so we can fall back to JSON::PP |
| 233 |
next if $encoder eq ENCODER && $err =~ m/Modification of a read-only value attempted/; |
| 234 |
|
| 235 |
# Different error, time to die. |
| 236 |
die $err; |
| 237 |
} |
| 238 |
} |
| 239 |
else { |
| 240 |
$json = ENCODER->encode( |
| 241 |
{ |
| 242 |
stamp => $stamp, |
| 243 |
times => $times, |
| 244 |
stream_id => $id, |
| 245 |
tid => $tid, |
| 246 |
pid => $$, |
| 247 |
event_id => $event_id, |
| 248 |
facet_data => $facets, |
| 249 |
assert_count => $self->{+_NO_NUMBERS} ? undef : $num, |
| 250 |
} |
| 251 |
); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
# Local is expensive! Only do it if we really need to. |
| 256 |
local($\, $,) = (undef, '') if $\ || $,; |
| 257 |
|
| 258 |
my $job_id = $self->{+JOB_ID}; |
| 259 |
|
| 260 |
print $fh $leader ? ("T2-HARNESS-$job_id-EVENT: ", $json, "\n") : ($json, "\n"); |
| 261 |
|
| 262 |
print $_ "T2-HARNESS-$job_id-ESYNC: ", join(ipc_separator() => $$, $tid, $id) . "\n" for @sync; |
| 263 |
} |
| 264 |
|
| 265 |
sub encoding { |
| 266 |
my $self = shift; |
| 267 |
|
| 268 |
if (@_) { |
| 269 |
my ($enc) = @_; |
| 270 |
$self->record({control => {encoding => $enc}}); |
| 271 |
$self->_set_encoding($enc); |
| 272 |
$self->{+TB}->encoding($enc) if $self->{+TB}; |
| 273 |
} |
| 274 |
|
| 275 |
return $self->{+_ENCODING}; |
| 276 |
} |
| 277 |
|
| 278 |
sub _set_encoding { |
| 279 |
my $self = shift; |
| 280 |
|
| 281 |
if (@_) { |
| 282 |
my ($enc) = @_; |
| 283 |
|
| 284 |
# Do not apply encoding to the UTF8 output, we let the utf8 formatter |
| 285 |
# handle that. This means do not apply encoding to $self->{+_FH}. |
| 286 |
|
| 287 |
apply_encoding(\*STDOUT, $enc); |
| 288 |
apply_encoding(\*STDERR, $enc); |
| 289 |
|
| 290 |
my $job_id = $self->{+JOB_ID}; |
| 291 |
for my $fh (@{$self->{+IO}}) { |
| 292 |
print $fh "T2-HARNESS-$job_id-ENCODING: $enc\n"; |
| 293 |
apply_encoding($fh, $enc); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
return $self->{+_ENCODING}; |
| 298 |
} |
| 299 |
|
| 300 |
if ($^C) { |
| 301 |
no warnings 'redefine'; |
| 302 |
*write = sub { }; |
| 303 |
} |
| 304 |
|
| 305 |
sub write { |
| 306 |
my ($self, $e, $num, $f) = @_; |
| 307 |
$f ||= $e->facet_data; |
| 308 |
|
| 309 |
$self->_set_encoding($f->{control}->{encoding}) if $f->{control}->{encoding}; |
| 310 |
|
| 311 |
# Hide these if we must, but do not remove them for good. |
| 312 |
local $f->{info} if $self->{+_NO_DIAG}; |
| 313 |
local $f->{plan} if $self->{+_NO_HEADER}; |
| 314 |
|
| 315 |
my $tb_only = 0; |
| 316 |
if ($self->{+TB}) { |
| 317 |
$tb_only ||= $self->{+TB_HANDLES}->[0] != $self->{+TB}->{handles}->[0]; |
| 318 |
$tb_only ||= $self->{+TB_HANDLES}->[1] != $self->{+TB}->{handles}->[1]; |
| 319 |
|
| 320 |
my $todo_match = $self->{+TB_HANDLES}->[0] == $self->{+TB}->{handles}->[2] |
| 321 |
|| $self->{+TB_HANDLES}->[1] == $self->{+TB}->{handles}->[2]; |
| 322 |
|
| 323 |
$tb_only ||= !$todo_match; |
| 324 |
|
| 325 |
if ($tb_only) { |
| 326 |
my $buffered = hub_truth($f)->{buffered}; |
| 327 |
$self->{+TB}->write($e, $num, $f) if $self->{+TB} && !$buffered; |
| 328 |
return; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
$self->record($f, $num); |
| 333 |
} |
| 334 |
|
| 335 |
sub no_header { $_[0]->{+_NO_HEADER} } |
| 336 |
sub no_diag { $_[0]->{+_NO_DIAG} } |
| 337 |
sub no_numbers { $_[0]->{+_NO_NUMBERS} } |
| 338 |
|
| 339 |
sub handles { |
| 340 |
my $self = shift; |
| 341 |
|
| 342 |
return $self->{+TB}->handles if $self->{+TB}; |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
sub set_no_header { |
| 347 |
my $self = shift; |
| 348 |
($self->{+_NO_HEADER}) = @_; |
| 349 |
$self->{+TB}->set_no_header(@_) if $self->{+TB}; |
| 350 |
$self->{+_NO_HEADER}; |
| 351 |
} |
| 352 |
|
| 353 |
sub set_no_diag { |
| 354 |
my $self = shift; |
| 355 |
($self->{+_NO_DIAG}) = @_; |
| 356 |
$self->{+TB}->set_no_diag(@_) if $self->{+TB}; |
| 357 |
$self->{+_NO_DIAG}; |
| 358 |
} |
| 359 |
|
| 360 |
sub set_no_numbers { |
| 361 |
my $self = shift; |
| 362 |
($self->{+_NO_NUMBERS}) = @_; |
| 363 |
$self->{+TB}->set_no_numbers(@_) if $self->{+TB}; |
| 364 |
$self->{+_NO_NUMBERS}; |
| 365 |
} |
| 366 |
|
| 367 |
sub set_handles { |
| 368 |
my $self = shift; |
| 369 |
return $self->{+TB}->set_handles(@_) if $self->{+TB}; |
| 370 |
return; |
| 371 |
} |
| 372 |
|
| 373 |
sub terminate { |
| 374 |
my $self = shift; |
| 375 |
return $self->SUPER::terminate(@_) unless $self->{+TB}; |
| 376 |
return $self->{+TB}->terminate(@_); |
| 377 |
} |
| 378 |
|
| 379 |
sub finalize { |
| 380 |
my $self = shift; |
| 381 |
return $self->SUPER::finalize(@_) unless $self->{+TB}; |
| 382 |
return $self->{+TB}->finalize(@_); |
| 383 |
} |
| 384 |
|
| 385 |
sub DESTROY {} |
| 386 |
|
| 387 |
our $AUTOLOAD; |
| 388 |
|
| 389 |
sub AUTOLOAD { |
| 390 |
my $this = shift; |
| 391 |
|
| 392 |
my $meth = $AUTOLOAD; |
| 393 |
$meth =~ s/^.*:://g; |
| 394 |
|
| 395 |
my $type = ref($this); |
| 396 |
|
| 397 |
return $this->{+TB}->$meth(@_) |
| 398 |
if $type && $this->{+TB} && $this->{+TB}->can($meth); |
| 399 |
|
| 400 |
$type ||= $this; |
| 401 |
croak qq{Can't locate object method "$meth" via package "$type"}; |
| 402 |
} |
| 403 |
|
| 404 |
sub isa { |
| 405 |
my $in = shift; |
| 406 |
return $in->SUPER::isa(@_) unless ref($in) && $in->{+TB}; |
| 407 |
return $in->SUPER::isa(@_) || $in->{+TB}->isa(@_); |
| 408 |
} |
| 409 |
|
| 410 |
sub can { |
| 411 |
my $in = shift; |
| 412 |
return $in->SUPER::can(@_) unless ref($in) && $in->{+TB}; |
| 413 |
return $in->SUPER::can(@_) || $in->{+TB}->can(@_); |
| 414 |
} |
| 415 |
|
| 416 |
1; |
| 417 |
|
| 418 |
__END__ |
| 419 |
|
| 420 |
=pod |
| 421 |
|
| 422 |
=encoding UTF-8 |
| 423 |
|
| 424 |
=head1 NAME |
| 425 |
|
| 426 |
Test2::Formatter::Stream - Test2 Formatter that directly writes events. |
| 427 |
|
| 428 |
=head1 DESCRIPTION |
| 429 |
|
| 430 |
This formatter writes all test2 events to event files (one per process/thread) |
| 431 |
instead of writing them to STDERR/STDOUT. It will output synchronization |
| 432 |
messages to STDERR/STDOUT every time an event is written. From this data the |
| 433 |
test output can be properly reconstructed in order with STDERR/STDOUT and |
| 434 |
events mostly synced so that they appear in the correct order. |
| 435 |
|
| 436 |
This formatter is not usually useful to humans. This formatter is used by |
| 437 |
L<Test2::Harness> when possible to prevent the loss of data that normally |
| 438 |
occurs when TAP is used. |
| 439 |
|
| 440 |
=head1 SYNOPSIS |
| 441 |
|
| 442 |
If you really want your test to output this: |
| 443 |
|
| 444 |
use Test2::Formatter::Stream; |
| 445 |
use Test2::V0; |
| 446 |
... |
| 447 |
|
| 448 |
Otherwise just use L<App::Yath> without the C<--no-stream> argument and this |
| 449 |
formatter will be used when possible. |
| 450 |
|
| 451 |
=head1 SOURCE |
| 452 |
|
| 453 |
The source code repository for Test2-Harness can be found at |
| 454 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 455 |
|
| 456 |
=head1 MAINTAINERS |
| 457 |
|
| 458 |
=over 4 |
| 459 |
|
| 460 |
=item Chad Granum E<lt>[email protected]<gt> |
| 461 |
|
| 462 |
=back |
| 463 |
|
| 464 |
=head1 AUTHORS |
| 465 |
|
| 466 |
=over 4 |
| 467 |
|
| 468 |
=item Chad Granum E<lt>[email protected]<gt> |
| 469 |
|
| 470 |
=back |
| 471 |
|
| 472 |
=head1 COPYRIGHT |
| 473 |
|
| 474 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 475 |
|
| 476 |
This program is free software; you can redistribute it and/or |
| 477 |
modify it under the same terms as Perl itself. |
| 478 |
|
| 479 |
See F<http://dev.perl.org/licenses/> |
| 480 |
|
| 481 |
=cut |