]> git.vpit.fr Git - perl/modules/autovivification.git/blob - t/lib/autovivification/TestThreads.pm
Threads tests may not be able to spawn all the threads
[perl/modules/autovivification.git] / t / lib / autovivification / TestThreads.pm
1 package autovivification::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 autovivification;
23
24  skipall 'This autovivification isn\'t thread safe'
25                                         unless autovivification::A_THREADSAFE();
26
27  my $force = $ENV{PERL_AUTOVIVIFICATION_TEST_THREADS} ? 1 : !1;
28  skipall 'This perl wasn\'t built to support threads'
29                                                     unless $Config{useithreads};
30  skipall 'perl 5.13.4 required to test thread safety'
31                                               unless $force or "$]" >= 5.013004;
32
33  my $t_v = $force ? '0' : '1.67';
34  my $has_threads =  do {
35   local $@;
36   eval "use threads $t_v; 1";
37  };
38  skipall "threads $t_v required to test thread safety" unless $has_threads;
39
40  defined and diag "Using threads $_" for $threads::VERSION;
41
42  my %exports = (
43   spawn => \&spawn,
44  );
45
46  my $pkg = caller;
47  while (my ($name, $code) = each %exports) {
48   no strict 'refs';
49   *{$pkg.'::'.$name} = $code;
50  }
51 }
52
53 sub spawn {
54  local $@;
55  my @diag;
56  my $thread = eval {
57   local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
58   threads->create(@_);
59  };
60  push @diag, "Thread creation error: $@" if $@;
61  if (@diag) {
62   require Test::Leaner;
63   Test::Leaner::diag($_) for @diag;
64  }
65  return $thread ? $thread : ();
66 }
67
68 1;