| 1 |
package Test2::Harness::Runner::Preload::Stage; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Carp qw/croak/; |
| 8 |
|
| 9 |
use Test2::Harness::Util::HashBase qw{ |
| 10 |
<name |
| 11 |
<frame |
| 12 |
<children |
| 13 |
<pre_fork_callbacks |
| 14 |
<post_fork_callbacks |
| 15 |
<pre_launch_callbacks |
| 16 |
<load_sequence |
| 17 |
<watches |
| 18 |
eager |
| 19 |
reload_remove_check |
| 20 |
reload_inplace_check |
| 21 |
}; |
| 22 |
|
| 23 |
sub init { |
| 24 |
my $self = shift; |
| 25 |
|
| 26 |
$self->{+FRAME} //= [caller(1)]; |
| 27 |
|
| 28 |
croak "'name' is a required attribute" unless $self->{+NAME}; |
| 29 |
|
| 30 |
croak "Stage name 'base' is reserved, pick another name" if $self->{+NAME} eq 'base'; |
| 31 |
croak "Stage name 'NOPRELOAD' is reserved, pick another name" if $self->{+NAME} eq 'NOPRELOAD'; |
| 32 |
|
| 33 |
$self->{+CHILDREN} //= []; |
| 34 |
|
| 35 |
$self->{+PRE_FORK_CALLBACKS} //= []; |
| 36 |
$self->{+POST_FORK_CALLBACKS} //= []; |
| 37 |
$self->{+PRE_LAUNCH_CALLBACKS} //= []; |
| 38 |
|
| 39 |
$self->{+LOAD_SEQUENCE} //= []; |
| 40 |
$self->{+WATCHES} //= {}; |
| 41 |
} |
| 42 |
|
| 43 |
sub watch { |
| 44 |
my $self = shift; |
| 45 |
my ($file, $callback) = @_; |
| 46 |
croak "The first argument must be a file" unless $file && -f $file; |
| 47 |
croak "The callback argument is required" unless $callback && ref($callback) eq 'CODE'; |
| 48 |
croak "There is already a watch on file '$file'" if $self->{+WATCHES}->{$file}; |
| 49 |
|
| 50 |
$self->{+WATCHES}->{$file} = $callback; |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
sub all_children { |
| 55 |
my $self = shift; |
| 56 |
|
| 57 |
my @out = @{$self->{+CHILDREN}}; |
| 58 |
|
| 59 |
for (my $i = 0; $i < @out; $i++) { |
| 60 |
my $it = $out[$i]; |
| 61 |
push @out => @{$it->children}; |
| 62 |
} |
| 63 |
|
| 64 |
return \@out; |
| 65 |
} |
| 66 |
|
| 67 |
sub add_child { |
| 68 |
my $self = shift; |
| 69 |
my ($stage) = @_; |
| 70 |
push @{$self->{+CHILDREN}} => $stage; |
| 71 |
} |
| 72 |
|
| 73 |
sub add_pre_fork_callback { |
| 74 |
my $self = shift; |
| 75 |
my ($cb) = @_; |
| 76 |
croak "Callback must be a coderef" unless ref($cb) eq 'CODE'; |
| 77 |
push @{$self->{+PRE_FORK_CALLBACKS}} => $cb; |
| 78 |
} |
| 79 |
|
| 80 |
sub add_post_fork_callback { |
| 81 |
my $self = shift; |
| 82 |
my ($cb) = @_; |
| 83 |
croak "Callback must be a coderef" unless ref($cb) eq 'CODE'; |
| 84 |
push @{$self->{+POST_FORK_CALLBACKS}} => $cb; |
| 85 |
} |
| 86 |
|
| 87 |
sub add_pre_launch_callback { |
| 88 |
my $self = shift; |
| 89 |
my ($cb) = @_; |
| 90 |
croak "Callback must be a coderef" unless ref($cb) eq 'CODE'; |
| 91 |
push @{$self->{+PRE_LAUNCH_CALLBACKS}} => $cb; |
| 92 |
} |
| 93 |
|
| 94 |
sub add_to_load_sequence { |
| 95 |
my $self = shift; |
| 96 |
|
| 97 |
for my $item (@_) { |
| 98 |
croak "Item '$item' is not a valid preload, must be a module name (scalar) or a coderef" |
| 99 |
unless ref($item) eq 'CODE' || !ref($item); |
| 100 |
|
| 101 |
push @{$self->{+LOAD_SEQUENCE}} => $item; |
| 102 |
} |
| 103 |
|
| 104 |
return @_; |
| 105 |
} |
| 106 |
|
| 107 |
sub do_pre_fork { my $self = shift; $_->(@_) for @{$self->{+PRE_FORK_CALLBACKS}} } |
| 108 |
sub do_post_fork { my $self = shift; $_->(@_) for @{$self->{+POST_FORK_CALLBACKS}} } |
| 109 |
sub do_pre_launch { my $self = shift; $_->(@_) for @{$self->{+PRE_LAUNCH_CALLBACKS}} } |
| 110 |
|
| 111 |
1; |
| 112 |
|
| 113 |
__END__ |
| 114 |
|
| 115 |
=pod |
| 116 |
|
| 117 |
=encoding UTF-8 |
| 118 |
|
| 119 |
=head1 NAME |
| 120 |
|
| 121 |
Test2::Harness::Runner::Preload::Stage - Abstraction of a preload stage. |
| 122 |
|
| 123 |
=head1 DESCRIPTION |
| 124 |
|
| 125 |
This is an implementation detail. You are not intended to directly use/modify |
| 126 |
instances of this class. See L<Test2::Harness::Runner::Preload> for |
| 127 |
documentation on how to write a custom preload library. |
| 128 |
|
| 129 |
=head1 SOURCE |
| 130 |
|
| 131 |
The source code repository for Test2-Harness can be found at |
| 132 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 133 |
|
| 134 |
=head1 MAINTAINERS |
| 135 |
|
| 136 |
=over 4 |
| 137 |
|
| 138 |
=item Chad Granum E<lt>[email protected]<gt> |
| 139 |
|
| 140 |
=back |
| 141 |
|
| 142 |
=head1 AUTHORS |
| 143 |
|
| 144 |
=over 4 |
| 145 |
|
| 146 |
=item Chad Granum E<lt>[email protected]<gt> |
| 147 |
|
| 148 |
=back |
| 149 |
|
| 150 |
=head1 COPYRIGHT |
| 151 |
|
| 152 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 153 |
|
| 154 |
This program is free software; you can redistribute it and/or |
| 155 |
modify it under the same terms as Perl itself. |
| 156 |
|
| 157 |
See F<http://dev.perl.org/licenses/> |
| 158 |
|
| 159 |
=cut |