5 if ($ENV{'PERL_CORE'}){
7 unshift @INC, '../lib';
10 if (! $Config{'useithreads'}) {
11 print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
16 use ExtUtils::testlib;
19 my ($id, $ok, $name) = @_;
21 # You have to do it this way or VMS will get confused.
23 print("ok $id - $name\n");
25 print("not ok $id - $name\n");
26 printf("# Failed test at line %d\n", (caller)[2]);
34 print("1..33\n"); ### Number of tests that will be run ###
39 if ($threads::VERSION && ! exists($ENV{'PERL_CORE'})) {
40 print(STDERR "# Testing threads $threads::VERSION\n");
45 ### Start of Testing ###
47 ok(2, 1 == $threads::threads, "Check that threads::threads is true");
50 ok(3,'bar' eq $_[0], "Test that argument passing works");
52 threads->create('test1', 'bar')->join();
55 ok(4,'bar' eq $_[0]->[0]->{'foo'}, "Test that passing arguments as references work");
57 threads->create(\&test2, [{'foo' => 'bar'}])->join();
60 ok(5, shift() == 1, "Test a normal sub");
62 threads->create(\&test3, 1)->join();
66 ok(6, 1, "Detach test");
69 my $thread1 = threads->create('test4');
71 while ($thread1->is_running()) {
76 ok(7, 1, "Detach test");
80 threads->create('test6')->join();
81 ok(9, 1, "Nested thread test");
85 ok(8, 1, "Nested thread test");
88 threads->create('test5')->join();
92 my $self = threads->self();
93 ok(10, $self->tid == 7, "Wanted 7, got ".$self->tid);
94 ok(11, threads->tid() == 7, "Wanted 7, got ".threads->tid());
96 threads->create('test7')->join;
99 my $self = threads->self();
100 ok(12, $self->tid == 8, "Wanted 8, got ".$self->tid);
101 ok(13, threads->tid() == 8, "Wanted 8, got ".threads->tid());
103 threads->create('test8')->join;
106 ok(14, 0 == threads->self->tid(), "Check so that tid for threads work for main thread");
107 ok(15, 0 == threads->tid(), "Check so that tid for threads work for main thread");
112 ok(16, threads->tid() == 9, "Tid should be correct in the clone");
114 threads->create(sub {
115 ok(17, threads->tid() == 9, "And tid be 9 here too");
121 ok(19, threads->tid() == 10, "In destroy it should be correct too" )
124 threads->create(sub {
125 ok(18, threads->tid() == 10, "And tid be 10 here");
126 $foo = bless {}, 'Foo';
132 my $thr1 = threads->create(sub {});
133 my $thr2 = threads->create(sub {});
134 my $thr3 = threads->object($thr1->tid());
136 # Make sure both overloaded '==' and '!=' are working correctly
137 ok(20, $thr1 != $thr2, 'Treads not equal');
138 ok(21, !($thr1 == $thr2), 'Treads not equal');
139 ok(22, $thr1 == $thr3, 'Threads equal');
140 ok(23, !($thr1 != $thr3), 'Threads equal');
142 ok(24, $thr1->_handle(), 'Handle method');
143 ok(25, $thr2->_handle(), 'Handle method');
145 ok(26, threads->object($thr1->tid())->tid() == 11, 'Object method');
146 ok(27, threads->object($thr2->tid())->tid() == 12, 'Object method');
151 my $sub = sub { ok(28, shift() == 1, "Test code ref"); };
152 threads->create($sub, 1)->join();
154 my $thrx = threads->object(99);
155 ok(29, ! defined($thrx), 'No object');
156 $thrx = threads->object();
157 ok(30, ! defined($thrx), 'No object');
158 $thrx = threads->object(undef);
159 ok(31, ! defined($thrx), 'No object');
160 $thrx = threads->object(0);
161 ok(32, ! defined($thrx), 'No object');
163 threads->import('stringify');
164 $thr1 = threads->create(sub {});
165 ok(33, "$thr1" eq $thr1->tid(), 'Stringify');