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 __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] );
61 sub capture_executed_sql_bind {
62 my ($self, $cref) = @_;
64 $self->throw_exception("Expecting a coderef to run") unless ref $cref eq 'CODE';
66 require DBICTest::SQLTracerObj;
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;
73 # can not use local() due to an unknown number of storages
75 my $orig_states = { map
76 { $_ => $self->storage->$_ }
77 qw(debugcb debugobj debug)
80 my $sg = scope_guard {
81 $self->storage->$_ ( $orig_states->{$_} ) for keys %$orig_states;
84 $self->storage->debugcb(undef);
85 $self->storage->debugobj( my $tracer_obj = DBICTest::SQLTracerObj->new );
86 $self->storage->debug(1);
88 local $Test::Builder::Level = $Test::Builder::Level + 2;
91 return $tracer_obj->{sqlbinds} || [];
94 sub is_executed_querycount {
95 my ($self, $cref, $exp_counts, $msg) = @_;
97 local $Test::Builder::Level = $Test::Builder::Level + 1;
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);
102 my @got = map { $_->[0] } @{ $self->capture_executed_sql_bind($cref) };
104 return Test::More::is( @got, $exp_counts, $msg )
105 unless ref $exp_counts;
107 my $got_counts = { map { $_ => 0 } keys %$exp_counts };
108 $got_counts->{$_}++ for @got;
110 return Test::More::is_deeply(
117 sub is_executed_sql_bind {
118 my ($self, $cref, $sqlbinds, $msg) = @_;
120 local $Test::Builder::Level = $Test::Builder::Level + 1;
122 $self->throw_exception("Expecting an arrayref of SQL/Bind pairs") unless ref $sqlbinds eq 'ARRAY';
124 my @expected = @$sqlbinds;
126 my @got = map { $_->[1] } @{ $self->capture_executed_sql_bind($cref) };
129 return Test::Builder->new->ok(1, $msg || "No queries executed while running $cref")
130 if !@got and !@expected;
132 require SQL::Abstract::Test;
134 while (@expected or @got) {
135 my $left = shift @got;
136 my $right = shift @expected;
138 # allow the right side to "simplify" the entire shebang
139 if ($left and $right) {
141 for my $i (1..$#$right) {
145 ref $left->[$i] eq 'ARRAY'
149 $left->[$i] = $left->[$i][1]
154 $ret &= SQL::Abstract::Test::is_same_sql_bind(
166 # we need the $locker to be referenced here for delayed destruction
167 if ($locker->{lock_name} and ($ENV{DBICTEST_LOCK_HOLDER}||0) == $$) {
168 DEBUG_TEST_CONCURRENCY_LOCKS
169 and dbg "$locker->{type} LOCK RELEASED (END): $locker->{lock_name}";
173 my $weak_registry = {};
176 my $self = shift->next::method(@_);
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
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? :)
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 >.>
192 #$dsn =~ s/([^A-Za-z0-9_\-\.\=])/ sprintf '~%02X', ord($1) /ge;
193 #$dsn =~ s/^dbi/dbi/i;
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
201 ! $DBICTest::global_exclusive_lock
203 ( ! $ENV{DBICTEST_LOCK_HOLDER} or $ENV{DBICTEST_LOCK_HOLDER} == $$ )
207 ($_[0]||'') !~ /^ (?i:dbi) \: SQLite \: (?: dbname\= )? (?: \:memory\: | t [\/\\] var [\/\\] DBIxClass\-) /x
213 # guard against infinite recursion
214 local $ENV{DBICTEST_LOCK_HOLDER} = -1;
216 # we need to work with a forced fresh clone so that we do not upset any state
217 # of the main $schema (some tests examine it quite closely)
218 local $SIG{__WARN__} = sub {};
221 # this will either give us an undef $locktype or will determine things
222 # properly with a default ( possibly connecting in the process )
224 my $s = ref($self)->connect(@{$self->storage->connect_info})->storage;
226 $locktype = $s->sqlt_type || 'generic';
228 # in case sqlt_type did connect, doesn't matter if it fails or something
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)) {
238 # this will release whatever lock we may currently be holding
239 # which is fine since the type does not match as checked above
240 DEBUG_TEST_CONCURRENCY_LOCKS
242 and dbg "$locker->{type} LOCK RELEASED (UNDEF): $locker->{lock_name}";
246 my $lockpath = tmpdir . "_dbictest_$locktype.lock";
248 DEBUG_TEST_CONCURRENCY_LOCKS
249 and dbg "Waiting for $locktype LOCK: $lockpath...";
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: $!";
257 await_flock ($lock_fh, LOCK_EX) or die "Unable to lock $lockpath: $!";
259 DEBUG_TEST_CONCURRENCY_LOCKS
260 and dbg "Got $locktype LOCK: $lockpath";
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 $!";
267 read ($lock_fh, $old_pid, 100)
269 ($old_pid) = $old_pid =~ /^(\d+)$/
271 DEBUG_TEST_CONCURRENCY_LOCKS
272 and dbg "Post-grab WAIT for $old_pid START: $lockpath";
275 kill (0, $old_pid) or last;
276 select( undef, undef, undef, 0.1 );
279 DEBUG_TEST_CONCURRENCY_LOCKS
280 and dbg "Post-grab WAIT for $old_pid FINISHED: $lockpath";
283 truncate $lock_fh, 0;
284 seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!";
285 $lock_fh->autoflush(1);
288 $ENV{DBICTEST_LOCK_HOLDER} ||= $$;
293 lock_name => "$lockpath",
298 if ($INC{'Test/Builder.pm'}) {
299 populate_weakregistry ( $weak_registry, $self->storage );
301 my $cur_connect_call = $self->storage->on_connect_call;
303 $self->storage->on_connect_call([
304 (ref $cur_connect_call eq 'ARRAY'
306 : ($cur_connect_call || ())
309 populate_weakregistry( $weak_registry, shift->_dbh )
318 my $self = shift->next::method(@_);
319 populate_weakregistry ( $weak_registry, $self )
320 if $INC{'Test/Builder.pm'};
325 # Make sure we run after any cleanup in other END blocks
326 push @{ B::end_av()->object_2svref }, sub {
327 assert_empty_weakregistry($weak_registry, 'quiet');