]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/lib/indirect/TestThreads.pm
Port threads test to Test::Leaner and move boilerplate to an helper module
[perl/modules/indirect.git] / t / lib / indirect / TestThreads.pm
1 package indirect::TestThreads;
2
3 use strict;
4 use warnings;
5
6 use Config qw<%Config>;
7
8 sub skipall {
9  my ($msg) = @_;
10  require Test::Leaner;
11  Test::Leaner::plan(skip_all => $msg);
12 }
13
14 sub diag {
15  require Test::Leaner;
16  Test::Leaner::diag(@_);
17 }
18
19 sub import {
20  shift;
21
22  require indirect;
23
24  skipall 'This indirect isn\'t thread safe' unless indirect::I_THREADSAFE();
25
26  my $force = $ENV{PERL_INDIRECT_TEST_THREADS} ? 1 : !1;
27  skipall 'This perl wasn\'t built to support threads'
28                                                     unless $Config{useithreads};
29  skipall 'perl 5.13.4 required to test thread safety'
30                                               unless $force or "$]" >= 5.013004;
31
32  my $t_v = $force ? '0' : '1.67';
33  my $has_threads =  do {
34   local $@;
35   eval "use threads $t_v; 1";
36  };
37  skipall "threads $t_v required to test thread safety" unless $has_threads;
38
39  defined and diag "Using threads $_" for $threads::VERSION;
40
41  my %exports = (
42   spawn => \&spawn,
43  );
44
45  my $pkg = caller;
46  while (my ($name, $code) = each %exports) {
47   no strict 'refs';
48   *{$pkg.'::'.$name} = $code;
49  }
50 }
51
52 sub spawn {
53  local $@;
54  my @diag;
55  my $thread = eval {
56   local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
57   threads->create(@_);
58  };
59  push @diag, "Thread creation error: $@" if $@;
60  if (@diag) {
61   require Test::Leaner;
62   Test::Leaner::diag($_) for @diag;
63  }
64  return $thread ? $thread : ();
65 }
66
67 1;