1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
package Test2::Workflow::Runner;
use strict;
use warnings;
our $VERSION = '1.302210';
use Test2::API();
use Test2::Todo();
use Test2::AsyncSubtest();
use Test2::Util qw/get_tid CAN_REALLY_FORK/;
use Scalar::Util qw/blessed/;
use Time::HiRes qw/sleep/;
use List::Util qw/shuffle min/;
use Carp qw/confess/;
use Test2::Util::HashBase qw{
stack no_fork no_threads max slots pid tid rand subtests filter
};
use overload(
'fallback' => 1,
'&{}' => sub {
my $self = shift;
sub {
@_ = ($self);
goto &run;
}
},
);
sub init {
my $self = shift;
$self->{+STACK} = [];
$self->{+SUBTESTS} = [];
$self->{+PID} = $$;
$self->{+TID} = get_tid();
$self->{+NO_FORK} ||= $ENV{T2_WORKFLOW_NO_FORK} || !CAN_REALLY_FORK();
my $can_thread = Test2::AsyncSubtest->CAN_REALLY_THREAD();
my $should_thread = ($ENV{T2_WORKFLOW_USE_THREADS} || $ENV{T2_DO_THREAD_TESTS}) && !$ENV{T2_WORKFLOW_NO_THREADS};
$self->{+NO_THREADS} ||= !($can_thread && $should_thread);
$self->{+RAND} = 1 unless defined $self->{+RAND};
my @max = grep {defined $_} $self->{+MAX}, $ENV{T2_WORKFLOW_ASYNC};
my $max = @max ? min(@max) : 3;
$self->{+MAX} = $max;
$self->{+SLOTS} = [] if $max;
unless(defined($self->{+FILTER})) {
if (my $raw = $ENV{T2_WORKFLOW}) {
my ($file, $line, $name);
if ($raw =~ m/^(.*)\s+(\d+)$/) {
($file, $line) = ($1, $2);
}
elsif($raw =~ m/^(\d+)$/) {
$line = $1;
}
else {
$name = $raw;
}
$self->{+FILTER} = {
file => $file,
line => $line,
name => $name,
};
}
}
if (my $task = delete $self->{task}) {
$self->push_task($task);
}
}
sub is_local {
my $self = shift;
return 0 unless $self->{+PID} == $$;
return 0 unless $self->{+TID} == get_tid();
return 1;
}
sub send_event {
my $self = shift;
my ($type, %params) = @_;
my $class;
if ($type =~ m/\+(.*)$/) {
$class = $1;
}
else {
$class = "Test2::Event::$type";
}
my $hub = Test2::API::test2_stack()->top();
my $e = $class->new(
trace => Test2::Util::Trace->new(
frame => [caller(0)],
buffered => $hub->buffered,
nested => $hub->nested,
hid => $hub->hid,
huuid => $hub->uuid,
#cid => $self->{+CID},
#uuid => $self->{+UUID},
),
%params,
);
$hub->send($e);
}
sub current_subtest {
my $self = shift;
my $stack = $self->{+STACK} or return undef;
for my $state (reverse @$stack) {
next unless $state->{subtest};
return $state->{subtest};
}
return undef;
}
sub run {
my $self = shift;
my $stack = $self->stack;
my $c = 0;
while (@$stack) {
$self->cull;
my $state = $stack->[-1];
my $task = $state->{task};
unless($state->{started}++) {
my $skip = $task->skip;
my $filter;
if (my $f = $self->{+FILTER}) {
my $in_var = grep { $_->{filter_satisfied} } @$stack;
$filter = $task->filter($f) unless $in_var;
$state->{filter_satisfied} = 1 if $filter->{satisfied};
}
$skip ||= $filter->{skip} if $filter;
if ($skip) {
$state->{ended}++;
$self->send_event(
'Skip',
reason => $skip || $filter,
name => $task->name,
pass => 1,
effective_pass => 1,
);
pop @$stack;
next;
}
if ($task->flat) {
my $st = $self->current_subtest;
my $hub = $st ? $st->hub : Test2::API::test2_stack->top;
$state->{todo} = Test2::Todo->new(reason => $task->todo, hub => $hub)
if $task->todo;
$hub->send($_) for @{$task->events};
}
else {
my $st = Test2::AsyncSubtest->new(
name => $task->name,
frame => $task->frame,
);
$state->{subtest} = $st;
$state->{todo} = Test2::Todo->new(reason => $task->todo, hub => $st->hub)
if $task->todo;
for my $e (@{$task->events}) {
my $hub = $st->hub;
$e->trace->{buffered} = $hub->buffered;
$e->trace->{nested} = $hub->nested;
$e->trace->{hid} = $hub->hid;
$e->trace->{huuid} = $hub->uuid;
$hub->send($e);
}
my $slot = $self->isolate($state);
# if we forked/threaded then this state has ended here.
if (defined($slot)) {
push @{$self->{+SUBTESTS}} => [$st, $task] unless $st->finished;
$state->{subtest} = undef;
$state->{ended} = 1;
}
}
}
if ($state->{ended}) {
$state->{todo}->end() if $state->{todo};
$state->{subtest}->stop() if $state->{subtest};
return if $state->{in_thread};
if(my $guard = delete $state->{in_fork}) {
$state->{subtest}->detach;
$guard->dismiss;
exit 0;
}
pop @$stack;
next;
}
if($state->{subtest} && !$state->{subtest_started}++) {
push @{$self->{+SUBTESTS}} => [$state->{subtest}, $task];
$state->{subtest}->start();
}
if ($task->isa('Test2::Workflow::Task::Action')) {
$state->{PID} = $$;
my $ok = eval { $task->code->($self); 1 };
unless ($state->{PID} == $$) {
print STDERR "Task '" . $task->name . "' started in pid $state->{PID}, but ended in pid $$, did you forget to exit after forking?\n";
exit 255;
}
$task->exception($@) unless $ok;
$state->{ended} = 1;
next;
}
if (!$state->{stage} || $state->{stage} eq 'BEFORE') {
$state->{before} = (defined $state->{before}) ? $state->{before} : 0;
if (my $add = $task->before->[$state->{before}++]) {
if ($add->around) {
$state->{PID} = $$;
my $ok = eval { $add->code->($self); 1 };
my $err = $@;
my $complete = $state->{stage} && $state->{stage} eq 'AFTER';
unless ($state->{PID} == $$) {
print STDERR "Task '" . $task->name . "' started in pid $state->{PID}, but ended in pid $$, did you forget to exit after forking?\n";
exit 255;
}
unless($ok && $complete) {
$state->{ended} = 1;
$state->{stage} = 'AFTER';
$task->exception($ok ? "'around' task failed to continue into the workflow chain.\n" : $err);
}
}
else {
$self->push_task($add);
}
}
else {
$state->{stage} = 'VARIANT';
}
}
elsif ($state->{stage} eq 'VARIANT') {
if (my $v = $task->variant) {
$self->push_task($v);
}
$state->{stage} = 'PRIMARY';
}
elsif ($state->{stage} eq 'PRIMARY') {
unless (defined $state->{order}) {
my $rand = defined($task->rand) ? $task->rand : $self->rand;
$state->{order} = [0 .. scalar(@{$task->primary}) - 1];
@{$state->{order}} = shuffle(@{$state->{order}})
if $rand;
}
my $num = shift @{$state->{order}};
if (defined $num) {
$self->push_task($task->primary->[$num]);
}
else {
$state->{stage} = 'AFTER';
}
}
elsif ($state->{stage} eq 'AFTER') {
$state->{after} = (defined $state->{after}) ? $state->{after} : 0;
if (my $add = $task->after->[$state->{after}++]) {
return if $add->around;
$self->push_task($add);
}
else {
$state->{ended} = 1;
}
}
}
$self->finish;
}
sub push_task {
my $self = shift;
my ($task) = @_;
confess "No Task!" unless $task;
confess "Bad Task ($task)!" unless blessed($task) && $task->isa('Test2::Workflow::Task');
if ($task->isa('Test2::Workflow::Build')) {
confess "Can only push a Build instance when initializing the stack"
if @{$self->{+STACK}};
$task = $task->compile();
}
push @{$self->{+STACK}} => {
task => $task,
name => $task->name,
};
}
sub add_mock {
my $self = shift;
my ($mock) = @_;
my $stack = $self->{+STACK};
confess "Nothing on the stack!"
unless $stack && @$stack;
my ($state) = grep { !$_->{task}->scaffold} reverse @$stack;
push @{$state->{mocks}} => $mock;
}
sub isolate {
my $self = shift;
my ($state) = @_;
return if $state->{task}->skip;
my $iso = $state->{task}->iso;
my $async = $state->{task}->async;
# No need to isolate
return undef unless $iso || $async;
# Cannot isolate
unless($self->{+MAX} && $self->is_local) {
# async does not NEED to be isolated
return undef unless $iso;
}
# Wait for a slot, if max is set to 0 then we will not find a slot, instead
# we use '0'. We need to return a defined value to let the stack know that
# the task has ended.
my $slot = 0;
while($self->{+MAX} && $self->is_local) {
$self->cull;
for my $s (1 .. $self->{+MAX}) {
my $st = $self->{+SLOTS}->[$s];
next if $st && !$st->finished;
$self->{+SLOTS}->[$s] = undef;
$slot = $s;
last;
}
last if $slot;
sleep(0.02);
}
my $st = $state->{subtest}
or confess "Cannot isolate a task without a subtest";
if (!$self->no_fork) {
my $out = $st->fork;
if (blessed($out)) {
$state->{in_fork} = $out;
# drop back out to complete the task.
return undef;
}
else {
$self->send_event(
'Note',
message => "Forked PID $out to run: " . $state->{task}->name,
);
$state->{pid} = $out;
}
}
elsif (!$self->no_threads) {
$state->{in_thread} = 1;
my $thr = $st->run_thread(\&run, $self);
$state->{thread} = $thr;
delete $state->{in_thread};
$self->send_event(
'Note',
message => "Started Thread-ID " . $thr->tid . " to run: " . $state->{task}->name,
);
}
else {
$st->finish(skip => "No isolation method available");
return 0;
}
if($slot) {
$self->{+SLOTS}->[$slot] = $st;
}
else {
$st->finish;
}
return $slot;
}
sub cull {
my $self = shift;
my $subtests = delete $self->{+SUBTESTS} || return;
my @new;
# Cull subtests in reverse order, Nested subtests end before their parents.
for my $set (reverse @$subtests) {
my ($st, $task) = @$set;
next if $st->finished;
if (!$st->active && $st->ready) {
$st->finish();
next;
}
# Use unshift to preserve order.
unshift @new => $set;
}
$self->{+SUBTESTS} = \@new;
return;
}
sub finish {
my $self = shift;
while(@{$self->{+SUBTESTS}}) {
$self->cull;
sleep(0.02) if @{$self->{+SUBTESTS}};
}
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Test2::Workflow::Runner - Runs the workflows.
=head1 SOURCE
The source code repository for Test2-Workflow can be found at
F<https://github.com/Test-More/test-more/>.
=head1 MAINTAINERS
=over 4
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
=back
=head1 AUTHORS
=over 4
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
=back
=head1 COPYRIGHT
Copyright Chad Granum E<lt>exodist7@gmail.comE<gt>.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://dev.perl.org/licenses/>
=cut
|