(2nd revised) 1st patch to sync blead 'threads' with CPAN
[p5sagit/p5-mst-13.2.git] / ext / threads / t / basic.t
index 893c30b..a4c4fef 100755 (executable)
@@ -1,4 +1,5 @@
-
+use strict;
+use warnings;
 
 #
 # The reason this does not use a Test module is that
 
 
 BEGIN {
-    chdir 't' if -d 't';
-    @INC = '../lib';
-    require Config; import Config;
+    if ($ENV{'PERL_CORE'}){
+        chdir 't';
+        unshift @INC, '../lib';
+    }
+    use Config;
     unless ($Config{'useithreads'}) {
        print "1..0 # Skip: no useithreads\n";
        exit 0; 
@@ -24,17 +27,19 @@ BEGIN {
 }
 
 use ExtUtils::testlib;
-use strict;
-BEGIN { $| = 1; print "1..15\n" };
-use threads;
 
+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 ###
 
 
 
@@ -50,7 +55,6 @@ sub ok {
 }
 
 
-
 sub test1 {
        ok(2,'bar' eq $_[0],"Test that argument passing works");
 }
@@ -60,12 +64,12 @@ sub test2 {
        ok(3,'bar' eq $_[0]->[0]->{foo},"Test that passing arguments as references work");
 }
 
-threads->create('test2',[{foo => 'bar'}])->join();
+threads->create(\&test2,[{foo => 'bar'}])->join();
 
 
 #test execuion of normal sub
 sub test3 { ok(4,shift() == 1,"Test a normal sub") }
-threads->create('test3',1)->join();
+threads->create(\&test3,1)->join();
 
 
 #check Config
@@ -73,20 +77,13 @@ ok(5, 1 == $threads::threads,"Check that threads::threads is true");
 
 #test trying to detach thread
 
-sub test4 { ok(6,1,"Detach test"); rmdir "thrsem" }
-
-# Just a sleep() would not guarantee that we sleep and will not
-# wake up before the just created thread finishes.  Instead, let's
-# use the filesystem as a semaphore.  Creating a directory and removing
-# it should be a reasonably atomic operation even over NFS. 
-# Also, we do not want to depend here on shared variables.
-
-mkdir "thrsem", 0700;
+sub test4 { ok(6,1,"Detach test") }
 
 my $thread1 = threads->create('test4');
 
 $thread1->detach();
-sleep 1 while -d "thrsem";
+threads->yield; # help out non-preemptive thread implementations
+sleep 2;
 ok(7,1,"Detach test");
 
 
@@ -123,8 +120,49 @@ threads->create('test8')->join;
 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");
 
-END {
-    1 while rmdir "thrsem";
+{
+       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();
 }
 
-1;
+{ 
+
+    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