Author: theory
Date: Mon Jul 26 12:09:38 2010
New Revision: 14286

Modified:
   dbi/trunk/t/16destroy.t

Log:
Rewrite AutoInactiveDestroy tests.

Modified: dbi/trunk/t/16destroy.t
==============================================================================
--- dbi/trunk/t/16destroy.t     (original)
+++ dbi/trunk/t/16destroy.t     Mon Jul 26 12:09:38 2010
@@ -2,48 +2,133 @@
 
 use strict;
 
-use Test::More tests => 14;
+use Test::More tests => 20;
 
 BEGIN{ use_ok( 'DBI' ) }
 
+my $expect_active;
+
+## main Test Driver Package
+{
+    package DBD::Test;
+
+    use strict;
+    use warnings;
+
+    my $drh = undef;
+
+    sub driver {
+        return $drh if $drh;
+        my ($class, $attr) = @_;
+        $class = "${class}::dr";
+        ($drh) = DBI::_new_drh($class, {
+            Name    => 'Test',
+            Version => '1.0',
+        }, 77 );
+        return $drh;
+    }
+}
+
+## Test Driver
+{
+    package DBD::Test::dr;
+
+    use warnings;
+    use Test::More;
+
+    sub connect { # normally overridden, but a handy default
+        my($drh, $dbname, $user, $auth, $attrs)= @_;
+        my ($outer, $dbh) = DBI::_new_dbh($drh);
+        $dbh->STORE(Active => 1);
+        $dbh->STORE(AutoCommit => 1);
+        $dbh->STORE( $_ => $attrs->{$_}) for keys %$attrs;
+        return $outer;
+    }
+
+    $DBD::Test::dr::imp_data_size = 0;
+    cmp_ok($DBD::Test::dr::imp_data_size, '==', 0, '... check 
DBD::Test::dr::imp_data_size to avoid typo');
+}
+
+## Test db package
+{
+    package DBD::Test::db;
+
+    use strict;
+    use warnings;
+    use Test::More;
+
+    $DBD::Test::db::imp_data_size = 0;
+    cmp_ok($DBD::Test::db::imp_data_size, '==', 0, '... check 
DBD::Test::db::imp_data_size to avoid typo');
+
+    sub STORE {
+        my ($dbh, $attrib, $value) = @_;
+        # would normally validate and only store known attributes
+        # else pass up to DBI to handle
+        if ($attrib eq 'AutoCommit') {
+            # convert AutoCommit values to magic ones to let DBI
+            # know that the driver has 'handled' the AutoCommit attribute
+            $value = ($value) ? -901 : -900;
+        }
+        return $dbh->{$attrib} = $value if $attrib =~ /^examplep_/;
+        return $dbh->SUPER::STORE($attrib, $value);
+    }
+
+    sub DESTROY {
+        if ($expect_active < 0) {
+            exit shift->FETCH('Active') || 0;
+        }
+        return $expect_active
+            ? ok( shift->FETCH('Active'), 'Should be active in DESTROY')
+            : ok( !shift->FETCH('Active'), 'Should not be active in DESTROY');
+    }
+}
+
 my $dsn = 'dbi:ExampleP:dummy';
 
-# Connect to the example driver.
-ok my $dbh = DBI->connect($dsn, '', ''),
-    'Create plain dbh';
-
-isa_ok( $dbh, 'DBI::db' );
-
-# Clean up when we're done.
-END { $dbh->disconnect if $dbh };
-
-ok $dbh->{Active}, 'Should start active';
-$dbh->DESTROY;
-ok $dbh->{Active}, 'Should still be active';
-
-# Try InactiveDestroy.
-ok $dbh = DBI->connect($dsn, '', '', { InactiveDestroy => 1 }),
-    'Create with ActiveDestroy';
-ok $dbh->{Active}, 'Should start active';
-#DBI->trace(9);
-$dbh->DESTROY;
-ok !$dbh->{Active}, 'Should no longer be active';
-#DBI->trace(0);
-
-# Try AutoInactiveDestroy.
-ok $dbh = DBI->connect($dsn, '', '', { AutoInactiveDestroy => 1 }),
-    'Create with AutoInactiveDestroy';
-ok $dbh->{Active}, 'Should start active';
-$dbh->DESTROY;
-ok $dbh->{Active}, 'Should still be active';
-
-# Try AutoInactiveDestroy and "fork".
-ok $dbh = DBI->connect($dsn, '', '', { AutoInactiveDestroy => 1 }),
-    'Create with AutoInactiveDestroy again';
-ok $dbh->{Active}, 'Should start active';
-do {
-    # Fake fork.
-    local $$ = 0;
-    $dbh->DESTROY;
-};
-ok !$dbh->{Active}, 'Should not still be active';
+$INC{'DBD/Test.pm'} = 'dummy';  # required to fool DBI->install_driver()
+ok my $drh = DBI->install_driver('Test'), 'Install test driver';
+
+NOSETTING: {
+    # Try defaults.
+    ok my $dbh = $drh->connect, 'Connect to test driver';
+    ok $dbh->{Active}, 'Should start active';
+    $expect_active = 1;
+}
+
+IAD: {
+    # Try InactiveDestroy.
+    ok my $dbh = $drh->connect($dsn, '', '', { InactiveDestroy => 1 }),
+        'Create with ActiveDestroy';
+    ok $dbh->{InactiveDestroy}, 'InactiveDestroy should be set';
+    ok $dbh->{Active}, 'Should start active';
+    $expect_active = 0;
+}
+
+AIAD: {
+    # Try AutoInactiveDestroy.
+    ok my $dbh = $drh->connect($dsn, '', '', { AutoInactiveDestroy => 1 }),
+        'Create with AutoInactiveDestroy';
+    ok $dbh->{AutoInactiveDestroy}, 'InactiveDestroy should be set';
+    ok $dbh->{Active}, 'Should start active';
+    $expect_active = 1;
+}
+
+FORK: {
+    # Try AutoInactiveDestroy and fork.
+    ok my $dbh = $drh->connect($dsn, '', '', { AutoInactiveDestroy => 1 }),
+        'Create with AutoInactiveDestroy again';
+    ok $dbh->{AutoInactiveDestroy}, 'InactiveDestroy should be set';
+    ok $dbh->{Active}, 'Should start active';
+
+    # XXX Add code to skip where fork() is unimplemented.
+    my $pid = fork();
+    if ($pid) {
+        # parent.
+        $expect_active = 1;
+        wait;
+        ok $? == 0, 'Child should be inactive on DESTROY';
+    } else {
+        # child.
+        $expect_active = -1;
+    }
+}

Reply via email to