Commit | Line | Data |
27a701f9 |
1 | package #hide from pause |
2 | DBICTest::BaseSchema; |
3 | |
4 | use strict; |
5 | use warnings; |
bedbc811 |
6 | use base qw(DBICTest::Base DBIx::Class::Schema); |
27a701f9 |
7 | |
e952df76 |
8 | use Fcntl qw(:DEFAULT :seek :flock); |
9 | use Time::HiRes 'sleep'; |
bbf6a9a5 |
10 | use DBIx::Class::_Util 'scope_guard'; |
e952df76 |
11 | use DBICTest::Util::LeakTracer qw(populate_weakregistry assert_empty_weakregistry); |
439a7283 |
12 | use DBICTest::Util qw( local_umask tmpdir await_flock dbg DEBUG_TEST_CONCURRENCY_LOCKS ); |
e952df76 |
13 | use namespace::clean; |
14 | |
ddcc02d1 |
15 | if( $ENV{DBICTEST_ASSERT_NO_SPURIOUS_EXCEPTION_ACTION} ) { |
16 | __PACKAGE__->exception_action( sub { |
17 | |
18 | my ( $fr_num, $disarmed, $throw_exception_fr_num ); |
19 | while( ! $disarmed and my @fr = caller(++$fr_num) ) { |
20 | |
21 | $throw_exception_fr_num ||= ( |
22 | $fr[3] eq 'DBIx::Class::ResultSource::throw_exception' |
23 | and |
24 | $fr_num |
25 | ); |
26 | |
27 | $disarmed = !! ( |
28 | $fr[1] =~ / \A (?: \. [\/\\] )? x?t [\/\\] .+ \.t \z /x |
29 | and |
30 | ( |
31 | $fr[3] =~ /\A (?: |
32 | Test::Exception::throws_ok |
33 | | |
34 | Test::Exception::dies_ok |
35 | | |
36 | Try::Tiny::try |
37 | | |
38 | \Q(eval)\E |
39 | ) \z /x |
40 | or |
41 | ( |
42 | $fr[3] eq 'Test::Exception::lives_ok' |
43 | and |
44 | ( $::TODO or Test::Builder->new->in_todo ) |
45 | ) |
46 | ) |
47 | ); |
48 | } |
49 | |
50 | Test::Builder->new->ok(0, join "\n", |
51 | 'Unexpected &exception_action invocation', |
52 | '', |
53 | ' You almost certainly used eval/try instead of dbic_internal_try()', |
54 | " Adjust *one* of the eval-ish constructs in the callstack starting" . DBICTest::Util::stacktrace($throw_exception_fr_num||()) |
55 | ) unless $disarmed; |
56 | |
57 | DBIx::Class::Exception->throw( $_[0] ); |
58 | }) |
59 | } |
60 | |
2cfc22dd |
61 | sub capture_executed_sql_bind { |
62 | my ($self, $cref) = @_; |
63 | |
64 | $self->throw_exception("Expecting a coderef to run") unless ref $cref eq 'CODE'; |
65 | |
4faaf174 |
66 | require DBICTest::SQLTracerObj; |
67 | |
2cfc22dd |
68 | # hack around stupid, stupid API |
69 | no warnings 'redefine'; |
70 | local *DBIx::Class::Storage::DBI::_format_for_trace = sub { $_[1] }; |
71 | Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO; |
72 | |
b74b15b0 |
73 | # can not use local() due to an unknown number of storages |
74 | # (think replicated) |
75 | my $orig_states = { map |
76 | { $_ => $self->storage->$_ } |
77 | qw(debugcb debugobj debug) |
78 | }; |
4faaf174 |
79 | |
bbf6a9a5 |
80 | my $sg = scope_guard { |
b74b15b0 |
81 | $self->storage->$_ ( $orig_states->{$_} ) for keys %$orig_states; |
bbf6a9a5 |
82 | }; |
b74b15b0 |
83 | |
84 | $self->storage->debugcb(undef); |
85 | $self->storage->debugobj( my $tracer_obj = DBICTest::SQLTracerObj->new ); |
86 | $self->storage->debug(1); |
2cfc22dd |
87 | |
49eeb48d |
88 | local $Test::Builder::Level = $Test::Builder::Level + 2; |
2cfc22dd |
89 | $cref->(); |
90 | |
91 | return $tracer_obj->{sqlbinds} || []; |
92 | } |
93 | |
49eeb48d |
94 | sub is_executed_querycount { |
95 | my ($self, $cref, $exp_counts, $msg) = @_; |
96 | |
97 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
98 | |
99 | $self->throw_exception("Expecting an hashref of counts or an integer representing total query count") |
100 | unless ref $exp_counts eq 'HASH' or (defined $exp_counts and ! ref $exp_counts); |
101 | |
102 | my @got = map { $_->[0] } @{ $self->capture_executed_sql_bind($cref) }; |
103 | |
104 | return Test::More::is( @got, $exp_counts, $msg ) |
105 | unless ref $exp_counts; |
106 | |
107 | my $got_counts = { map { $_ => 0 } keys %$exp_counts }; |
108 | $got_counts->{$_}++ for @got; |
109 | |
110 | return Test::More::is_deeply( |
111 | $got_counts, |
112 | $exp_counts, |
113 | $msg, |
114 | ); |
115 | } |
116 | |
2cfc22dd |
117 | sub is_executed_sql_bind { |
118 | my ($self, $cref, $sqlbinds, $msg) = @_; |
119 | |
120 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
121 | |
122 | $self->throw_exception("Expecting an arrayref of SQL/Bind pairs") unless ref $sqlbinds eq 'ARRAY'; |
123 | |
124 | my @expected = @$sqlbinds; |
125 | |
126 | my @got = map { $_->[1] } @{ $self->capture_executed_sql_bind($cref) }; |
127 | |
128 | |
129 | return Test::Builder->new->ok(1, $msg || "No queries executed while running $cref") |
130 | if !@got and !@expected; |
131 | |
132 | require SQL::Abstract::Test; |
133 | my $ret = 1; |
134 | while (@expected or @got) { |
135 | my $left = shift @got; |
136 | my $right = shift @expected; |
137 | |
138 | # allow the right side to "simplify" the entire shebang |
139 | if ($left and $right) { |
140 | $left = [ @$left ]; |
141 | for my $i (1..$#$right) { |
142 | if ( |
143 | ! ref $right->[$i] |
144 | and |
145 | ref $left->[$i] eq 'ARRAY' |
146 | and |
147 | @{$left->[$i]} == 2 |
148 | ) { |
149 | $left->[$i] = $left->[$i][1] |
150 | } |
151 | } |
152 | } |
153 | |
154 | $ret &= SQL::Abstract::Test::is_same_sql_bind( |
155 | \( $left || [] ), |
156 | \( $right || [] ), |
157 | $msg, |
158 | ); |
159 | } |
160 | |
161 | return $ret; |
162 | } |
163 | |
e952df76 |
164 | our $locker; |
165 | END { |
166 | # we need the $locker to be referenced here for delayed destruction |
167 | if ($locker->{lock_name} and ($ENV{DBICTEST_LOCK_HOLDER}||0) == $$) { |
69016f65 |
168 | DEBUG_TEST_CONCURRENCY_LOCKS |
169 | and dbg "$locker->{type} LOCK RELEASED (END): $locker->{lock_name}"; |
e952df76 |
170 | } |
171 | } |
172 | |
173 | my $weak_registry = {}; |
174 | |
175 | sub connection { |
176 | my $self = shift->next::method(@_); |
177 | |
178 | # MASSIVE FIXME |
179 | # we can't really lock based on DSN, as we do not yet have a way to tell that e.g. |
180 | # DBICTEST_MSSQL_DSN=dbi:Sybase:server=192.168.0.11:1433;database=dbtst |
181 | # and |
182 | # DBICTEST_MSSQL_ODBC_DSN=dbi:ODBC:server=192.168.0.11;port=1433;database=dbtst;driver=FreeTDS;tds_version=8.0 |
183 | # are the same server |
184 | # hence we lock everything based on sqlt_type or just globally if not available |
185 | # just pretend we are python you know? :) |
186 | |
187 | |
188 | # when we get a proper DSN resolution sanitize to produce a portable lockfile name |
189 | # this may look weird and unnecessary, but consider running tests from |
190 | # windows over a samba share >.> |
191 | #utf8::encode($dsn); |
192 | #$dsn =~ s/([^A-Za-z0-9_\-\.\=])/ sprintf '~%02X', ord($1) /ge; |
193 | #$dsn =~ s/^dbi/dbi/i; |
194 | |
195 | # provide locking for physical (non-memory) DSNs, so that tests can |
196 | # safely run in parallel. While the harness (make -jN test) does set |
197 | # an envvar, we can not detect when a user invokes prove -jN. Hence |
198 | # perform the locking at all times, it shouldn't hurt. |
199 | # the lock fh *should* inherit across forks/subprocesses |
e952df76 |
200 | if ( |
201 | ! $DBICTest::global_exclusive_lock |
202 | and |
203 | ( ! $ENV{DBICTEST_LOCK_HOLDER} or $ENV{DBICTEST_LOCK_HOLDER} == $$ ) |
204 | and |
205 | ref($_[0]) ne 'CODE' |
206 | and |
207 | ($_[0]||'') !~ /^ (?i:dbi) \: SQLite \: (?: dbname\= )? (?: \:memory\: | t [\/\\] var [\/\\] DBIxClass\-) /x |
208 | ) { |
209 | |
0f6d86e4 |
210 | my $locktype; |
211 | |
212 | { |
e952df76 |
213 | # guard against infinite recursion |
214 | local $ENV{DBICTEST_LOCK_HOLDER} = -1; |
215 | |
0f6d86e4 |
216 | # we need to work with a forced fresh clone so that we do not upset any state |
e952df76 |
217 | # of the main $schema (some tests examine it quite closely) |
218 | local $SIG{__WARN__} = sub {}; |
219 | local $@; |
0f6d86e4 |
220 | |
221 | # this will either give us an undef $locktype or will determine things |
222 | # properly with a default ( possibly connecting in the process ) |
223 | eval { |
224 | my $s = ref($self)->connect(@{$self->storage->connect_info})->storage; |
225 | |
226 | $locktype = $s->sqlt_type || 'generic'; |
227 | |
228 | # in case sqlt_type did connect, doesn't matter if it fails or something |
229 | $s->disconnect; |
e952df76 |
230 | }; |
0f6d86e4 |
231 | } |
e952df76 |
232 | |
233 | # Never hold more than one lock. This solves the "lock in order" issues |
234 | # unrelated tests may have |
235 | # Also if there is no connection - there is no lock to be had |
236 | if ($locktype and (!$locker or $locker->{type} ne $locktype)) { |
237 | |
238 | # this will release whatever lock we may currently be holding |
239 | # which is fine since the type does not match as checked above |
69016f65 |
240 | DEBUG_TEST_CONCURRENCY_LOCKS |
241 | and $locker |
242 | and dbg "$locker->{type} LOCK RELEASED (UNDEF): $locker->{lock_name}"; |
243 | |
e952df76 |
244 | undef $locker; |
245 | |
439a7283 |
246 | my $lockpath = tmpdir . "_dbictest_$locktype.lock"; |
e952df76 |
247 | |
69016f65 |
248 | DEBUG_TEST_CONCURRENCY_LOCKS |
249 | and dbg "Waiting for $locktype LOCK: $lockpath..."; |
250 | |
e952df76 |
251 | my $lock_fh; |
252 | { |
253 | my $u = local_umask(0); # so that the file opens as 666, and any user can lock |
254 | sysopen ($lock_fh, $lockpath, O_RDWR|O_CREAT) or die "Unable to open $lockpath: $!"; |
255 | } |
69016f65 |
256 | |
630e2ea8 |
257 | await_flock ($lock_fh, LOCK_EX) or die "Unable to lock $lockpath: $!"; |
69016f65 |
258 | |
259 | DEBUG_TEST_CONCURRENCY_LOCKS |
260 | and dbg "Got $locktype LOCK: $lockpath"; |
e952df76 |
261 | |
262 | # see if anyone was holding a lock before us, and wait up to 5 seconds for them to terminate |
263 | # if we do not do this we may end up trampling over some long-running END or somesuch |
264 | seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!"; |
265 | my $old_pid; |
266 | if ( |
267 | read ($lock_fh, $old_pid, 100) |
268 | and |
269 | ($old_pid) = $old_pid =~ /^(\d+)$/ |
270 | ) { |
69016f65 |
271 | DEBUG_TEST_CONCURRENCY_LOCKS |
272 | and dbg "Post-grab WAIT for $old_pid START: $lockpath"; |
273 | |
e952df76 |
274 | for (1..50) { |
275 | kill (0, $old_pid) or last; |
276 | sleep 0.1; |
277 | } |
69016f65 |
278 | |
279 | DEBUG_TEST_CONCURRENCY_LOCKS |
280 | and dbg "Post-grab WAIT for $old_pid FINISHED: $lockpath"; |
e952df76 |
281 | } |
e952df76 |
282 | |
283 | truncate $lock_fh, 0; |
284 | seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!"; |
285 | $lock_fh->autoflush(1); |
286 | print $lock_fh $$; |
287 | |
288 | $ENV{DBICTEST_LOCK_HOLDER} ||= $$; |
289 | |
290 | $locker = { |
291 | type => $locktype, |
292 | fh => $lock_fh, |
293 | lock_name => "$lockpath", |
294 | }; |
295 | } |
296 | } |
297 | |
298 | if ($INC{'Test/Builder.pm'}) { |
299 | populate_weakregistry ( $weak_registry, $self->storage ); |
300 | |
301 | my $cur_connect_call = $self->storage->on_connect_call; |
302 | |
303 | $self->storage->on_connect_call([ |
304 | (ref $cur_connect_call eq 'ARRAY' |
305 | ? @$cur_connect_call |
306 | : ($cur_connect_call || ()) |
307 | ), |
308 | [sub { |
309 | populate_weakregistry( $weak_registry, shift->_dbh ) |
310 | }], |
311 | ]); |
312 | } |
313 | |
314 | return $self; |
315 | } |
316 | |
317 | sub clone { |
318 | my $self = shift->next::method(@_); |
319 | populate_weakregistry ( $weak_registry, $self ) |
320 | if $INC{'Test/Builder.pm'}; |
321 | $self; |
322 | } |
323 | |
324 | END { |
961d79db |
325 | # Make sure we run after any cleanup in other END blocks |
961d79db |
326 | push @{ B::end_av()->object_2svref }, sub { |
327 | assert_empty_weakregistry($weak_registry, 'quiet'); |
328 | }; |
e952df76 |
329 | } |
27a701f9 |
330 | |
331 | 1; |