<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test2::IPC::Driver::Files;
use strict;
use warnings;

our $VERSION = '1.302190';

BEGIN { require Test2::IPC::Driver; our @ISA = qw(Test2::IPC::Driver) }

use Test2::Util::HashBase qw{tempdir event_ids read_ids timeouts tid pid globals};

use Scalar::Util qw/blessed/;
use File::Temp();
use Storable();
use File::Spec();
use POSIX();

use Test2::Util qw/try get_tid pkg_to_file IS_WIN32 ipc_separator do_rename do_unlink try_sig_mask/;
use Test2::API qw/test2_ipc_set_pending/;

sub is_viable { 1 }

sub init {
    my $self = shift;

    my $tmpdir = File::Temp::tempdir(
        $ENV{T2_TEMPDIR_TEMPLATE} || "test2" . ipc_separator . $$ . ipc_separator . "XXXXXX",
        CLEANUP =&gt; 0,
        TMPDIR =&gt; 1,
    );

    $self-&gt;abort_trace("Could not get a temp dir") unless $tmpdir;

    $self-&gt;{+TEMPDIR} = File::Spec-&gt;canonpath($tmpdir);

    print STDERR "\nIPC Temp Dir: $tmpdir\n\n"
        if $ENV{T2_KEEP_TEMPDIR};

    $self-&gt;{+EVENT_IDS} = {};
    $self-&gt;{+READ_IDS} = {};
    $self-&gt;{+TIMEOUTS} = {};

    $self-&gt;{+TID} = get_tid();
    $self-&gt;{+PID} = $$;

    $self-&gt;{+GLOBALS} = {};

    return $self;
}

sub hub_file {
    my $self = shift;
    my ($hid) = @_;
    my $tdir = $self-&gt;{+TEMPDIR};
    return File::Spec-&gt;catfile($tdir, "HUB" . ipc_separator . $hid);
}

sub event_file {
    my $self = shift;
    my ($hid, $e) = @_;

    my $tempdir = $self-&gt;{+TEMPDIR};
    my $type = blessed($e) or $self-&gt;abort("'$e' is not a blessed object!");

    $self-&gt;abort("'$e' is not an event object!")
        unless $type-&gt;isa('Test2::Event');

    my $tid = get_tid();
    my $eid = $self-&gt;{+EVENT_IDS}-&gt;{$hid}-&gt;{$$}-&gt;{$tid} += 1;

    my @type = split '::', $type;
    my $name = join(ipc_separator, $hid, $$, $tid, $eid, @type);

    return File::Spec-&gt;catfile($tempdir, $name);
}

sub add_hub {
    my $self = shift;
    my ($hid) = @_;

    my $hfile = $self-&gt;hub_file($hid);

    $self-&gt;abort_trace("File for hub '$hid' already exists")
        if -e $hfile;

    open(my $fh, '&gt;', $hfile) or $self-&gt;abort_trace("Could not create hub file '$hid': $!");
    print $fh "$$\n" . get_tid() . "\n";
    close($fh);
}

sub drop_hub {
    my $self = shift;
    my ($hid) = @_;

    my $tdir = $self-&gt;{+TEMPDIR};
    my $hfile = $self-&gt;hub_file($hid);

    $self-&gt;abort_trace("File for hub '$hid' does not exist")
        unless -e $hfile;

    open(my $fh, '&lt;', $hfile) or $self-&gt;abort_trace("Could not open hub file '$hid': $!");
    my ($pid, $tid) = &lt;$fh&gt;;
    close($fh);

    $self-&gt;abort_trace("A hub file can only be closed by the process that started it\nExpected $pid, got $$")
        unless $pid == $$;

    $self-&gt;abort_trace("A hub file can only be closed by the thread that started it\nExpected $tid, got " . get_tid())
        unless get_tid() == $tid;

    if ($ENV{T2_KEEP_TEMPDIR}) {
        my ($ok, $err) = do_rename($hfile, File::Spec-&gt;canonpath("$hfile.complete"));
        $self-&gt;abort_trace("Could not rename file '$hfile' -&gt; '$hfile.complete': $err") unless $ok
    }
    else {
        my ($ok, $err) = do_unlink($hfile);
        $self-&gt;abort_trace("Could not remove file for hub '$hid': $err") unless $ok
    }

    opendir(my $dh, $tdir) or $self-&gt;abort_trace("Could not open temp dir!");

    my %bad;
    for my $file (readdir($dh)) {
        next if $file =~ m{\.complete$};
        next unless $file =~ m{^$hid};

        eval { $bad{$file} = $self-&gt;read_event_file(File::Spec-&gt;catfile($tdir, $file)); 1 } or $bad{$file} = $@ || "Unknown error reading file";
    }
    closedir($dh);

    return unless keys %bad;

    my $data;
    my $ok = eval {
        require JSON::PP;
        local *UNIVERSAL::TO_JSON = sub { +{ %{$_[0]} } };
        my $json = JSON::PP-&gt;new-&gt;ascii-&gt;pretty-&gt;canonical-&gt;allow_unknown-&gt;allow_blessed-&gt;convert_blessed;
        $data = $json-&gt;encode(\%bad);
        1;
    };
    $ok ||= eval {
        require Data::Dumper;
        local $Data::Dumper::Sortkeys = 1;
        $data = Data::Dumper::Dumper(\%bad);
        1;
    };

    $data = "Could not dump data... sorry." unless defined $data;

    $self-&gt;abort_trace("Not all files from hub '$hid' have been collected!\nHere is the leftover data:\n========================\n$data\n===================\n");
}

sub send {
    my $self = shift;
    my ($hid, $e, $global) = @_;

    my $tempdir = $self-&gt;{+TEMPDIR};
    my $hfile = $self-&gt;hub_file($hid);
    my $dest = $global ? 'GLOBAL' : $hid;

    $self-&gt;abort(&lt;&lt;"    EOT") unless $global || -f $hfile;
hub '$hid' is not available, failed to send event!

There was an attempt to send an event to a hub in a parent process or thread,
but that hub appears to be gone. This can happen if you fork, or start a new
thread from inside subtest, and the parent finishes the subtest before the
child returns.

This can also happen if the parent process is done testing before the child
finishes. Test2 normally waits automatically in the root process, but will not
do so if Test::Builder is loaded for legacy reasons.
    EOT

    my $file = $self-&gt;event_file($dest, $e);
    my $ready = File::Spec-&gt;canonpath("$file.ready");

    if ($global) {
        my $name = $ready;
        $name =~ s{^.*(GLOBAL)}{GLOBAL};
        $self-&gt;{+GLOBALS}-&gt;{$hid}-&gt;{$name}++;
    }

    # Write and rename the file.
    my ($ren_ok, $ren_err);
    my ($ok, $err) = try_sig_mask(sub {
        Storable::store($e, $file);
        ($ren_ok, $ren_err) = do_rename("$file", $ready);
    });

    if ($ok) {
        $self-&gt;abort("Could not rename file '$file' -&gt; '$ready': $ren_err") unless $ren_ok;
        test2_ipc_set_pending($file);
    }
    else {
        my $src_file = __FILE__;
        $err =~ s{ at \Q$src_file\E.*$}{};
        chomp($err);
        my $tid = get_tid();
        my $trace = $e-&gt;trace-&gt;debug;
        my $type = blessed($e);

        $self-&gt;abort(&lt;&lt;"        EOT");

*******************************************************************************
There was an error writing an event:
Destination: $dest
Origin PID:  $$
Origin TID:  $tid
Event Type:  $type
Event Trace: $trace
File Name:   $file
Ready Name:  $ready
Error: $err
*******************************************************************************

        EOT
    }

    return 1;
}

sub driver_abort {
    my $self = shift;
    my ($msg) = @_;

    local ($@, $!, $?, $^E);
    eval {
        my $abort = File::Spec-&gt;catfile($self-&gt;{+TEMPDIR}, "ABORT");
        open(my $fh, '&gt;&gt;', $abort) or die "Could not open abort file: $!";
        print $fh $msg, "\n";
        close($fh) or die "Could not close abort file: $!";
        1;
    } or warn $@;
}

sub cull {
    my $self = shift;
    my ($hid) = @_;

    my $tempdir = $self-&gt;{+TEMPDIR};

    opendir(my $dh, $tempdir) or $self-&gt;abort("could not open IPC temp dir ($tempdir)!");

    my $read = $self-&gt;{+READ_IDS};
    my $timeouts = $self-&gt;{+TIMEOUTS};

    my @out;
    for my $info (sort cmp_events map { $self-&gt;should_read_event($hid, $_) } readdir($dh)) {
        unless ($info-&gt;{global}) {
            my $next = $self-&gt;{+READ_IDS}-&gt;{$info-&gt;{hid}}-&gt;{$info-&gt;{pid}}-&gt;{$info-&gt;{tid}} ||= 1;

            $timeouts-&gt;{$info-&gt;{file}} ||= time;

            if ($next != $info-&gt;{eid}) {
                # Wait up to N seconds for missing events
                next unless 5 &lt; time - $timeouts-&gt;{$info-&gt;{file}};
                $self-&gt;abort("Missing event HID: $info-&gt;{hid}, PID: $info-&gt;{pid}, TID: $info-&gt;{tid}, EID: $info-&gt;{eid}.");
            }

            $self-&gt;{+READ_IDS}-&gt;{$info-&gt;{hid}}-&gt;{$info-&gt;{pid}}-&gt;{$info-&gt;{tid}} = $info-&gt;{eid} + 1;
        }

        my $full = $info-&gt;{full_path};
        my $obj = $self-&gt;read_event_file($full);
        push @out =&gt; $obj;

        # Do not remove global events
        next if $info-&gt;{global};

        if ($ENV{T2_KEEP_TEMPDIR}) {
            my $complete = File::Spec-&gt;canonpath("$full.complete");
            my ($ok, $err) = do_rename($full, $complete);
            $self-&gt;abort("Could not rename IPC file '$full', '$complete': $err") unless $ok;
        }
        else {
            my ($ok, $err) = do_unlink("$full");
            $self-&gt;abort("Could not unlink IPC file '$full': $err") unless $ok;
        }
    }

    closedir($dh);
    return @out;
}

sub parse_event_filename {
    my $self = shift;
    my ($file) = @_;

    # The || is to force 0 in false
    my $complete = substr($file, -9, 9) eq '.complete' || 0 and substr($file, -9, 9, "");
    my $ready    = substr($file, -6, 6) eq '.ready'    || 0 and substr($file, -6, 6, "");

    my @parts = split ipc_separator, $file;
    my ($global, $hid) = $parts[0] eq 'GLOBAL' ? (1, shift @parts) : (0, join ipc_separator, splice(@parts, 0, 4));
    my ($pid, $tid, $eid) = splice(@parts, 0, 3);
    my $type = join '::' =&gt; @parts;

    return {
        file     =&gt; $file,
        ready    =&gt; !!$ready,
        complete =&gt; !!$complete,
        global   =&gt; $global,
        type     =&gt; $type,
        hid      =&gt; $hid,
        pid      =&gt; $pid,
        tid      =&gt; $tid,
        eid      =&gt; $eid,
    };
}

sub should_read_event {
    my $self = shift;
    my ($hid, $file) = @_;

    return if substr($file, 0, 1) eq '.';
    return if substr($file, 0, 3) eq 'HUB';
    CORE::exit(255) if $file eq 'ABORT';

    my $parsed = $self-&gt;parse_event_filename($file);

    return if $parsed-&gt;{complete};
    return unless $parsed-&gt;{ready};
    return unless $parsed-&gt;{global} || $parsed-&gt;{hid} eq $hid;

    return if $parsed-&gt;{global} &amp;&amp; $self-&gt;{+GLOBALS}-&gt;{$hid}-&gt;{$file}++;

    # Untaint the path.
    my $full = File::Spec-&gt;catfile($self-&gt;{+TEMPDIR}, $file);
    ($full) = ($full =~ m/^(.*)$/gs) if ${^TAINT};

    $parsed-&gt;{full_path} = $full;

    return $parsed;
}

sub cmp_events {
    # Globals first
    return -1 if $a-&gt;{global} &amp;&amp; !$b-&gt;{global};
    return  1 if $b-&gt;{global} &amp;&amp; !$a-&gt;{global};

    return $a-&gt;{pid} &lt;=&gt; $b-&gt;{pid}
        || $a-&gt;{tid} &lt;=&gt; $b-&gt;{tid}
        || $a-&gt;{eid} &lt;=&gt; $b-&gt;{eid};
}

sub read_event_file {
    my $self = shift;
    my ($file) = @_;

    my $obj = Storable::retrieve($file);
    $self-&gt;abort("Got an unblessed object: '$obj'")
        unless blessed($obj);

    unless ($obj-&gt;isa('Test2::Event')) {
        my $pkg  = blessed($obj);
        my $mod_file = pkg_to_file($pkg);
        my ($ok, $err) = try { require $mod_file };

        $self-&gt;abort("Event has unknown type ($pkg), tried to load '$mod_file' but failed: $err")
            unless $ok;

        $self-&gt;abort("'$obj' is not a 'Test2::Event' object")
            unless $obj-&gt;isa('Test2::Event');
    }

    return $obj;
}

sub waiting {
    my $self = shift;
    require Test2::Event::Waiting;
    $self-&gt;send(
        GLOBAL =&gt; Test2::Event::Waiting-&gt;new(
            trace =&gt; Test2::EventFacet::Trace-&gt;new(frame =&gt; [caller()]),
        ),
        'GLOBAL'
    );
    return;
}

sub DESTROY {
    my $self = shift;

    return unless defined $self-&gt;pid;
    return unless defined $self-&gt;tid;

    return unless $$        == $self-&gt;pid;
    return unless get_tid() == $self-&gt;tid;

    my $tempdir = $self-&gt;{+TEMPDIR};

    my $aborted = 0;
    my $abort_file = File::Spec-&gt;catfile($self-&gt;{+TEMPDIR}, "ABORT");
    if (-e $abort_file) {
        $aborted = 1;
        my ($ok, $err) = do_unlink($abort_file);
        warn $err unless $ok;
    }

    opendir(my $dh, $tempdir) or $self-&gt;abort("Could not open temp dir! ($tempdir)");
    while(my $file = readdir($dh)) {
        next if $file =~ m/^\.+$/;
        next if $file =~ m/\.complete$/;
        my $full = File::Spec-&gt;catfile($tempdir, $file);

        my $sep = ipc_separator;
        if ($aborted || $file =~ m/^(GLOBAL|HUB$sep)/) {
            $full =~ m/^(.*)$/;
            $full = $1; # Untaint it
            next if $ENV{T2_KEEP_TEMPDIR};
            my ($ok, $err) = do_unlink($full);
            $self-&gt;abort("Could not unlink IPC file '$full': $err") unless $ok;
            next;
        }

        $self-&gt;abort("Leftover files in the directory ($full)!\n");
    }
    closedir($dh);

    if ($ENV{T2_KEEP_TEMPDIR}) {
        print STDERR "# Not removing temp dir: $tempdir\n";
        return;
    }

    my $abort = File::Spec-&gt;catfile($self-&gt;{+TEMPDIR}, "ABORT");
    unlink($abort) if -e $abort;
    rmdir($tempdir) or warn "Could not remove IPC temp dir ($tempdir)";
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Test2::IPC::Driver::Files - Temp dir + Files concurrency model.

=head1 DESCRIPTION

This is the default, and fallback concurrency model for L&lt;Test2&gt;. This
sends events between processes and threads using serialized files in a
temporary directory. This is not particularly fast, but it works everywhere.

=head1 SYNOPSIS

    use Test2::IPC::Driver::Files;

    # IPC is now enabled

=head1 ENVIRONMENT VARIABLES

=over 4

=item T2_KEEP_TEMPDIR=0

When true, the tempdir used by the IPC driver will not be deleted when the test
is done.

=item T2_TEMPDIR_TEMPLATE='test2-XXXXXX'

This can be used to set the template for the IPC temp dir. The template should
follow template specifications from L&lt;File::Temp&gt;.

=back

=head1 SEE ALSO

See L&lt;Test2::IPC::Driver&gt; for methods.

=head1 SOURCE

The source code repository for Test2 can be found at
F&lt;http://github.com/Test-More/test-more/&gt;.

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 AUTHORS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2020 Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See F&lt;http://dev.perl.org/licenses/&gt;

=cut
</pre></body></html>