Upgrade to Encode 2.16
[p5sagit/p5-mst-13.2.git] / ext / threads / t / problems.t
CommitLineData
0f1612a7 1use strict;
2use warnings;
da46a8d0 3
4BEGIN {
0f1612a7 5 if ($ENV{'PERL_CORE'}){
6 chdir 't';
7 unshift @INC, '../lib';
8 }
9 use Config;
da46a8d0 10 unless ($Config{'useithreads'}) {
11 print "1..0 # Skip: no useithreads\n";
12 exit 0;
13 }
14}
15
0f1612a7 16use ExtUtils::testlib;
17
18BEGIN {
19 $| = 1;
20 if ($] == 5.008) {
f2cba68d 21 print("1..11\n"); ### Number of tests that will be run ###
0f1612a7 22 } else {
23 print("1..15\n"); ### Number of tests that will be run ###
24 }
25};
26
da46a8d0 27use threads;
28use threads::shared;
0f1612a7 29print("ok 1 - Loaded\n");
30
31### Start of Testing ###
32
33no warnings 'deprecated'; # Suppress warnings related to :unique
34
94a66813 35use Hash::Util 'lock_keys';
997c206d 36
37# Note that we can't use Test::More here, as we would need to
38# call is() from within the DESTROY() function at global destruction time,
39# and parts of Test::* may have already been freed by then
40
0f1612a7 41my $test : shared = 2;
997c206d 42
43sub is($$$) {
44 my ($got, $want, $desc) = @_;
f2cba68d 45 lock($test);
997c206d 46 unless ($got eq $want) {
47 print "# EXPECTED: $want\n";
94a66813 48 print "# GOT: $got\n";
997c206d 49 print "not ";
50 }
51 print "ok $test - $desc\n";
52 $test++;
53}
da46a8d0 54
55
56#
57# This tests for too much destruction
58# which was caused by cloning stashes
59# on join which led to double the dataspace
60#
61#########################
f2cba68d 62if ($] != 5.008)
da46a8d0 63{
da46a8d0 64 sub Foo::DESTROY {
65 my $self = shift;
66 my ($package, $file, $line) = caller;
997c206d 67 is(threads->tid(),$self->{tid},
68 "In destroy[$self->{tid}] it should be correct too" )
da46a8d0 69 }
70 my $foo;
71 $foo = bless {tid => 0}, 'Foo';
72 my $bar = threads->create(sub {
997c206d 73 is(threads->tid(),1, "And tid be 1 here");
da46a8d0 74 $foo->{tid} = 1;
75 return $foo;
76 })->join();
77 $bar->{tid} = 0;
da46a8d0 78}
ad4404a3 79
80#
81# This tests whether we can call Config::myconfig after threads have been
82# started (interpreter cloned). 5.8.1 and 5.8.2 contained a bug that would
83# disallow that too be done, because an attempt was made to change a variable
84# with the : unique attribute.
85#
86#########################
f2cba68d 87{
88 lock($test);
89 if ($] == 5.008 || $] >= 5.008003) {
90 threads->create( sub {1} )->join;
91 my $not = eval { Config::myconfig() } ? '' : 'not ';
92 print "${not}ok $test - Are we able to call Config::myconfig after clone\n";
93 } else {
94 print "ok $test # Skip Are we able to call Config::myconfig after clone\n";
95 }
96 $test++;
0f1612a7 97}
ad4404a3 98
53c33732 99# bugid 24383 - :unique hashes weren't being made readonly on interpreter
100# clone; check that they are.
101
102our $unique_scalar : unique;
103our @unique_array : unique;
104our %unique_hash : unique;
f4cc38af 105threads->create(
53c33732 106 sub {
f2cba68d 107 lock($test);
0abe3f7c 108 my $TODO = ":unique needs to be re-implemented in a non-broken way";
53c33732 109 eval { $unique_scalar = 1 };
0abe3f7c 110 print $@ =~ /read-only/
111 ? '' : 'not ', "ok $test # TODO $TODO unique_scalar\n";
53c33732 112 $test++;
113 eval { $unique_array[0] = 1 };
0abe3f7c 114 print $@ =~ /read-only/
115 ? '' : 'not ', "ok $test # TODO $TODO - unique_array\n";
53c33732 116 $test++;
0f1612a7 117 if ($] >= 5.008003 && $^O ne 'MSWin32') {
118 eval { $unique_hash{abc} = 1 };
119 print $@ =~ /disallowed/
120 ? '' : 'not ', "ok $test # TODO $TODO - unique_hash\n";
121 } else {
122 print("ok $test # Skip $TODO - unique_hash\n");
123 }
53c33732 124 $test++;
125 }
126)->join;
127
371fce9b 128# bugid #24940 :unique should fail on my and sub declarations
129
130for my $decl ('my $x : unique', 'sub foo : unique') {
f2cba68d 131 {
132 lock($test);
133 if ($] >= 5.008005) {
134 eval $decl;
135 print $@ =~ /^The 'unique' attribute may only be applied to 'our' variables/
136 ? '' : 'not ', "ok $test - $decl\n";
137 } else {
138 print("ok $test # Skip $decl\n");
139 }
140 $test++;
0f1612a7 141 }
371fce9b 142}
143
144
b23f1a86 145# Returing a closure from a thread caused problems. If the last index in
146# the anon sub's pad wasn't for a lexical, then a core dump could occur.
147# Otherwise, there might be leaked scalars.
148
a6144651 149# XXX DAPM 9-Jan-04 - backed this out for now - returning a closure from a
150# thread seems to crash win32
151
152# sub f {
153# my $x = "foo";
154# sub { $x."bar" };
155# }
156#
f4cc38af 157# my $string = threads->create(\&f)->join->();
a6144651 158# print $string eq 'foobar' ? '' : 'not ', "ok $test - returning closure\n";
159# $test++;
b23f1a86 160
0f1612a7 161
94a66813 162# Nothing is checking that total keys gets cloned correctly.
163
164my %h = (1,2,3,4);
165is (keys %h, 2, "keys correct in parent");
166
f4cc38af 167my $child = threads->create(sub { return scalar keys %h })->join;
94a66813 168is ($child, 2, "keys correct in child");
169
170lock_keys (%h);
171delete $h{1};
172
173is (keys %h, 1, "keys correct in parent with restricted hash");
174
f4cc38af 175$child = threads->create(sub { return scalar keys %h })->join;
94a66813 176is ($child, 1, "keys correct in child with restricted hash");
177
da46a8d0 1781;