xref: /openbsd/gnu/usr.bin/perl/ext/File-Glob/t/threads.t (revision 91f110e0)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = '../lib';
6    require Config; import Config;
7    if ($Config{'extensions'} !~ /\bFile\/Glob\b/i) {
8        print "1..0\n";
9        exit 0;
10    }
11}
12use strict;
13use warnings;
14# Test::More needs threads pre-loaded
15use if $Config{useithreads}, 'threads';
16use Test::More;
17
18BEGIN {
19    if (! $Config{'useithreads'}) {
20        plan skip_all => "Perl not compiled with 'useithreads'";
21    }
22}
23
24use File::Temp qw(tempdir);
25use File::Spec qw();
26use File::Glob qw(csh_glob);
27
28my($dir) = tempdir(CLEANUP => 1)
29    or die "Could not create temporary directory";
30
31my @temp_files = qw(1_file.tmp 2_file.tmp 3_file.tmp);
32for my $file (@temp_files) {
33    open my $fh, ">", File::Spec->catfile($dir, $file)
34        or die "Could not create file $dir/$file: $!";
35    close $fh;
36}
37my $cwd = Cwd::cwd();
38chdir $dir
39    or die "Could not chdir to $dir: $!";
40
41sub do_glob { scalar csh_glob("*") }
42# Stablish some glob state
43my $first_file = do_glob();
44is($first_file, $temp_files[0]);
45
46my @files;
47push @files, threads->create(\&do_glob)->join() for 1..5;
48is_deeply(
49    \@files,
50    [($temp_files[1]) x 5],
51    "glob() state is cloned for new threads"
52);
53
54@files = threads->create({'context' => 'list'},
55    sub {
56        return do_glob(), threads->create(\&do_glob)->join()
57    })->join();
58
59is_deeply(
60    \@files,
61    [@temp_files[1,2]],
62    "..and for new threads inside threads"
63);
64
65my $second_file = do_glob();
66is($second_file, $temp_files[1], "state doesn't leak from threads");
67
68chdir $cwd
69    or die "Could not chdir back to $cwd: $!";
70
71done_testing;
72