Commit | Line | Data |
65d35121 |
1 | package DBICTest::Util; |
2 | |
3 | use warnings; |
4 | use strict; |
5 | |
bbcc1fe8 |
6 | # this noop trick initializes the STDOUT, so that the TAP::Harness |
7 | # issued IO::Select->can_read calls (which are blocking wtf wtf wtf) |
8 | # keep spinning and scheduling jobs |
9 | # This results in an overall much smoother job-queue drainage, since |
10 | # the Harness blocks less |
11 | # (ideally this needs to be addressed in T::H, but a quick patchjob |
12 | # broke everything so tabling it for now) |
13 | BEGIN { |
14 | if ($INC{'Test/Builder.pm'}) { |
15 | local $| = 1; |
16 | print "#\n"; |
17 | } |
18 | } |
19 | |
69016f65 |
20 | use constant DEBUG_TEST_CONCURRENCY_LOCKS => |
21 | ( ($ENV{DBICTEST_DEBUG_CONCURRENCY_LOCKS}||'') =~ /^(\d+)$/ )[0] |
22 | || |
23 | 0 |
24 | ; |
25 | |
8d6b1478 |
26 | use Config; |
bbf6a9a5 |
27 | use Carp qw(cluck confess croak); |
630e2ea8 |
28 | use Fcntl ':flock'; |
a446d7f8 |
29 | use Scalar::Util qw(blessed refaddr); |
bbf6a9a5 |
30 | use DBIx::Class::_Util 'scope_guard'; |
65d35121 |
31 | |
32 | use base 'Exporter'; |
69016f65 |
33 | our @EXPORT_OK = qw( |
34 | dbg stacktrace |
35 | local_umask |
36 | visit_namespaces |
37 | check_customcond_args |
630e2ea8 |
38 | await_flock DEBUG_TEST_CONCURRENCY_LOCKS |
69016f65 |
39 | ); |
40 | |
41 | if (DEBUG_TEST_CONCURRENCY_LOCKS) { |
42 | require DBI; |
43 | my $oc = DBI->can('connect'); |
44 | no warnings 'redefine'; |
45 | *DBI::connect = sub { |
46 | DBICTest::Util::dbg("Connecting to $_[1]"); |
47 | goto $oc; |
48 | } |
49 | } |
50 | |
51 | sub dbg ($) { |
52 | require Time::HiRes; |
53 | printf STDERR "\n%.06f %5s %-78s %s\n", |
54 | scalar Time::HiRes::time(), |
55 | $$, |
56 | $_[0], |
57 | $0, |
58 | ; |
59 | } |
8d6b1478 |
60 | |
630e2ea8 |
61 | # File locking is hard. Really hard. By far the best lock implementation |
62 | # I've seen is part of the guts of File::Temp. However it is sadly not |
63 | # reusable. Since I am not aware of folks doing NFS parallel testing, |
64 | # nor are we known to work on VMS, I am just going to punt this and |
65 | # use the portable-ish flock() provided by perl itself. If this does |
66 | # not work for you - patches more than welcome. |
67 | # |
68 | # This figure esentially means "how long can a single test hold a |
69 | # resource before everyone else gives up waiting and aborts" or |
70 | # in other words "how long does the longest test-group legitimally run?" |
71 | my $lock_timeout_minutes = 15; # yes, that's long, I know |
72 | my $wait_step_seconds = 0.25; |
73 | |
74 | sub await_flock ($$) { |
75 | my ($fh, $locktype) = @_; |
76 | |
77 | my ($res, $tries); |
78 | while( |
79 | ! ( $res = flock( $fh, $locktype | LOCK_NB ) ) |
80 | and |
81 | ++$tries <= $lock_timeout_minutes * 60 / $wait_step_seconds |
82 | ) { |
83 | select( undef, undef, undef, $wait_step_seconds ); |
84 | |
85 | # "say something" every 10 cycles to work around RT#108390 |
86 | # jesus christ our tooling is such a crock of shit :( |
87 | print "#\n" if not $tries % 10; |
88 | } |
89 | |
90 | return $res; |
91 | } |
92 | |
bbf6a9a5 |
93 | |
94 | sub local_umask ($) { |
8d6b1478 |
95 | return unless defined $Config{d_umask}; |
96 | |
bbf6a9a5 |
97 | croak 'Calling local_umask() in void context makes no sense' |
8d6b1478 |
98 | if ! defined wantarray; |
99 | |
bbf6a9a5 |
100 | my $old_umask = umask($_[0]); |
8d6b1478 |
101 | die "Setting umask failed: $!" unless defined $old_umask; |
102 | |
bbf6a9a5 |
103 | scope_guard(sub { |
104 | local ($@, $!, $?); |
105 | |
106 | eval { |
107 | defined(umask $old_umask) or die "nope"; |
108 | 1; |
109 | } or cluck ( |
110 | "Unable to reset old umask '$old_umask': " . ($! || 'Unknown error') |
111 | ); |
112 | }); |
8d6b1478 |
113 | } |
114 | |
65d35121 |
115 | sub stacktrace { |
116 | my $frame = shift; |
117 | $frame++; |
118 | my (@stack, @frame); |
119 | |
821edc09 |
120 | while (@frame = CORE::caller($frame++)) { |
65d35121 |
121 | push @stack, [@frame[3,1,2]]; |
122 | } |
123 | |
124 | return undef unless @stack; |
125 | |
126 | $stack[0][0] = ''; |
127 | return join "\tinvoked as ", map { sprintf ("%s at %s line %d\n", @$_ ) } @stack; |
128 | } |
129 | |
a3a17a15 |
130 | sub check_customcond_args ($) { |
131 | my $args = shift; |
132 | |
133 | confess "Expecting a hashref" |
134 | unless ref $args eq 'HASH'; |
135 | |
a446d7f8 |
136 | for (qw(rel_name foreign_relname self_alias foreign_alias)) { |
a3a17a15 |
137 | confess "Custom condition argument '$_' must be a plain string" |
138 | if length ref $args->{$_} or ! length $args->{$_}; |
139 | } |
140 | |
a446d7f8 |
141 | confess "Current and legacy rel_name arguments do not match" |
142 | if $args->{rel_name} ne $args->{foreign_relname}; |
143 | |
a3a17a15 |
144 | confess "Custom condition argument 'self_resultsource' must be a rsrc instance" |
145 | unless defined blessed $args->{self_resultsource} and $args->{self_resultsource}->isa('DBIx::Class::ResultSource'); |
146 | |
147 | confess "Passed resultsource has no record of the supplied rel_name - likely wrong \$rsrc" |
a446d7f8 |
148 | unless ref $args->{self_resultsource}->relationship_info($args->{rel_name}); |
149 | |
e884e5d9 |
150 | my $struct_cnt = 0; |
1adbd3fc |
151 | |
98def3ef |
152 | if (defined $args->{self_result_object} or defined $args->{self_rowobj} ) { |
e884e5d9 |
153 | $struct_cnt++; |
98def3ef |
154 | for (qw(self_result_object self_rowobj)) { |
a446d7f8 |
155 | confess "Custom condition argument '$_' must be a result instance" |
156 | unless defined blessed $args->{$_} and $args->{$_}->isa('DBIx::Class::Row'); |
157 | } |
a3a17a15 |
158 | |
98def3ef |
159 | confess "Current and legacy self_result_object arguments do not match" |
160 | if refaddr($args->{self_result_object}) != refaddr($args->{self_rowobj}); |
a3a17a15 |
161 | } |
162 | |
e884e5d9 |
163 | if (defined $args->{foreign_values}) { |
164 | $struct_cnt++; |
1adbd3fc |
165 | |
e884e5d9 |
166 | confess "Custom condition argument 'foreign_values' must be a hash reference" |
167 | unless ref $args->{foreign_values} eq 'HASH'; |
1adbd3fc |
168 | } |
169 | |
e884e5d9 |
170 | confess "Data structures supplied on both ends of a relationship" |
171 | if $struct_cnt == 2; |
1adbd3fc |
172 | |
a3a17a15 |
173 | $args; |
174 | } |
175 | |
c9abd679 |
176 | sub visit_namespaces { |
177 | my $args = { (ref $_[0]) ? %{$_[0]} : @_ }; |
178 | |
179 | my $visited_count = 1; |
180 | |
181 | # A package and a namespace are subtly different things |
182 | $args->{package} ||= 'main'; |
183 | $args->{package} = 'main' if $args->{package} =~ /^ :: (?: main )? $/x; |
184 | $args->{package} =~ s/^:://; |
185 | |
186 | if ( $args->{action}->($args->{package}) ) { |
187 | my $ns = |
188 | ( ($args->{package} eq 'main') ? '' : $args->{package} ) |
189 | . |
190 | '::' |
191 | ; |
192 | |
193 | $visited_count += visit_namespaces( %$args, package => $_ ) for |
194 | grep |
195 | # this happens sometimes on %:: traversal |
196 | { $_ ne '::main' } |
197 | map |
198 | { $_ =~ /^(.+?)::$/ ? "$ns$1" : () } |
199 | do { no strict 'refs'; keys %$ns } |
200 | ; |
201 | } |
202 | |
203 | return $visited_count; |
204 | } |
205 | |
65d35121 |
206 | 1; |