]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/lib/indirect/TestThreads.pm
Port module loading in tests to VPIT::TestHelpers
[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.013004;
22
23  load_or_skip('threads', $force ? '0' : '1.67', [ ],
24               'required to test thread safety');
25
26  my %exports = (
27   spawn => \&spawn,
28  );
29
30  my $pkg = caller;
31  while (my ($name, $code) = each %exports) {
32   no strict 'refs';
33   *{$pkg.'::'.$name} = $code;
34  }
35 }
36
37 sub spawn {
38  local $@;
39  my @diag;
40  my $thread = eval {
41   local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
42   threads->create(@_);
43  };
44  push @diag, "Thread creation error: $@" if $@;
45  if (@diag) {
46   require Test::Leaner;
47   Test::Leaner::diag($_) for @diag;
48  }
49  return $thread ? $thread : ();
50 }
51
52 1;