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