X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=ext%2Fthreads%2Ft%2Fbasic.t;h=a4c4fef463e6ac26a1d6c6e03d1fddd51150e1a7;hb=0f1612a7416fa2b6a078554fb1e7168e5fd5c31c;hp=3f24fcd1db358016a7df30dddc4739560a390cf4;hpb=a54396a03c51089dce3d7bc2dee3f48f90443e38;p=p5sagit%2Fp5-mst-13.2.git diff --git a/ext/threads/t/basic.t b/ext/threads/t/basic.t index 3f24fcd..a4c4fef 100755 --- a/ext/threads/t/basic.t +++ b/ext/threads/t/basic.t @@ -1,4 +1,5 @@ - +use strict; +use warnings; # # The reason this does not use a Test module is that @@ -6,103 +7,162 @@ # # And even when that will be fixed, this is a basic # test and should not rely on shared variables -# +# +# This will test the basic API, it will not use any coderefs +# as they are more advanced # ######################### +BEGIN { + if ($ENV{'PERL_CORE'}){ + chdir 't'; + unshift @INC, '../lib'; + } + use Config; + unless ($Config{'useithreads'}) { + print "1..0 # Skip: no useithreads\n"; + exit 0; + } +} + use ExtUtils::testlib; -use strict; -BEGIN { print "1..12\n" }; + +BEGIN { $| = 1; print "1..28\n" }; use threads; -print "ok 1\n"; +if ($threads::VERSION && ! exists($ENV{'PERL_CORE'})) { + print(STDERR "# Testing threads $threads::VERSION\n"); +} + +ok(1, 1, 'Loaded'); + +### Start of Testing ### -######################### -# Insert your test code below, the Test module is use()ed here so read -# its man page ( perldoc Test ) for help writing this test script. -#my $bar; sub ok { my ($id, $ok, $name) = @_; - + # You have to do it this way or VMS will get confused. print $ok ? "ok $id - $name\n" : "not ok $id - $name\n"; printf "# Failed test at line %d\n", (caller)[2] unless $ok; - + return $ok; } +sub test1 { + ok(2,'bar' eq $_[0],"Test that argument passing works"); +} +threads->create('test1','bar')->join(); +sub test2 { + ok(3,'bar' eq $_[0]->[0]->{foo},"Test that passing arguments as references work"); +} -#test passing of simple argument -my $thread = threads->create(sub { ok(2, 'bar' eq $_[0]),"" },"bar"); -$thread->join(); - - -#test passing of complex argument - -$thread = threads->create(sub { ok(3, 'bar' eq $_[0]->[0]->{foo})},[{foo => 'bar'}]); - -$thread->join(); +threads->create(\&test2,[{foo => 'bar'}])->join(); #test execuion of normal sub -sub bar { ok(4,shift() == 1,"") } -threads->create(\&bar,1)->join(); +sub test3 { ok(4,shift() == 1,"Test a normal sub") } +threads->create(\&test3,1)->join(); #check Config -ok(5, 1 == $Config::threads,""); +ok(5, 1 == $threads::threads,"Check that threads::threads is true"); #test trying to detach thread -my $thread1 = threads->create(sub {ok(6,1,"")}); +sub test4 { ok(6,1,"Detach test") } + +my $thread1 = threads->create('test4'); $thread1->detach(); -sleep 1; -ok(7,1,""); -#create nested threads -unless($^O eq 'MSWin32') { - my $thread3 = threads->create(sub { threads->create(sub {})})->join(); -} +threads->yield; # help out non-preemptive thread implementations +sleep 2; +ok(7,1,"Detach test"); + -my @threads; -my $i; -unless($^O eq 'MSWin32') { -for(1..25) { - push @threads, threads->create(sub { for(1..100000) { my $i } threads->create(sub { sleep 2})->join() }); +sub test5 { + threads->create('test6')->join(); + ok(9,1,"Nested thread test"); } -foreach my $thread (@threads) { - $thread->join(); + +sub test6 { + ok(8,1,"Nested thread test"); } + +threads->create('test5')->join(); + +sub test7 { + my $self = threads->self(); + ok(10, $self->tid == 7, "Wanted 7, got ".$self->tid); + ok(11, threads->tid() == 7, "Wanted 7, got ".threads->tid()); } -ok(8,1,""); -threads->create(sub { - my $self = threads->self(); - ok(9,$self->tid() == 57,""); -})->join(); -threads->create(sub { - my $self = threads->self(); - ok(10,$self->tid() == 58,""); -})->join(); + +threads->create('test7')->join; + +sub test8 { + my $self = threads->self(); + ok(12, $self->tid == 8, "Wanted 8, got ".$self->tid); + ok(13, threads->tid() == 8, "Wanted 8, got ".threads->tid()); +} + +threads->create('test8')->join; + #check support for threads->self() in main thread -ok(11, 0 == threads->self->tid(),""); -ok(12, 0 == threads->tid(),"Check so that tid for threads work for current tid"); +ok(14, 0 == threads->self->tid(),"Check so that tid for threads work for main thread"); +ok(15, 0 == threads->tid(),"Check so that tid for threads work for main thread"); +{ + no warnings; + local *CLONE = sub { ok(16, threads->tid() == 9, "Tid should be correct in the clone")}; + threads->create(sub { ok(17, threads->tid() == 9, "And tid be 9 here too") })->join(); +} + +{ + sub Foo::DESTROY { + ok(19, threads->tid() == 10, "In destroy it should be correct too" ) + } + my $foo; + threads->create(sub { ok(18, threads->tid() == 10, "And tid be 10 here"); + $foo = bless {}, 'Foo'; + return undef; + })->join(); + +} +my $thr1 = threads->create(sub {}); +my $thr2 = threads->create(sub {}); +my $thr3 = threads->object($thr1->tid()); +ok(20, $thr1 != $thr2, 'Treads not equal'); +ok(21, $thr1 == $thr3, 'Threads equal'); +ok(22, threads->object($thr1->tid())->tid() == 11, 'Object method'); +ok(23, threads->object($thr2->tid())->tid() == 12, 'Object method'); +$thr1->join(); +$thr2->join(); +my $sub = sub { ok(24, shift() == 1, "Test code ref"); }; +threads->create($sub, 1)->join(); +my $thrx = threads->object(99); +ok(25, ! defined($thrx), 'No object'); +$thrx = threads->object(); +ok(26, ! defined($thrx), 'No object'); +$thrx = threads->object(undef); +ok(27, ! defined($thrx), 'No object'); +$thrx = threads->object(0); +ok(28, ! defined($thrx), 'No object'); +# EOF