1 package #hide from pause
6 use base qw(DBICTest::Base DBIx::Class::Schema);
8 use Fcntl qw(:DEFAULT :seek :flock);
10 use DBIx::Class::_Util 'scope_guard';
11 use DBICTest::Util::LeakTracer qw(populate_weakregistry assert_empty_weakregistry);
12 use DBICTest::Util qw( local_umask tmpdir await_flock dbg DEBUG_TEST_CONCURRENCY_LOCKS );
15 if( $ENV{DBICTEST_ASSERT_NO_SPURIOUS_EXCEPTION_ACTION} ) {
16 my $ea = __PACKAGE__->exception_action( sub {
18 my ( $fr_num, $disarmed, $throw_exception_fr_num );
19 while( ! $disarmed and my @fr = caller(++$fr_num) ) {
21 $throw_exception_fr_num ||= (
22 $fr[3] eq 'DBIx::Class::ResultSource::throw_exception'
28 $fr[1] =~ / \A (?: \. [\/\\] )? x?t [\/\\] .+ \.t \z /x
32 Test::Exception::throws_ok
34 Test::Exception::dies_ok
42 $fr[3] eq 'Test::Exception::lives_ok'
44 ( $::TODO or Test::Builder->new->in_todo )
50 Test::Builder->new->ok(0, join "\n",
51 'Unexpected &exception_action invocation',
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||())
57 DBIx::Class::Exception->throw( $_[0] );
60 my $interesting_ns_rx = qr/^ (?: main$ | DBIx::Class:: | DBICTest:: ) /x;
62 # hard-set $SIG{__DIE__} to the class-wide exception_action
63 # with a little escape preceeding it
66 # without this there would be false positives everywhere :(
68 (caller(0))[0] !~ $interesting_ns_rx
73 (caller(1))[0] !~ $interesting_ns_rx
81 sub capture_executed_sql_bind {
82 my ($self, $cref) = @_;
84 $self->throw_exception("Expecting a coderef to run") unless ref $cref eq 'CODE';
86 require DBICTest::SQLTracerObj;
88 # hack around stupid, stupid API
89 no warnings 'redefine';
90 local *DBIx::Class::Storage::DBI::_format_for_trace = sub { $_[1] };
91 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
93 # can not use local() due to an unknown number of storages
95 my $orig_states = { map
96 { $_ => $self->storage->$_ }
97 qw(debugcb debugobj debug)
100 my $sg = scope_guard {
101 $self->storage->$_ ( $orig_states->{$_} ) for keys %$orig_states;
104 $self->storage->debugcb(undef);
105 $self->storage->debugobj( my $tracer_obj = DBICTest::SQLTracerObj->new );
106 $self->storage->debug(1);
108 local $Test::Builder::Level = $Test::Builder::Level + 2;
111 return $tracer_obj->{sqlbinds} || [];
114 sub is_executed_querycount {
115 my ($self, $cref, $exp_counts, $msg) = @_;
117 local $Test::Builder::Level = $Test::Builder::Level + 1;
119 $self->throw_exception("Expecting an hashref of counts or an integer representing total query count")
120 unless ref $exp_counts eq 'HASH' or (defined $exp_counts and ! ref $exp_counts);
122 my @got = map { $_->[0] } @{ $self->capture_executed_sql_bind($cref) };
124 return Test::More::is( @got, $exp_counts, $msg )
125 unless ref $exp_counts;
127 my $got_counts = { map { $_ => 0 } keys %$exp_counts };
128 $got_counts->{$_}++ for @got;
130 return Test::More::is_deeply(
137 sub is_executed_sql_bind {
138 my ($self, $cref, $sqlbinds, $msg) = @_;
140 local $Test::Builder::Level = $Test::Builder::Level + 1;
142 $self->throw_exception("Expecting an arrayref of SQL/Bind pairs") unless ref $sqlbinds eq 'ARRAY';
144 my @expected = @$sqlbinds;
146 my @got = map { $_->[1] } @{ $self->capture_executed_sql_bind($cref) };
149 return Test::Builder->new->ok(1, $msg || "No queries executed while running $cref")
150 if !@got and !@expected;
152 require SQL::Abstract::Test;
154 while (@expected or @got) {
155 my $left = shift @got;
156 my $right = shift @expected;
158 # allow the right side to "simplify" the entire shebang
159 if ($left and $right) {
161 for my $i (1..$#$right) {
165 ref $left->[$i] eq 'ARRAY'
169 $left->[$i] = $left->[$i][1]
174 $ret &= SQL::Abstract::Test::is_same_sql_bind(
186 # we need the $locker to be referenced here for delayed destruction
187 if ($locker->{lock_name} and ($ENV{DBICTEST_LOCK_HOLDER}||0) == $$) {
188 DEBUG_TEST_CONCURRENCY_LOCKS
189 and dbg "$locker->{type} LOCK RELEASED (END): $locker->{lock_name}";
193 my $weak_registry = {};
196 my $self = shift->next::method(@_);
199 # we can't really lock based on DSN, as we do not yet have a way to tell that e.g.
200 # DBICTEST_MSSQL_DSN=dbi:Sybase:server=192.168.0.11:1433;database=dbtst
202 # DBICTEST_MSSQL_ODBC_DSN=dbi:ODBC:server=192.168.0.11;port=1433;database=dbtst;driver=FreeTDS;tds_version=8.0
203 # are the same server
204 # hence we lock everything based on sqlt_type or just globally if not available
205 # just pretend we are python you know? :)
208 # when we get a proper DSN resolution sanitize to produce a portable lockfile name
209 # this may look weird and unnecessary, but consider running tests from
210 # windows over a samba share >.>
212 #$dsn =~ s/([^A-Za-z0-9_\-\.\=])/ sprintf '~%02X', ord($1) /ge;
213 #$dsn =~ s/^dbi/dbi/i;
215 # provide locking for physical (non-memory) DSNs, so that tests can
216 # safely run in parallel. While the harness (make -jN test) does set
217 # an envvar, we can not detect when a user invokes prove -jN. Hence
218 # perform the locking at all times, it shouldn't hurt.
219 # the lock fh *should* inherit across forks/subprocesses
221 ! $DBICTest::global_exclusive_lock
223 ( ! $ENV{DBICTEST_LOCK_HOLDER} or $ENV{DBICTEST_LOCK_HOLDER} == $$ )
227 ($_[0]||'') !~ /^ (?i:dbi) \: SQLite \: (?: dbname\= )? (?: \:memory\: | t [\/\\] var [\/\\] DBIxClass\-) /x
233 # guard against infinite recursion
234 local $ENV{DBICTEST_LOCK_HOLDER} = -1;
236 # we need to work with a forced fresh clone so that we do not upset any state
237 # of the main $schema (some tests examine it quite closely)
238 local $SIG{__WARN__} = sub {};
242 # this will either give us an undef $locktype or will determine things
243 # properly with a default ( possibly connecting in the process )
245 my $s = ref($self)->connect(@{$self->storage->connect_info})->storage;
247 $locktype = $s->sqlt_type || 'generic';
249 # in case sqlt_type did connect, doesn't matter if it fails or something
254 # Never hold more than one lock. This solves the "lock in order" issues
255 # unrelated tests may have
256 # Also if there is no connection - there is no lock to be had
257 if ($locktype and (!$locker or $locker->{type} ne $locktype)) {
259 # this will release whatever lock we may currently be holding
260 # which is fine since the type does not match as checked above
261 DEBUG_TEST_CONCURRENCY_LOCKS
263 and dbg "$locker->{type} LOCK RELEASED (UNDEF): $locker->{lock_name}";
267 my $lockpath = tmpdir . "_dbictest_$locktype.lock";
269 DEBUG_TEST_CONCURRENCY_LOCKS
270 and dbg "Waiting for $locktype LOCK: $lockpath...";
274 my $u = local_umask(0); # so that the file opens as 666, and any user can lock
275 sysopen ($lock_fh, $lockpath, O_RDWR|O_CREAT) or die "Unable to open $lockpath: $!";
278 await_flock ($lock_fh, LOCK_EX) or die "Unable to lock $lockpath: $!";
280 DEBUG_TEST_CONCURRENCY_LOCKS
281 and dbg "Got $locktype LOCK: $lockpath";
283 # see if anyone was holding a lock before us, and wait up to 5 seconds for them to terminate
284 # if we do not do this we may end up trampling over some long-running END or somesuch
285 seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!";
288 read ($lock_fh, $old_pid, 100)
290 ($old_pid) = $old_pid =~ /^(\d+)$/
292 DEBUG_TEST_CONCURRENCY_LOCKS
293 and dbg "Post-grab WAIT for $old_pid START: $lockpath";
296 kill (0, $old_pid) or last;
297 select( undef, undef, undef, 0.1 );
300 DEBUG_TEST_CONCURRENCY_LOCKS
301 and dbg "Post-grab WAIT for $old_pid FINISHED: $lockpath";
304 truncate $lock_fh, 0;
305 seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!";
306 $lock_fh->autoflush(1);
309 $ENV{DBICTEST_LOCK_HOLDER} ||= $$;
314 lock_name => "$lockpath",
319 if ($INC{'Test/Builder.pm'}) {
320 populate_weakregistry ( $weak_registry, $self->storage );
322 my $cur_connect_call = $self->storage->on_connect_call;
324 $self->storage->on_connect_call([
325 (ref $cur_connect_call eq 'ARRAY'
327 : ($cur_connect_call || ())
330 populate_weakregistry( $weak_registry, shift->_dbh )
339 my $self = shift->next::method(@_);
340 populate_weakregistry ( $weak_registry, $self )
341 if $INC{'Test/Builder.pm'};
346 # Make sure we run after any cleanup in other END blocks
347 push @{ B::end_av()->object_2svref }, sub {
348 assert_empty_weakregistry($weak_registry, 'quiet');