blib/lib/DBIO/Test/Util.pm

filter: Statementstmt 74.2% 78 / 105 Branchbran 39.5% 19 / 48 Conditioncond 42.8% 9 / 21 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 85.7% 18 / 21 Podpod 0.0% 0 / 6 totaltotal 59.3% 127 / 214 CC 36 SCAR 31.8
CC
36
Cov
61%
check_customcond_args
58.5
await_flock
20.0
local_umask
20.0
CRAP 23.9
« blib/lib/DBIO/SQLMaker/ClassicExtensions.pm ↑ Summary blib/lib/DBIO/Generate/Style/Candy.pm »
1 package DBIO::Test::Util;
2 # ABSTRACT: Utility functions for DBIO test suite
3  
4 257 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 257 our $VERSION = '0.900007';
7  
8 257 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 257 ▶use DBIO::Test::Util::UmaskGuard;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # this noop trick initializes the STDOUT, so that the TAP::Harness
13 # issued IO::Select->can_read calls (which are blocking wtf wtf wtf)
14 # keep spinning and scheduling jobs
15 # This results in an overall much smoother job-queue drainage, since
16 # the Harness blocks less
17 # (ideally this needs to be addressed in T::H, but a quick patchjob
18 # broke everything so tabling it for now)
19 257 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
20 257 ▶ if ($INC{'Test/Builder.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Test/Builder.pm"}) 253 4 257
21 253 local $| = 1;
22 253 print "#\n";
23 }
24 }
25  
26 257 ▶use constant DEBUG_TEST_CONCURRENCY_LOCKS =>
Subroutine BEGIN: called CC=3 SCAR=11.0
27 257 ▶ ( ($ENV{DBIO_TEST_DEBUG_CONCURRENCY_LOCKS}||'') =~ /^(\d+)$/ )[0]
Condition100%4/4
(($ENV{'DBIO_TEST_DEBUG_CONCURRENCY_LOCKS'} || '') =~ /^(\d+)$/)[0] || 0
l!l
1256
$ENV{'DBIO_TEST_DEBUG_CONCURRENCY_LOCKS'} || ''
l!l
1256
MC/DC100%1/1
(($ENV{'DBIO_TEST_DEBUG_CONCURRENCY_LOCKS'} || '') =~ /^(\d+)$/)[0] || 0
(($ENV{'DBIO_TEST_DEBUG_CONCURRENCY_LOCKS'} || '') =~ /^(\d+)$/)[0]
MC/DC100%1/1
$ENV{'DBIO_TEST_DEBUG_CONCURRENCY_LOCKS'} || ''
$ENV{'DBIO_TEST_DEBUG_CONCURRENCY_LOCKS'}
Truth table100%2/2
A
A= (($ENV{'DBIO_TEST_DEBUG_CONCURRENCY_LOCKS'} || '') =~ /^(\d+)$/)[0]
Aresult
11
00
Truth table100%2/2
A
A= $ENV{'DBIO_TEST_DEBUG_CONCURRENCY_LOCKS'}
Aresult
11
00
28 ||
29 0
30 257 ;
31  
32 257 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 257 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 257 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 257 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 257 ▶use DBIO::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 257 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 257 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 257 our @EXPORT_OK = qw(
41 dbg stacktrace
42 local_umask
43 visit_namespaces
44 check_customcond_args
45 await_flock DEBUG_TEST_CONCURRENCY_LOCKS
46 );
47  
48 257 if (DEBUG_TEST_CONCURRENCY_LOCKS) {
49 require DBI;
50 my $oc = DBI->can('connect');
51 257 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶ *DBI::connect = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 1 DBIO::Test::Util::dbg(
54 'Connecting to ' . DBIO::Util::mask_connect_secrets($_[1], [ @_[1 .. $#_] ])
55 );
56 1 goto $oc;
57 }
58 }
59  
60 1 ▶sub dbg ($) {
Subroutine dbg: called CC=1 SCAR=0.0
Pod undocumented
61 1 require Time::HiRes;
62 1 printf STDERR "\n%.06f %5s %-78s %s\n",
63 scalar Time::HiRes::time(),
64 $$,
65 $_[0],
66 $0,
67 ;
68 }
69  
70 # File locking is hard. Really hard. By far the best lock implementation
71 # I've seen is part of the guts of File::Temp. However it is sadly not
72 # reusable. Since I am not aware of folks doing NFS parallel testing,
73 # nor are we known to work on VMS, I am just going to punt this and
74 # use the portable-ish flock() provided by perl itself. If this does
75 # not work for you - patches more than welcome.
76 #
77 # This figure esentially means "how long can a single test hold a
78 # resource before everyone else gives up waiting and aborts" or
79 # in other words "how long does the longest test-group legitimally run?"
80 257 my $lock_timeout_minutes = 15; # yes, that's long, I know
81 257 my $wait_step_seconds = 0.25;
82  
83 0 ▶sub await_flock ($$) {
Subroutine await_flock: not called CC=4 SCAR=30.0
Pod undocumented
84 0 my ($fh, $locktype) = @_;
85  
86 0 my ($res, $tries);
87 0 ▶ while(
Branch: 1 branch
BranchTrueFalseTotal
if (not $res = flock($fh, $locktype | 4) and ++$tries <= $lock_timeout_minutes * 60 / $wait_step_seconds) 0 0 0
Condition0%0/3
not $res = flock($fh, $locktype | 4) and ++$tries <= $lock_timeout_minutes * 60 / $wait_step_seconds
!ll&&!rl&&r
000
MC/DC0%0/2
not $res = flock($fh, $locktype | 4) and ++$tries <= $lock_timeout_minutes * 60 / $wait_step_seconds
not $res = flock($fh, $locktype | 4) ++$tries <= $lock_timeout_minutes * 60 / $wait_step_seconds
Truth table0%0/3
not(A) and B
A= not $res = flock($fh, $locktype | 4)B= ++$tries <= $lock_timeout_minutes * 60 / $wait_step_seconds
ABresult
0X0
100
111
88 ! ( $res = flock( $fh, $locktype | LOCK_NB ) )
89 and
90 ++$tries <= $lock_timeout_minutes * 60 / $wait_step_seconds
91 ) {
92 0 select( undef, undef, undef, $wait_step_seconds );
93  
94 # "say something" every 10 cycles to work around RT#108390
95 # jesus christ our tooling is such a crock of shit :(
96 0 ▶ unless ( $tries % 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tries % 10) 0 0 0
97  
98 # Turning on autoflush is crucial: if stars align just right buffering
99 # will ensure we never actually call write() underneath until the grand
100 # timeout is reached (and that's too long). Reproducible via
101 #
102 # DBIO_TEST_VERSION_WARNS_INDISCRIMINATELY=1 \
103 # DBIO_TEST_RUN_ALL_TESTS=1 \
104 # strace -f \
105 # prove -lj10 xt/extra/internals/
106 #
107 0 select( ( select(\*STDOUT), $|=1 )[0] );
108  
109 0 print "#\n";
110 }
111 }
112  
113 0 return $res;
114 }
115  
116 0 ▶sub local_umask {
Subroutine local_umask: not called CC=4 SCAR=30.0
Pod undocumented
117 0 ▶ return unless defined $Config{d_umask};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $DBIO::Test::Util::Config{"d_umask"} 0 0 0
118  
119 0 ▶ die 'Calling local_umask() in void context makes no sense'
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
120 if ! defined wantarray;
121  
122 0 my $old_umask = umask(shift());
123 0 ▶ die "Setting umask failed: $!" unless defined $old_umask;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old_umask 0 0 0
124  
125 0 return bless \$old_umask, 'DBIO::Test::Util::UmaskGuard';
126 }
127  
128 0 ▶sub stacktrace {
Subroutine stacktrace: not called CC=3 SCAR=24.8
Pod undocumented
129 0 my $frame = shift;
130 0 $frame++;
131 0 my (@stack, @frame);
132  
133 0 ▶ while (@frame = caller($frame++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@frame = caller $frame++) 0 0 0
134 0 push @stack, [@frame[3,1,2]];
135 }
136  
137 0 ▶ return undef unless @stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @stack 0 0 0
138  
139 0 $stack[0][0] = '';
140 0 return join "\tinvoked as ", map { sprintf ("%s at %s line %d\n", @$_ ) } @stack;
141 }
142  
143 1 ▶sub check_customcond_args ($) {
Subroutine check_customcond_args: called CC=18 SCAR=40.7
Pod undocumented
144 1 my $args = shift;
145  
146 1 ▶ confess "Expecting a hashref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 1 1
147 unless ref $args eq 'HASH';
148  
149 1 for (qw(rel_name foreign_relname self_alias foreign_alias)) {
150 confess "Custom condition argument '$_' must be a plain string"
151 4 ▶ if length ref $args->{$_} or ! length $args->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if length ref $args->{$_} or not length $args->{$_} 0 4 4
Condition33%1/3
length ref $args->{$_} or not length $args->{$_}
l!l&&r!l&&!r
004
MC/DC0%0/2
length ref $args->{$_} or not length $args->{$_}
length ref $args->{$_} not length $args->{$_}
Truth table33%1/3
A or not(B)
A= length ref $args->{$_}B= not length $args->{$_}
ABresult
1X1
011
000
152 }
153  
154 confess "Current and legacy rel_name arguments do not match"
155 1 ▶ if $args->{rel_name} ne $args->{foreign_relname};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"rel_name"} ne $args->{"foreign_relname"} 0 1 1
156  
157 confess "Custom condition argument 'self_resultsource' must be a rsrc instance"
158 1 ▶ unless defined blessed $args->{self_resultsource} and $args->{self_resultsource}->isa('DBIO::ResultSource');
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($args->{"self_resultsource"}) and $args->{"self_resultsource"}->isa("DBIO::ResultSource") 0 1 1
Condition33%1/3
defined builtin::blessed($args->{"self_resultsource"}) and $args->{"self_resultsource"}->isa("DBIO::ResultSource")
!ll&&!rl&&r
001
MC/DC0%0/2
defined builtin::blessed($args->{"self_resultsource"}) and $args->{"self_resultsource"}->isa("DBIO::ResultSource")
defined builtin::blessed($args->{"self_resultsource"}) $args->{"self_resultsource"}->isa("DBIO::ResultSource")
Truth table33%1/3
A and B
A= defined builtin::blessed($args->{"self_resultsource"})B= $args->{"self_resultsource"}->isa("DBIO::ResultSource")
ABresult
0X0
100
111
159  
160 confess "Passed resultsource has no record of the supplied rel_name - likely wrong \$rsrc"
161 1 ▶ unless ref $args->{self_resultsource}->relationship_info($args->{rel_name});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{"self_resultsource"}->relationship_info($args->{"rel_name"}) 0 1 1
162  
163 1 my $struct_cnt = 0;
164  
165 1 ▶ if (defined $args->{self_result_object} or defined $args->{self_rowobj} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"self_result_object"} or defined $args->{"self_rowobj"}) 0 1 1
Condition33%1/3
defined $args->{"self_result_object"} or defined $args->{"self_rowobj"}
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $args->{"self_result_object"} or defined $args->{"self_rowobj"}
defined $args->{"self_result_object"} defined $args->{"self_rowobj"}
Truth table33%1/3
A or B
A= defined $args->{"self_result_object"}B= defined $args->{"self_rowobj"}
ABresult
1X1
011
000
166 0 $struct_cnt++;
167 0 for (qw(self_result_object self_rowobj)) {
168 confess "Custom condition argument '$_' must be a result instance"
169 0 ▶ unless defined blessed $args->{$_} and $args->{$_}->isa('DBIO::Row');
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($args->{$_}) and $args->{$_}->isa("DBIO::Row") 0 0 0
Condition0%0/3
defined builtin::blessed($args->{$_}) and $args->{$_}->isa("DBIO::Row")
!ll&&!rl&&r
000
MC/DC0%0/2
defined builtin::blessed($args->{$_}) and $args->{$_}->isa("DBIO::Row")
defined builtin::blessed($args->{$_}) $args->{$_}->isa("DBIO::Row")
Truth table0%0/3
A and B
A= defined builtin::blessed($args->{$_})B= $args->{$_}->isa("DBIO::Row")
ABresult
0X0
100
111
170 }
171  
172 confess "Current and legacy self_result_object arguments do not match"
173 0 ▶ if refaddr($args->{self_result_object}) != refaddr($args->{self_rowobj});
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($args->{"self_result_object"}) != builtin::refaddr($args->{"self_rowobj"}) 0 0 0
174 }
175  
176 1 ▶ if (defined $args->{foreign_values}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"foreign_values"}) 1 0 1
177 1 $struct_cnt++;
178  
179 confess "Custom condition argument 'foreign_values' must be a hash reference"
180 1 ▶ unless ref $args->{foreign_values} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{"foreign_values"} eq "HASH" 0 1 1
181 }
182  
183 1 ▶ confess "Data structures supplied on both ends of a relationship"
Branch: 1 branch
BranchTrueFalseTotal
if $struct_cnt == 2 0 1 1
184 if $struct_cnt == 2;
185  
186 1 $args;
187 }
188  
189 991 ▶sub visit_namespaces {
Subroutine visit_namespaces: called CC=8 SCAR=20.9
Pod undocumented
190 0 ▶ my $args = { (ref $_[0]) ? %{$_[0]} : @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 991 991
191  
192 991 my $visited_count = 1;
193  
194 # A package and a namespace are subtly different things
195 991 ▶ $args->{package} ||= 'main';
Condition100%2/2
$args->{"package"} ||= "main"
l!l
9901
MC/DC100%1/1
$args->{"package"} ||= "main"
$args->{"package"}
Truth table100%2/2
A
A= $args->{"package"}
Aresult
11
00
196 991 ▶ $args->{package} = 'main' if $args->{package} =~ /^ :: (?: main )? $/x;
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"package"} =~ /^ :: (?: main )? $/x 0 991 991
197 991 $args->{package} =~ s/^:://;
198  
199 991 ▶ if ( $args->{action}->($args->{package}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"action"}->($args->{"package"})) 969 22 991
200 my $ns =
201 ( ($args->{package} eq 'main') ? '' : $args->{package} )
202 969 ▶ .
Branch: 1 branch
BranchTrueFalseTotal
$args->{'package'} eq 'main' ? : 1 968 969
203 '::'
204 ;
205  
206 969 $visited_count += visit_namespaces( %$args, package => $_ ) for
207 grep
208 # this happens sometimes on %:: traversal
209 991 { $_ ne '::main' }
210 map
211 21236 ▶ { $_ =~ /^(.+?)::$/ ? "$ns$1" : () }
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^(.+?)::$/ ? : 991 20245 21236
212 257 ▶ do { no strict 'refs'; keys %$ns }
Subroutine BEGIN: called CC=1 SCAR=0.0
213 ;
214 }
215  
216 991 return $visited_count;
217 }
218  
219 257 1;
220  
221 __END__
« blib/lib/DBIO/SQLMaker/ClassicExtensions.pm ↑ Summary blib/lib/DBIO/Generate/Style/Candy.pm »