1 2 3# 4# The reason this does not use a Test module is that 5# they mess up test numbers between threads 6# 7# And even when that will be fixed, this is a basic 8# test and should not rely on shared variables 9# 10# This will test the basic API, it will not use any coderefs 11# as they are more advanced 12# 13######################### 14 15 16BEGIN { 17 chdir 't' if -d 't'; 18 push @INC, '../lib'; 19 require Config; import Config; 20 unless ($Config{'useithreads'}) { 21 print "1..0 # Skip: no useithreads\n"; 22 exit 0; 23 } 24} 25 26use ExtUtils::testlib; 27use strict; 28BEGIN { $| = 1; print "1..19\n" }; 29use threads; 30 31 32 33print "ok 1\n"; 34 35 36######################### 37 38 39 40 41sub ok { 42 my ($id, $ok, $name) = @_; 43 44 # You have to do it this way or VMS will get confused. 45 print $ok ? "ok $id - $name\n" : "not ok $id - $name\n"; 46 47 printf "# Failed test at line %d\n", (caller)[2] unless $ok; 48 49 return $ok; 50} 51 52 53 54sub test1 { 55 ok(2,'bar' eq $_[0],"Test that argument passing works"); 56} 57threads->create('test1','bar')->join(); 58 59sub test2 { 60 ok(3,'bar' eq $_[0]->[0]->{foo},"Test that passing arguments as references work"); 61} 62 63threads->create('test2',[{foo => 'bar'}])->join(); 64 65 66#test execuion of normal sub 67sub test3 { ok(4,shift() == 1,"Test a normal sub") } 68threads->create('test3',1)->join(); 69 70 71#check Config 72ok(5, 1 == $threads::threads,"Check that threads::threads is true"); 73 74#test trying to detach thread 75 76sub test4 { ok(6,1,"Detach test") } 77 78my $thread1 = threads->create('test4'); 79 80$thread1->detach(); 81threads->yield; # help out non-preemptive thread implementations 82sleep 2; 83ok(7,1,"Detach test"); 84 85 86 87sub test5 { 88 threads->create('test6')->join(); 89 ok(9,1,"Nested thread test"); 90} 91 92sub test6 { 93 ok(8,1,"Nested thread test"); 94} 95 96threads->create('test5')->join(); 97 98sub test7 { 99 my $self = threads->self(); 100 ok(10, $self->tid == 7, "Wanted 7, got ".$self->tid); 101 ok(11, threads->tid() == 7, "Wanted 7, got ".threads->tid()); 102} 103 104threads->create('test7')->join; 105 106sub test8 { 107 my $self = threads->self(); 108 ok(12, $self->tid == 8, "Wanted 8, got ".$self->tid); 109 ok(13, threads->tid() == 8, "Wanted 8, got ".threads->tid()); 110} 111 112threads->create('test8')->join; 113 114 115#check support for threads->self() in main thread 116ok(14, 0 == threads->self->tid(),"Check so that tid for threads work for main thread"); 117ok(15, 0 == threads->tid(),"Check so that tid for threads work for main thread"); 118 119{ 120 no warnings; 121 local *CLONE = sub { ok(16, threads->tid() == 9, "Tid should be correct in the clone")}; 122 threads->create(sub { ok(17, threads->tid() == 9, "And tid be 9 here too") })->join(); 123} 124 125{ 126 127 sub Foo::DESTROY { 128 ok(19, threads->tid() == 10, "In destroy it should be correct too" ) 129 } 130 my $foo; 131 threads->create(sub { ok(18, threads->tid() == 10, "And tid be 10 here"); 132 $foo = bless {}, 'Foo'; 133 return undef; 134 })->join(); 135 136} 1371; 138 139 140 141 142 143 144 145