Fix parsing DSN when the driver part includes DBI attributes
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest / BaseSchema.pm
index 80d3fb1..2663530 100644 (file)
@@ -6,12 +6,78 @@ use warnings;
 use base qw(DBICTest::Base DBIx::Class::Schema);
 
 use Fcntl qw(:DEFAULT :seek :flock);
-use Time::HiRes 'sleep';
-use Scope::Guard ();
+use IO::Handle ();
+use DBIx::Class::_Util 'scope_guard';
 use DBICTest::Util::LeakTracer qw(populate_weakregistry assert_empty_weakregistry);
-use DBICTest::Util qw( local_umask dbg DEBUG_TEST_CONCURRENCY_LOCKS );
+use DBICTest::Util qw( local_umask tmpdir await_flock dbg DEBUG_TEST_CONCURRENCY_LOCKS );
 use namespace::clean;
 
+if( $ENV{DBICTEST_ASSERT_NO_SPURIOUS_EXCEPTION_ACTION} ) {
+  my $ea = __PACKAGE__->exception_action( sub {
+
+    my ( $fr_num, $disarmed, $throw_exception_fr_num );
+    while( ! $disarmed and my @fr = caller(++$fr_num) ) {
+
+      $throw_exception_fr_num ||= (
+        $fr[3] eq 'DBIx::Class::ResultSource::throw_exception'
+          and
+        $fr_num
+      );
+
+      $disarmed = !! (
+        $fr[1] =~ / \A (?: \. [\/\\] )? x?t [\/\\] .+ \.t \z /x
+          and
+        (
+          $fr[3] =~ /\A (?:
+            Test::Exception::throws_ok
+              |
+            Test::Exception::dies_ok
+              |
+            Try::Tiny::try
+              |
+            \Q(eval)\E
+          ) \z /x
+            or
+          (
+            $fr[3] eq 'Test::Exception::lives_ok'
+              and
+            ( $::TODO or Test::Builder->new->in_todo )
+          )
+        )
+      );
+    }
+
+    Test::Builder->new->ok(0, join "\n",
+      'Unexpected &exception_action invocation',
+      '',
+      '  You almost certainly used eval/try instead of dbic_internal_try()',
+      "  Adjust *one* of the eval-ish constructs in the callstack starting" . DBICTest::Util::stacktrace($throw_exception_fr_num||())
+    ) unless $disarmed;
+
+    DBIx::Class::Exception->throw( $_[0] );
+  });
+
+  my $interesting_ns_rx = qr/^ (?: main$ | DBIx::Class:: | DBICTest:: ) /x;
+
+  # hard-set $SIG{__DIE__} to the class-wide exception_action
+  # with a little escape preceeding it
+  $SIG{__DIE__} = sub {
+
+    # without this there would be false positives everywhere :(
+    die @_ if (
+      (caller(0))[0] !~ $interesting_ns_rx
+        or
+      (
+        caller(0) eq 'main'
+          and
+        (caller(1))[0] !~ $interesting_ns_rx
+      )
+    );
+
+    &$ea;
+  };
+}
+
 sub capture_executed_sql_bind {
   my ($self, $cref) = @_;
 
@@ -31,9 +97,9 @@ sub capture_executed_sql_bind {
     qw(debugcb debugobj debug)
   };
 
-  my $sg = Scope::Guard->new(sub {
+  my $sg = scope_guard {
     $self->storage->$_ ( $orig_states->{$_} ) for keys %$orig_states;
-  });
+  };
 
   $self->storage->debugcb(undef);
   $self->storage->debugobj( my $tracer_obj = DBICTest::SQLTracerObj->new );
@@ -151,13 +217,6 @@ sub connection {
   # an envvar, we can not detect when a user invokes prove -jN. Hence
   # perform the locking at all times, it shouldn't hurt.
   # the lock fh *should* inherit across forks/subprocesses
-  #
-  # File locking is hard. Really hard. By far the best lock implementation
-  # I've seen is part of the guts of File::Temp. However it is sadly not
-  # reusable. Since I am not aware of folks doing NFS parallel testing,
-  # nor are we known to work on VMS, I am just going to punt this and
-  # use the portable-ish flock() provided by perl itself. If this does
-  # not work for you - patches more than welcome.
   if (
     ! $DBICTest::global_exclusive_lock
       and
@@ -165,7 +224,7 @@ sub connection {
       and
     ref($_[0]) ne 'CODE'
       and
-    ($_[0]||'') !~ /^ (?i:dbi) \: SQLite \: (?: dbname\= )? (?: \:memory\: | t [\/\\] var [\/\\] DBIxClass\-) /x
+    ($_[0]||'') !~ /^ (?i:dbi) \: SQLite (?: \: | \W ) .*? (?: dbname\= )? (?: \:memory\: | t [\/\\] var [\/\\] DBIxClass\-) /x
   ) {
 
     my $locktype;
@@ -177,6 +236,7 @@ sub connection {
       # we need to work with a forced fresh clone so that we do not upset any state
       # of the main $schema (some tests examine it quite closely)
       local $SIG{__WARN__} = sub {};
+      local $SIG{__DIE__};
       local $@;
 
       # this will either give us an undef $locktype or will determine things
@@ -204,7 +264,7 @@ sub connection {
 
       undef $locker;
 
-      my $lockpath = DBICTest::RunMode->tmpdir->file("_dbictest_$locktype.lock");
+      my $lockpath = tmpdir . "_dbictest_$locktype.lock";
 
       DEBUG_TEST_CONCURRENCY_LOCKS
         and dbg "Waiting for $locktype LOCK: $lockpath...";
@@ -215,7 +275,7 @@ sub connection {
         sysopen ($lock_fh, $lockpath, O_RDWR|O_CREAT) or die "Unable to open $lockpath: $!";
       }
 
-      flock ($lock_fh, LOCK_EX) or die "Unable to lock $lockpath: $!";
+      await_flock ($lock_fh, LOCK_EX) or die "Unable to lock $lockpath: $!";
 
       DEBUG_TEST_CONCURRENCY_LOCKS
         and dbg "Got $locktype LOCK: $lockpath";
@@ -234,7 +294,7 @@ sub connection {
 
         for (1..50) {
           kill (0, $old_pid) or last;
-          sleep 0.1;
+          select( undef, undef, undef, 0.1 );
         }
 
         DEBUG_TEST_CONCURRENCY_LOCKS