]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/lib/indirect/TestThreads.pm
Make Perl version numbers more readable
[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 use VPIT::TestHelpers;
9
10 sub import {
11  shift;
12
13  require indirect;
14
15  skip_all 'This indirect isn\'t thread safe' unless indirect::I_THREADSAFE();
16
17  my $force = $ENV{PERL_INDIRECT_TEST_THREADS} ? 1 : !1;
18  skip_all 'This perl wasn\'t built to support threads'
19                                                     unless $Config{useithreads};
20  skip_all 'perl 5.13.4 required to test thread safety'
21                                              unless $force or "$]" >= 5.013_004;
22
23  load_or_skip_all('threads', $force ? '0' : '1.67', [ ]);
24
25  my %exports = (
26   spawn => \&spawn,
27  );
28
29  my $pkg = caller;
30  while (my ($name, $code) = each %exports) {
31   no strict 'refs';
32   *{$pkg.'::'.$name} = $code;
33  }
34 }
35
36 sub spawn {
37  local $@;
38  my @diag;
39  my $thread = eval {
40   local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
41   threads->create(@_);
42  };
43  push @diag, "Thread creation error: $@" if $@;
44  if (@diag) {
45   require Test::Leaner;
46   Test::Leaner::diag($_) for @diag;
47  }
48  return $thread ? $thread : ();
49 }
50
51 1;