1 package #hide from pause
6 use base qw(DBICTest::Base DBIx::Class::Schema);
8 use Fcntl qw(:DEFAULT :seek :flock);
9 use Time::HiRes 'sleep';
11 use DBICTest::Util::LeakTracer qw(populate_weakregistry assert_empty_weakregistry);
12 use DBICTest::Util 'local_umask';
15 sub capture_executed_sql_bind {
16 my ($self, $cref) = @_;
18 $self->throw_exception("Expecting a coderef to run") unless ref $cref eq 'CODE';
20 require DBICTest::SQLTracerObj;
22 # hack around stupid, stupid API
23 no warnings 'redefine';
24 local *DBIx::Class::Storage::DBI::_format_for_trace = sub { $_[1] };
25 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
27 # can not use local() due to an unknown number of storages
29 my $orig_states = { map
30 { $_ => $self->storage->$_ }
31 qw(debugcb debugobj debug)
34 my $sg = Scope::Guard->new(sub {
35 $self->storage->$_ ( $orig_states->{$_} ) for keys %$orig_states;
38 $self->storage->debugcb(undef);
39 $self->storage->debugobj( my $tracer_obj = DBICTest::SQLTracerObj->new );
40 $self->storage->debug(1);
42 local $Test::Builder::Level = $Test::Builder::Level + 2;
45 return $tracer_obj->{sqlbinds} || [];
48 sub is_executed_querycount {
49 my ($self, $cref, $exp_counts, $msg) = @_;
51 local $Test::Builder::Level = $Test::Builder::Level + 1;
53 $self->throw_exception("Expecting an hashref of counts or an integer representing total query count")
54 unless ref $exp_counts eq 'HASH' or (defined $exp_counts and ! ref $exp_counts);
56 my @got = map { $_->[0] } @{ $self->capture_executed_sql_bind($cref) };
58 return Test::More::is( @got, $exp_counts, $msg )
59 unless ref $exp_counts;
61 my $got_counts = { map { $_ => 0 } keys %$exp_counts };
62 $got_counts->{$_}++ for @got;
64 return Test::More::is_deeply(
71 sub is_executed_sql_bind {
72 my ($self, $cref, $sqlbinds, $msg) = @_;
74 local $Test::Builder::Level = $Test::Builder::Level + 1;
76 $self->throw_exception("Expecting an arrayref of SQL/Bind pairs") unless ref $sqlbinds eq 'ARRAY';
78 my @expected = @$sqlbinds;
80 my @got = map { $_->[1] } @{ $self->capture_executed_sql_bind($cref) };
83 return Test::Builder->new->ok(1, $msg || "No queries executed while running $cref")
84 if !@got and !@expected;
86 require SQL::Abstract::Test;
88 while (@expected or @got) {
89 my $left = shift @got;
90 my $right = shift @expected;
92 # allow the right side to "simplify" the entire shebang
93 if ($left and $right) {
95 for my $i (1..$#$right) {
99 ref $left->[$i] eq 'ARRAY'
103 $left->[$i] = $left->[$i][1]
108 $ret &= SQL::Abstract::Test::is_same_sql_bind(
120 # we need the $locker to be referenced here for delayed destruction
121 if ($locker->{lock_name} and ($ENV{DBICTEST_LOCK_HOLDER}||0) == $$) {
122 #warn "$$ $0 $locker->{type} LOCK RELEASED";
126 my $weak_registry = {};
129 my $self = shift->next::method(@_);
132 # we can't really lock based on DSN, as we do not yet have a way to tell that e.g.
133 # DBICTEST_MSSQL_DSN=dbi:Sybase:server=192.168.0.11:1433;database=dbtst
135 # DBICTEST_MSSQL_ODBC_DSN=dbi:ODBC:server=192.168.0.11;port=1433;database=dbtst;driver=FreeTDS;tds_version=8.0
136 # are the same server
137 # hence we lock everything based on sqlt_type or just globally if not available
138 # just pretend we are python you know? :)
141 # when we get a proper DSN resolution sanitize to produce a portable lockfile name
142 # this may look weird and unnecessary, but consider running tests from
143 # windows over a samba share >.>
145 #$dsn =~ s/([^A-Za-z0-9_\-\.\=])/ sprintf '~%02X', ord($1) /ge;
146 #$dsn =~ s/^dbi/dbi/i;
148 # provide locking for physical (non-memory) DSNs, so that tests can
149 # safely run in parallel. While the harness (make -jN test) does set
150 # an envvar, we can not detect when a user invokes prove -jN. Hence
151 # perform the locking at all times, it shouldn't hurt.
152 # the lock fh *should* inherit across forks/subprocesses
154 # File locking is hard. Really hard. By far the best lock implementation
155 # I've seen is part of the guts of File::Temp. However it is sadly not
156 # reusable. Since I am not aware of folks doing NFS parallel testing,
157 # nor are we known to work on VMS, I am just going to punt this and
158 # use the portable-ish flock() provided by perl itself. If this does
159 # not work for you - patches more than welcome.
161 ! $DBICTest::global_exclusive_lock
163 ( ! $ENV{DBICTEST_LOCK_HOLDER} or $ENV{DBICTEST_LOCK_HOLDER} == $$ )
167 ($_[0]||'') !~ /^ (?i:dbi) \: SQLite \: (?: dbname\= )? (?: \:memory\: | t [\/\\] var [\/\\] DBIxClass\-) /x
171 # guard against infinite recursion
172 local $ENV{DBICTEST_LOCK_HOLDER} = -1;
174 # we need to connect a forced fresh clone so that we do not upset any state
175 # of the main $schema (some tests examine it quite closely)
176 local $SIG{__WARN__} = sub {};
179 my $st = ref($self)->connect(@{$self->storage->connect_info})->storage;
180 $st->ensure_connected; # do connect here, to catch a possible throw
185 my $t = $storage->sqlt_type || 'generic';
186 eval { $storage->disconnect };
193 # Never hold more than one lock. This solves the "lock in order" issues
194 # unrelated tests may have
195 # Also if there is no connection - there is no lock to be had
196 if ($locktype and (!$locker or $locker->{type} ne $locktype)) {
198 # this will release whatever lock we may currently be holding
199 # which is fine since the type does not match as checked above
202 my $lockpath = DBICTest::RunMode->tmpdir->file("_dbictest_$locktype.lock");
204 #warn "$$ $0 $locktype GRABBING LOCK";
207 my $u = local_umask(0); # so that the file opens as 666, and any user can lock
208 sysopen ($lock_fh, $lockpath, O_RDWR|O_CREAT) or die "Unable to open $lockpath: $!";
210 flock ($lock_fh, LOCK_EX) or die "Unable to lock $lockpath: $!";
211 #warn "$$ $0 $locktype LOCK GRABBED";
213 # see if anyone was holding a lock before us, and wait up to 5 seconds for them to terminate
214 # if we do not do this we may end up trampling over some long-running END or somesuch
215 seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!";
218 read ($lock_fh, $old_pid, 100)
220 ($old_pid) = $old_pid =~ /^(\d+)$/
223 kill (0, $old_pid) or last;
227 #warn "$$ $0 $locktype POST GRAB WAIT";
229 truncate $lock_fh, 0;
230 seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!";
231 $lock_fh->autoflush(1);
234 $ENV{DBICTEST_LOCK_HOLDER} ||= $$;
239 lock_name => "$lockpath",
244 if ($INC{'Test/Builder.pm'}) {
245 populate_weakregistry ( $weak_registry, $self->storage );
247 my $cur_connect_call = $self->storage->on_connect_call;
249 $self->storage->on_connect_call([
250 (ref $cur_connect_call eq 'ARRAY'
252 : ($cur_connect_call || ())
255 populate_weakregistry( $weak_registry, shift->_dbh )
264 my $self = shift->next::method(@_);
265 populate_weakregistry ( $weak_registry, $self )
266 if $INC{'Test/Builder.pm'};
271 # Make sure we run after any cleanup in other END blocks
272 push @{ B::end_av()->object_2svref }, sub {
273 assert_empty_weakregistry($weak_registry, 'quiet');