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