1 package #hide from pause
6 use base qw(DBICTest::Base DBIx::Class::Schema);
8 use Fcntl qw(:DEFAULT :seek :flock);
9 use DBIx::Class::_Util 'scope_guard';
10 use DBICTest::Util::LeakTracer qw(populate_weakregistry assert_empty_weakregistry);
11 use DBICTest::Util qw( local_umask tmpdir await_flock dbg DEBUG_TEST_CONCURRENCY_LOCKS );
14 if( $ENV{DBICTEST_ASSERT_NO_SPURIOUS_EXCEPTION_ACTION} ) {
15 __PACKAGE__->exception_action( sub {
17 my ( $fr_num, $disarmed, $throw_exception_fr_num );
18 while( ! $disarmed and my @fr = caller(++$fr_num) ) {
20 $throw_exception_fr_num ||= (
21 $fr[3] eq 'DBIx::Class::ResultSource::throw_exception'
27 $fr[1] =~ / \A (?: \. [\/\\] )? x?t [\/\\] .+ \.t \z /x
31 Test::Exception::throws_ok
33 Test::Exception::dies_ok
41 $fr[3] eq 'Test::Exception::lives_ok'
43 ( $::TODO or Test::Builder->new->in_todo )
49 Test::Builder->new->ok(0, join "\n",
50 'Unexpected &exception_action invocation',
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||())
56 DBIx::Class::Exception->throw( $_[0] );
60 sub capture_executed_sql_bind {
61 my ($self, $cref) = @_;
63 $self->throw_exception("Expecting a coderef to run") unless ref $cref eq 'CODE';
65 require DBICTest::SQLTracerObj;
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;
72 # can not use local() due to an unknown number of storages
74 my $orig_states = { map
75 { $_ => $self->storage->$_ }
76 qw(debugcb debugobj debug)
79 my $sg = scope_guard {
80 $self->storage->$_ ( $orig_states->{$_} ) for keys %$orig_states;
83 $self->storage->debugcb(undef);
84 $self->storage->debugobj( my $tracer_obj = DBICTest::SQLTracerObj->new );
85 $self->storage->debug(1);
87 local $Test::Builder::Level = $Test::Builder::Level + 2;
90 return $tracer_obj->{sqlbinds} || [];
93 sub is_executed_querycount {
94 my ($self, $cref, $exp_counts, $msg) = @_;
96 local $Test::Builder::Level = $Test::Builder::Level + 1;
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);
101 my @got = map { $_->[0] } @{ $self->capture_executed_sql_bind($cref) };
103 return Test::More::is( @got, $exp_counts, $msg )
104 unless ref $exp_counts;
106 my $got_counts = { map { $_ => 0 } keys %$exp_counts };
107 $got_counts->{$_}++ for @got;
109 return Test::More::is_deeply(
116 sub is_executed_sql_bind {
117 my ($self, $cref, $sqlbinds, $msg) = @_;
119 local $Test::Builder::Level = $Test::Builder::Level + 1;
121 $self->throw_exception("Expecting an arrayref of SQL/Bind pairs") unless ref $sqlbinds eq 'ARRAY';
123 my @expected = @$sqlbinds;
125 my @got = map { $_->[1] } @{ $self->capture_executed_sql_bind($cref) };
128 return Test::Builder->new->ok(1, $msg || "No queries executed while running $cref")
129 if !@got and !@expected;
131 require SQL::Abstract::Test;
133 while (@expected or @got) {
134 my $left = shift @got;
135 my $right = shift @expected;
137 # allow the right side to "simplify" the entire shebang
138 if ($left and $right) {
140 for my $i (1..$#$right) {
144 ref $left->[$i] eq 'ARRAY'
148 $left->[$i] = $left->[$i][1]
153 $ret &= SQL::Abstract::Test::is_same_sql_bind(
165 # we need the $locker to be referenced here for delayed destruction
166 if ($locker->{lock_name} and ($ENV{DBICTEST_LOCK_HOLDER}||0) == $$) {
167 DEBUG_TEST_CONCURRENCY_LOCKS
168 and dbg "$locker->{type} LOCK RELEASED (END): $locker->{lock_name}";
172 my $weak_registry = {};
175 my $self = shift->next::method(@_);
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
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? :)
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 >.>
191 #$dsn =~ s/([^A-Za-z0-9_\-\.\=])/ sprintf '~%02X', ord($1) /ge;
192 #$dsn =~ s/^dbi/dbi/i;
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
200 ! $DBICTest::global_exclusive_lock
202 ( ! $ENV{DBICTEST_LOCK_HOLDER} or $ENV{DBICTEST_LOCK_HOLDER} == $$ )
206 ($_[0]||'') !~ /^ (?i:dbi) \: SQLite \: (?: dbname\= )? (?: \:memory\: | t [\/\\] var [\/\\] DBIxClass\-) /x
212 # guard against infinite recursion
213 local $ENV{DBICTEST_LOCK_HOLDER} = -1;
215 # we need to work with a forced fresh clone so that we do not upset any state
216 # of the main $schema (some tests examine it quite closely)
217 local $SIG{__WARN__} = sub {};
220 # this will either give us an undef $locktype or will determine things
221 # properly with a default ( possibly connecting in the process )
223 my $s = ref($self)->connect(@{$self->storage->connect_info})->storage;
225 $locktype = $s->sqlt_type || 'generic';
227 # in case sqlt_type did connect, doesn't matter if it fails or something
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)) {
237 # this will release whatever lock we may currently be holding
238 # which is fine since the type does not match as checked above
239 DEBUG_TEST_CONCURRENCY_LOCKS
241 and dbg "$locker->{type} LOCK RELEASED (UNDEF): $locker->{lock_name}";
245 my $lockpath = tmpdir . "_dbictest_$locktype.lock";
247 DEBUG_TEST_CONCURRENCY_LOCKS
248 and dbg "Waiting for $locktype LOCK: $lockpath...";
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: $!";
256 await_flock ($lock_fh, LOCK_EX) or die "Unable to lock $lockpath: $!";
258 DEBUG_TEST_CONCURRENCY_LOCKS
259 and dbg "Got $locktype LOCK: $lockpath";
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 $!";
266 read ($lock_fh, $old_pid, 100)
268 ($old_pid) = $old_pid =~ /^(\d+)$/
270 DEBUG_TEST_CONCURRENCY_LOCKS
271 and dbg "Post-grab WAIT for $old_pid START: $lockpath";
274 kill (0, $old_pid) or last;
275 select( undef, undef, undef, 0.1 );
278 DEBUG_TEST_CONCURRENCY_LOCKS
279 and dbg "Post-grab WAIT for $old_pid FINISHED: $lockpath";
282 truncate $lock_fh, 0;
283 seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!";
284 $lock_fh->autoflush(1);
287 $ENV{DBICTEST_LOCK_HOLDER} ||= $$;
292 lock_name => "$lockpath",
297 if ($INC{'Test/Builder.pm'}) {
298 populate_weakregistry ( $weak_registry, $self->storage );
300 my $cur_connect_call = $self->storage->on_connect_call;
302 $self->storage->on_connect_call([
303 (ref $cur_connect_call eq 'ARRAY'
305 : ($cur_connect_call || ())
308 populate_weakregistry( $weak_registry, shift->_dbh )
317 my $self = shift->next::method(@_);
318 populate_weakregistry ( $weak_registry, $self )
319 if $INC{'Test/Builder.pm'};
324 # Make sure we run after any cleanup in other END blocks
325 push @{ B::end_av()->object_2svref }, sub {
326 assert_empty_weakregistry($weak_registry, 'quiet');