IFF debug goes to STDERR by default - silence possible wide-char warnings
[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);
9use Time::HiRes 'sleep';
10use DBICTest::Util::LeakTracer qw(populate_weakregistry assert_empty_weakregistry);
11use DBICTest::Util 'local_umask';
12use namespace::clean;
13
2cfc22dd 14{
15 package # moar hide
16 DBICTest::SQLTracerObj;
17 use base 'DBIx::Class::Storage::Statistics';
18
19 sub query_start { push @{$_[0]{sqlbinds}}, [ ($_[1] =~ /^\s*(\S+)/)[0], [ $_[1], @{ $_[2]||[] } ] ] }
20
21 # who the hell came up with this API >:(
22 for my $txn (qw(begin rollback commit)) {
23 no strict 'refs';
24 *{"txn_$txn"} = sub { push @{$_[0]{sqlbinds}}, [ uc $txn => [ uc $txn ] ] };
25 }
26
27 sub svp_begin { push @{$_[0]{sqlbinds}}, [ SAVEPOINT => [ "SAVEPOINT $_[1]" ] ] }
28 sub svp_release { push @{$_[0]{sqlbinds}}, [ RELEASE_SAVEPOINT => [ "RELEASE $_[1]" ] ] }
29 sub svp_rollback { push @{$_[0]{sqlbinds}}, [ ROLLBACK_TO_SAVEPOINT => [ "ROLLBACK TO $_[1]" ] ] }
30
31}
32
33sub capture_executed_sql_bind {
34 my ($self, $cref) = @_;
35
36 $self->throw_exception("Expecting a coderef to run") unless ref $cref eq 'CODE';
37
38 # hack around stupid, stupid API
39 no warnings 'redefine';
40 local *DBIx::Class::Storage::DBI::_format_for_trace = sub { $_[1] };
41 Class::C3->reinitialize if DBIx::Class::_ENV_::OLD_MRO;
42
43 local $self->storage->{debugcb};
44 local $self->storage->{debugobj} = my $tracer_obj = DBICTest::SQLTracerObj->new;
45 local $self->storage->{debug} = 1;
46
49eeb48d 47 local $Test::Builder::Level = $Test::Builder::Level + 2;
2cfc22dd 48 $cref->();
49
50 return $tracer_obj->{sqlbinds} || [];
51}
52
49eeb48d 53sub is_executed_querycount {
54 my ($self, $cref, $exp_counts, $msg) = @_;
55
56 local $Test::Builder::Level = $Test::Builder::Level + 1;
57
58 $self->throw_exception("Expecting an hashref of counts or an integer representing total query count")
59 unless ref $exp_counts eq 'HASH' or (defined $exp_counts and ! ref $exp_counts);
60
61 my @got = map { $_->[0] } @{ $self->capture_executed_sql_bind($cref) };
62
63 return Test::More::is( @got, $exp_counts, $msg )
64 unless ref $exp_counts;
65
66 my $got_counts = { map { $_ => 0 } keys %$exp_counts };
67 $got_counts->{$_}++ for @got;
68
69 return Test::More::is_deeply(
70 $got_counts,
71 $exp_counts,
72 $msg,
73 );
74}
75
2cfc22dd 76sub is_executed_sql_bind {
77 my ($self, $cref, $sqlbinds, $msg) = @_;
78
79 local $Test::Builder::Level = $Test::Builder::Level + 1;
80
81 $self->throw_exception("Expecting an arrayref of SQL/Bind pairs") unless ref $sqlbinds eq 'ARRAY';
82
83 my @expected = @$sqlbinds;
84
85 my @got = map { $_->[1] } @{ $self->capture_executed_sql_bind($cref) };
86
87
88 return Test::Builder->new->ok(1, $msg || "No queries executed while running $cref")
89 if !@got and !@expected;
90
91 require SQL::Abstract::Test;
92 my $ret = 1;
93 while (@expected or @got) {
94 my $left = shift @got;
95 my $right = shift @expected;
96
97 # allow the right side to "simplify" the entire shebang
98 if ($left and $right) {
99 $left = [ @$left ];
100 for my $i (1..$#$right) {
101 if (
102 ! ref $right->[$i]
103 and
104 ref $left->[$i] eq 'ARRAY'
105 and
106 @{$left->[$i]} == 2
107 ) {
108 $left->[$i] = $left->[$i][1]
109 }
110 }
111 }
112
113 $ret &= SQL::Abstract::Test::is_same_sql_bind(
114 \( $left || [] ),
115 \( $right || [] ),
116 $msg,
117 );
118 }
119
120 return $ret;
121}
122
e952df76 123our $locker;
124END {
125 # we need the $locker to be referenced here for delayed destruction
126 if ($locker->{lock_name} and ($ENV{DBICTEST_LOCK_HOLDER}||0) == $$) {
127 #warn "$$ $0 $locker->{type} LOCK RELEASED";
128 }
129}
130
131my $weak_registry = {};
132
133sub connection {
134 my $self = shift->next::method(@_);
135
136# MASSIVE FIXME
137# we can't really lock based on DSN, as we do not yet have a way to tell that e.g.
138# DBICTEST_MSSQL_DSN=dbi:Sybase:server=192.168.0.11:1433;database=dbtst
139# and
140# DBICTEST_MSSQL_ODBC_DSN=dbi:ODBC:server=192.168.0.11;port=1433;database=dbtst;driver=FreeTDS;tds_version=8.0
141# are the same server
142# hence we lock everything based on sqlt_type or just globally if not available
143# just pretend we are python you know? :)
144
145
146 # when we get a proper DSN resolution sanitize to produce a portable lockfile name
147 # this may look weird and unnecessary, but consider running tests from
148 # windows over a samba share >.>
149 #utf8::encode($dsn);
150 #$dsn =~ s/([^A-Za-z0-9_\-\.\=])/ sprintf '~%02X', ord($1) /ge;
151 #$dsn =~ s/^dbi/dbi/i;
152
153 # provide locking for physical (non-memory) DSNs, so that tests can
154 # safely run in parallel. While the harness (make -jN test) does set
155 # an envvar, we can not detect when a user invokes prove -jN. Hence
156 # perform the locking at all times, it shouldn't hurt.
157 # the lock fh *should* inherit across forks/subprocesses
158 #
159 # File locking is hard. Really hard. By far the best lock implementation
160 # I've seen is part of the guts of File::Temp. However it is sadly not
161 # reusable. Since I am not aware of folks doing NFS parallel testing,
162 # nor are we known to work on VMS, I am just going to punt this and
163 # use the portable-ish flock() provided by perl itself. If this does
164 # not work for you - patches more than welcome.
165 if (
166 ! $DBICTest::global_exclusive_lock
167 and
168 ( ! $ENV{DBICTEST_LOCK_HOLDER} or $ENV{DBICTEST_LOCK_HOLDER} == $$ )
169 and
170 ref($_[0]) ne 'CODE'
171 and
172 ($_[0]||'') !~ /^ (?i:dbi) \: SQLite \: (?: dbname\= )? (?: \:memory\: | t [\/\\] var [\/\\] DBIxClass\-) /x
173 ) {
174
175 my $locktype = do {
176 # guard against infinite recursion
177 local $ENV{DBICTEST_LOCK_HOLDER} = -1;
178
179 # we need to connect a forced fresh clone so that we do not upset any state
180 # of the main $schema (some tests examine it quite closely)
181 local $SIG{__WARN__} = sub {};
182 local $@;
183 my $storage = eval {
184 my $st = ref($self)->connect(@{$self->storage->connect_info})->storage;
185 $st->ensure_connected; # do connect here, to catch a possible throw
186 $st;
187 };
188 $storage
189 ? do {
190 my $t = $storage->sqlt_type || 'generic';
191 eval { $storage->disconnect };
192 $t;
193 }
194 : undef
195 ;
196 };
197
198 # Never hold more than one lock. This solves the "lock in order" issues
199 # unrelated tests may have
200 # Also if there is no connection - there is no lock to be had
201 if ($locktype and (!$locker or $locker->{type} ne $locktype)) {
202
203 # this will release whatever lock we may currently be holding
204 # which is fine since the type does not match as checked above
205 undef $locker;
206
207 my $lockpath = DBICTest::RunMode->tmpdir->file("_dbictest_$locktype.lock");
208
209 #warn "$$ $0 $locktype GRABBING LOCK";
210 my $lock_fh;
211 {
212 my $u = local_umask(0); # so that the file opens as 666, and any user can lock
213 sysopen ($lock_fh, $lockpath, O_RDWR|O_CREAT) or die "Unable to open $lockpath: $!";
214 }
215 flock ($lock_fh, LOCK_EX) or die "Unable to lock $lockpath: $!";
216 #warn "$$ $0 $locktype LOCK GRABBED";
217
218 # see if anyone was holding a lock before us, and wait up to 5 seconds for them to terminate
219 # if we do not do this we may end up trampling over some long-running END or somesuch
220 seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!";
221 my $old_pid;
222 if (
223 read ($lock_fh, $old_pid, 100)
224 and
225 ($old_pid) = $old_pid =~ /^(\d+)$/
226 ) {
227 for (1..50) {
228 kill (0, $old_pid) or last;
229 sleep 0.1;
230 }
231 }
232 #warn "$$ $0 $locktype POST GRAB WAIT";
233
234 truncate $lock_fh, 0;
235 seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!";
236 $lock_fh->autoflush(1);
237 print $lock_fh $$;
238
239 $ENV{DBICTEST_LOCK_HOLDER} ||= $$;
240
241 $locker = {
242 type => $locktype,
243 fh => $lock_fh,
244 lock_name => "$lockpath",
245 };
246 }
247 }
248
249 if ($INC{'Test/Builder.pm'}) {
250 populate_weakregistry ( $weak_registry, $self->storage );
251
252 my $cur_connect_call = $self->storage->on_connect_call;
253
254 $self->storage->on_connect_call([
255 (ref $cur_connect_call eq 'ARRAY'
256 ? @$cur_connect_call
257 : ($cur_connect_call || ())
258 ),
259 [sub {
260 populate_weakregistry( $weak_registry, shift->_dbh )
261 }],
262 ]);
263 }
264
265 return $self;
266}
267
268sub clone {
269 my $self = shift->next::method(@_);
270 populate_weakregistry ( $weak_registry, $self )
271 if $INC{'Test/Builder.pm'};
272 $self;
273}
274
275END {
276 assert_empty_weakregistry($weak_registry, 'quiet');
277}
27a701f9 278
2791;