]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1499262 - Bugzilla::DB should gracefully handle disconnection events that happen...
authorDylan William Hardison <dylan@hardison.net>
Tue, 16 Oct 2018 17:51:33 +0000 (13:51 -0400)
committerGitHub <noreply@github.com>
Tue, 16 Oct 2018 17:51:33 +0000 (13:51 -0400)
Bugzilla/DB.pm
Bugzilla/Test/MockDB.pm
t/db-error-cleanup.pl [new file with mode: 0644]

index 87110aaaad317ad7b7dbe1dbf2c2be9005338369..bcd455bae11fb283d96530d346675a5309a2862b 100644 (file)
@@ -1280,8 +1280,9 @@ sub bz_rollback_transaction {
     if (!$self->bz_in_transaction) {
         ThrowCodeError("not_in_transaction");
     } else {
-        $self->rollback();
         $self->{private_bz_transaction_count} = 0;
+        # the next line may fail if somehow we're connected but not in a txn
+        $self->rollback() if $self->connector->connected;
     }
 }
 
index fb7873ccf70a68b6b225850657097ef507dd639a..84e4a3bf354e5edfee861cd6d8cb5440b87f5894 100644 (file)
@@ -13,7 +13,7 @@ use Capture::Tiny qw(capture_merged);
 
 use Bugzilla::Test::MockLocalconfig (
     db_driver => 'sqlite',
-    db_name => ':memory:',
+    db_name => $ENV{test_db_name} // ':memory:',
 );
 use Bugzilla;
 BEGIN { Bugzilla->extensions };
diff --git a/t/db-error-cleanup.pl b/t/db-error-cleanup.pl
new file mode 100644 (file)
index 0000000..7db3366
--- /dev/null
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# This Source Code Form is "Incompatible With Secondary Licenses", as
+# defined by the Mozilla Public License, v. 2.0.
+use strict;
+use warnings;
+use 5.10.1;
+use lib qw( . lib local/lib/perl5 );
+
+BEGIN {
+  $ENV{LOG4PERL_CONFIG_FILE} = 'log4perl-t.conf';
+  $ENV{test_db_name} = 'db_errors';
+}
+
+END { unlink('data/db/db_errors') }
+
+use Bugzilla::Test::MockLocalconfig (urlbase => 'http://bmo-web.vm' );
+use Bugzilla::Test::MockDB;
+use Test2::V0;
+use Test2::Tools::Exception qw(dies lives);
+use Try::Tiny;
+use Bugzilla::Test::Util qw(create_user);
+
+ok(
+  dies {
+    my $dbh = Bugzilla->dbh;
+    $dbh->bz_start_transaction;
+    create_user('example@user.org', '*');
+    $dbh->dbh->disconnect;
+    $dbh->dbh;
+  },
+  "connecting after a disconnect in a bz transaction is fatal"
+) and note($@);
+
+ok(lives { Bugzilla->_cleanup }, "_cleanup") or note($@);
+
+ok(!Bugzilla->request_cache->{dbh}, "dbh should be gone after cleanup");
+
+my $user = Bugzilla::User->new({name => 'example@user.org'});
+ok(!$user, 'user was not created');
+
+ok(
+  dies {
+    my $dbh = Bugzilla->dbh;
+    $dbh->bz_start_transaction;
+    create_user('example@user.org', '*');
+    $dbh->dbh->disconnect;
+    $dbh->bz_commit_transaction;
+  },
+  "commit after a disconnect is fatal"
+) and note($@);
+
+ok(lives { Bugzilla->_cleanup }, "_cleanup") or note($@);
+
+ok(
+  lives {
+    my $dbh = Bugzilla->dbh;
+    $dbh->bz_start_transaction;
+    create_user('example@user.org', '*');
+    $dbh->dbh->disconnect;
+    Bugzilla->_cleanup;
+    $dbh->dbh;
+  },
+  "calling _cleanup after bz_start_transaction results in a working connection",
+) or note($@);
+
+
+done_testing;