]> git.vpit.fr Git - perl/modules/Test-Leaner.git/blobdiff - t/80-threads.t
Fix and test thread safety
[perl/modules/Test-Leaner.git] / t / 80-threads.t
diff --git a/t/80-threads.t b/t/80-threads.t
new file mode 100644 (file)
index 0000000..3b0a595
--- /dev/null
@@ -0,0 +1,47 @@
+#!perl -T
+
+use strict;
+use warnings;
+
+sub skipall {
+ my ($msg) = @_;
+ require Test::Leaner;
+ Test::Leaner::plan(skip_all => $msg);
+}
+
+use Config qw/%Config/;
+
+BEGIN {
+ my $force = $ENV{PERL_TEST_LEANER_TEST_THREADS} ? 1 : !1;
+ my $t_v   = $force ? '0' : '1.67';
+ skipall 'This perl wasn\'t built to support threads'
+                                                    unless $Config{useithreads};
+ skipall 'perl 5.13.4 required to test thread safety'
+                                                unless $force or $] >= 5.013004;
+ skipall "threads $t_v required to test thread safety"
+                                              unless eval "use threads $t_v; 1";
+}
+
+use Test::Leaner; # after threads
+
+BEGIN {
+ skipall 'This Test::Leaner isn\'t thread safe' unless Test::Leaner::THREADSAFE;
+ plan tests => 8 * 10;
+ defined and diag "Using threads $_" for $threads::VERSION;
+}
+
+sub tick {
+ sleep 1 if rand() < 0.5;
+}
+
+sub worker {
+ my $tid = threads->tid;
+ diag "spawned thread $tid";
+ tick;
+ for (1 .. 10) {
+  pass "test $_ in thread $tid";
+  tick;
+ }
+}
+
+$_->join for map threads->create(\&worker), 1 .. 8;