1 package # hide from PAUSE
8 use base 'DBICTest::BaseSchema';
10 use Fcntl qw/:DEFAULT :seek :flock/;
11 use Time::HiRes 'sleep';
12 use DBICTest::RunMode;
13 use DBICTest::Util::LeakTracer qw/populate_weakregistry assert_empty_weakregistry/;
14 use DBICTest::Util 'local_umask';
17 __PACKAGE__->mk_group_accessors(simple => 'custom_attr');
19 __PACKAGE__->load_classes(qw/
37 { 'DBICTest::Schema' => [qw/
51 'FourKeys_to_TwoKeys',
54 'ArtistUndirectedMap',
59 'Dummy', # this is a real result class we remove in the hook below
61 qw/SelfRefAlias TreeLike TwoKeyTreeLike Event EventTZ NoPrimaryKey/,
62 qw/Collection CollectionObject TypedObject Owners BooksInLibrary/,
63 qw/ForceForeign Encoded/,
66 sub sqlt_deploy_hook {
67 my ($self, $sqlt_schema) = @_;
69 $sqlt_schema->drop_table('dummy');
75 # we need the $locker to be referenced here for delayed destruction
76 if ($locker->{lock_name} and ($ENV{DBICTEST_LOCK_HOLDER}||0) == $$) {
77 #warn "$$ $0 $locktype LOCK RELEASED";
81 my $weak_registry = {};
84 my $self = shift->next::method(@_);
87 # we can't really lock based on DSN, as we do not yet have a way to tell that e.g.
88 # DBICTEST_MSSQL_DSN=dbi:Sybase:server=192.168.0.11:1433;database=dbtst
90 # DBICTEST_MSSQL_ODBC_DSN=dbi:ODBC:server=192.168.0.11;port=1433;database=dbtst;driver=FreeTDS;tds_version=8.0
92 # hence we lock everything based on sqlt_type or just globally if not available
93 # just pretend we are python you know? :)
96 # when we get a proper DSN resolution sanitize to produce a portable lockfile name
97 # this may look weird and unnecessary, but consider running tests from
98 # windows over a samba share >.>
100 #$dsn =~ s/([^A-Za-z0-9_\-\.\=])/ sprintf '~%02X', ord($1) /ge;
101 #$dsn =~ s/^dbi/dbi/i;
103 # provide locking for physical (non-memory) DSNs, so that tests can
104 # safely run in parallel. While the harness (make -jN test) does set
105 # an envvar, we can not detect when a user invokes prove -jN. Hence
106 # perform the locking at all times, it shouldn't hurt.
107 # the lock fh *should* inherit across forks/subprocesses
109 # File locking is hard. Really hard. By far the best lock implementation
110 # I've seen is part of the guts of File::Temp. However it is sadly not
111 # reusable. Since I am not aware of folks doing NFS parallel testing,
112 # nor are we known to work on VMS, I am just going to punt this and
113 # use the portable-ish flock() provided by perl itself. If this does
114 # not work for you - patches more than welcome.
116 ! $DBICTest::global_exclusive_lock
118 ( ! $ENV{DBICTEST_LOCK_HOLDER} or $ENV{DBICTEST_LOCK_HOLDER} == $$ )
122 ($_[0]||'') !~ /^ (?i:dbi) \: SQLite \: (?: dbname\= )? (?: \:memory\: | t [\/\\] var [\/\\] DBIxClass\-) /x
126 # guard against infinite recursion
127 local $ENV{DBICTEST_LOCK_HOLDER} = -1;
129 # we need to connect a forced fresh clone so that we do not upset any state
130 # of the main $schema (some tests examine it quite closely)
133 my $st = ref($self)->connect(@{$self->storage->connect_info})->storage;
134 $st->ensure_connected; # do connect here, to catch a possible throw
139 my $t = $storage->sqlt_type || 'generic';
140 eval { $storage->disconnect };
148 # Never hold more than one lock. This solves the "lock in order" issues
149 # unrelated tests may have
150 # Also if there is no connection - there is no lock to be had
151 if ($locktype and (!$locker or $locker->{type} ne $locktype)) {
153 my $lockpath = DBICTest::RunMode->tmpdir->file(".dbictest_$locktype.lock");
155 #warn "$$ $0 $locktype GRABBING LOCK";
158 my $u = local_umask(0); # so that the file opens as 666, and any user can lock
159 sysopen ($lock_fh, $lockpath, O_RDWR|O_CREAT) or die "Unable to open $lockpath: $!";
161 flock ($lock_fh, LOCK_EX) or die "Unable to lock $lockpath: $!";
162 #warn "$$ $0 $locktype LOCK GRABBED";
164 # see if anyone was holding a lock before us, and wait up to 5 seconds for them to terminate
165 # if we do not do this we may end up trampling over some long-running END or somesuch
166 seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!";
169 read ($lock_fh, $old_pid, 100)
171 ($old_pid) = $old_pid =~ /^(\d+)$/
174 kill (0, $old_pid) or last;
178 #warn "$$ $0 $locktype POST GRAB WAIT";
180 truncate $lock_fh, 0;
181 seek ($lock_fh, 0, SEEK_SET) or die "seek failed $!";
182 $lock_fh->autoflush(1);
185 $ENV{DBICTEST_LOCK_HOLDER} ||= $$;
190 lock_name => "$lockpath",
195 if ($INC{'Test/Builder.pm'}) {
196 populate_weakregistry ( $weak_registry, $self->storage );
198 my $cur_connect_call = $self->storage->on_connect_call;
200 $self->storage->on_connect_call([
201 (ref $cur_connect_call eq 'ARRAY'
203 : ($cur_connect_call || ())
206 populate_weakregistry( $weak_registry, shift->_dbh )
215 my $self = shift->next::method(@_);
216 populate_weakregistry ( $weak_registry, $self )
217 if $INC{'Test/Builder.pm'};
222 assert_empty_weakregistry($weak_registry, 'quiet');