Skip tests if Devel::Peek not built
[p5sagit/p5-mst-13.2.git] / ext / threads / t / join.t
1 BEGIN {
2     chdir 't' if -d 't';
3     push @INC, '../lib';
4     require Config; import Config;
5     unless ($Config{'useithreads'}) {
6         print "1..0 # Skip: no useithreads\n";
7         exit 0;
8     }
9     if ($Config{'extensions'} !~ /\bDevel\/Peek\b/) {
10         print "1..0 # Skip: Devel::Peek was not built\n";
11         exit 0;
12     }
13 }
14
15 use ExtUtils::testlib;
16 use strict;
17 BEGIN { print "1..14\n" };
18 use threads;
19 use threads::shared;
20
21 my $test_id = 1;
22 share($test_id);
23 use Devel::Peek qw(Dump);
24
25 sub ok {
26     my ($ok, $name) = @_;
27
28     # You have to do it this way or VMS will get confused.
29     print $ok ? "ok $test_id - $name\n" : "not ok $test_id - $name\n";
30
31     printf "# Failed test at line %d\n", (caller)[2] unless $ok;
32     $test_id++;
33     return $ok;
34 }
35
36 sub skip {
37     ok(1, "# Skipped: @_");
38 }
39
40 ok(1,"");
41
42
43 {
44     my $retval = threads->create(sub { return ("hi") })->join();
45     ok($retval eq 'hi', "Check basic returnvalue");
46 }
47 {
48     my ($thread) = threads->create(sub { return (1,2,3) });
49     my @retval = $thread->join();
50     ok($retval[0] == 1 && $retval[1] == 2 && $retval[2] == 3,'');
51 }
52 {
53     my $retval = threads->create(sub { return [1] })->join();
54     ok($retval->[0] == 1,"Check that a array ref works",);
55 }
56 {
57     my $retval = threads->create(sub { return { foo => "bar" }})->join();
58     ok($retval->{foo} eq 'bar',"Check that hash refs work");
59 }
60 {
61     my $retval = threads->create( sub {
62         open(my $fh, "+>threadtest") || die $!;
63         print $fh "test\n";
64         return $fh;
65     })->join();
66     ok(ref($retval) eq 'GLOB', "Check that we can return FH $retval");
67     print $retval "test2\n";
68 #    seek($retval,0,0);
69 #    ok(<$retval> eq "test\n");
70     close($retval);
71     unlink("threadtest");
72 }
73 {
74     my $test = "hi";
75     my $retval = threads->create(sub { return $_[0]}, \$test)->join();
76     ok($$retval eq 'hi','');
77 }
78 {
79     my $test = "hi";
80     share($test);
81     my $retval = threads->create(sub { return $_[0]}, \$test)->join();
82     ok($$retval eq 'hi','');
83     $test = "foo";
84     ok($$retval eq 'foo','');
85 }
86 {
87     my %foo;
88     share(%foo);
89     threads->create(sub { 
90         my $foo;
91         share($foo);
92         $foo = "thread1";
93         return $foo{bar} = \$foo;
94     })->join();
95     ok(1,"");
96 }
97
98 # We parse ps output so this is OS-dependent.
99 if ($^O eq 'linux') {
100   # First modify $0 in a subthread.
101   print "# mainthread: \$0 = $0\n";
102   threads->new( sub {
103                   print "# subthread: \$0 = $0\n";
104                   $0 = "foobar";
105                   print "# subthread: \$0 = $0\n" } )->join;
106   print "# mainthread: \$0 = $0\n";
107   print "# pid = $$\n";
108   if (open PS, "ps -f |") { # Note: must work in (all) systems.
109     my ($sawpid, $sawexe);
110     while (<PS>) {
111       chomp;
112       print "# [$_]\n";
113       if (/^\S+\s+$$\s/) {
114         $sawpid++;
115         if (/\sfoobar\s*$/) { # Linux 2.2 leaves extra trailing spaces.
116           $sawexe++;
117         }
118         last;
119       }
120     }
121     close PS or die;
122     if ($sawpid) {
123       ok($sawpid && $sawexe, 'altering $0 is effective');
124     } else {
125       skip("\$0 check: did not see pid $$ in 'ps -f |'");
126     }
127   } else {
128     skip("\$0 check: opening 'ps -f |' failed: $!");
129   }
130 } else {
131   skip("\$0 check: only on Linux");
132 }
133
134 {
135     my $t = threads->new(sub {});
136     $t->join;
137     my $x = threads->new(sub {});
138     $x->join;
139     eval {
140       $t->join;
141     };
142     my $ok = 0;
143     $ok++ if($@ =~/Thread already joined/);
144     ok($ok, "Double join works");
145 }
146
147 {
148     # The "use IO::File" is not actually used for anything; its only
149     # purpose is to incite a lot of calls to newCONSTSUB.  See the p5p
150     # archives for the thread "maint@20974 or before broke mp2 ithreads test".
151     use IO::File;
152     $_->join for map threads->new(sub{ok($_, "stress newCONSTSUB")}), 1..2;
153 }
154