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