aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2024-04-24 11:41:30 +0200
committerChristoph Wurst <christoph@winzerhof-wurst.at>2024-05-28 10:38:36 +0200
commit3bfba2042c0397452193e9b6f1fa4bd1596b17a6 (patch)
tree60a1cb4b7f8ca8540738f0178898319c1c887cd7 /tests
parentf0ec5489a444ced8d8794b93abf42a3de973cf54 (diff)
downloadnextcloud-server-3bfba2042c0397452193e9b6f1fa4bd1596b17a6.tar.gz
nextcloud-server-3bfba2042c0397452193e9b6f1fa4bd1596b17a6.zip
fix(db): Prevent two connections for single node databases
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/DB/ConnectionTest.php101
1 files changed, 101 insertions, 0 deletions
diff --git a/tests/lib/DB/ConnectionTest.php b/tests/lib/DB/ConnectionTest.php
new file mode 100644
index 00000000000..3f06082ff0c
--- /dev/null
+++ b/tests/lib/DB/ConnectionTest.php
@@ -0,0 +1,101 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace Test\DB;
+
+use Doctrine\DBAL\Configuration;
+use Doctrine\DBAL\Driver;
+use Doctrine\DBAL\Driver\Connection as DriverConnection;
+use Doctrine\DBAL\Platforms\MySQLPlatform;
+use OC\DB\Adapter;
+use OC\DB\Connection;
+use Test\TestCase;
+
+/**
+ * @group DB
+ */
+class ConnectionTest extends TestCase {
+
+ public function testSingleNodeConnectsToPrimaryOnly(): void {
+ $connectionParams = [
+ 'user' => 'test',
+ 'password' => 'topsecret',
+ 'host' => 'test',
+ ];
+ $adapter = $this->createMock(Adapter::class);
+ $driver = $this->createMock(Driver::class);
+ $configuration = $this->createMock(Configuration::class);
+ $connection = $this->getMockBuilder(Connection::class)
+ ->onlyMethods(['connectTo'])
+ ->setConstructorArgs([
+ [
+ 'adapter' => $adapter,
+ 'platform' => new MySQLPlatform(),
+ 'tablePrefix' => 'nctest',
+ 'primary' => $connectionParams,
+ 'replica' => [
+ $connectionParams,
+ ],
+ ],
+ $driver,
+ $configuration,
+ ])
+ ->getMock();
+ $driverConnection = $this->createMock(DriverConnection::class);
+ $connection->expects(self::once())
+ ->method('connectTo')
+ ->with('primary')
+ ->willReturn($driverConnection);
+
+ $connection->ensureConnectedToReplica();
+ $connection->ensureConnectedToPrimary();
+ $connection->ensureConnectedToReplica();
+ }
+
+ public function testClusterConnectsToPrimaryAndReplica(): void {
+ $connectionParamsPrimary = [
+ 'user' => 'test',
+ 'password' => 'topsecret',
+ 'host' => 'testprimary',
+ ];
+ $connectionParamsReplica = [
+ 'user' => 'test',
+ 'password' => 'topsecret',
+ 'host' => 'testreplica',
+ ];
+ $adapter = $this->createMock(Adapter::class);
+ $driver = $this->createMock(Driver::class);
+ $configuration = $this->createMock(Configuration::class);
+ $connection = $this->getMockBuilder(Connection::class)
+ ->onlyMethods(['connectTo'])
+ ->setConstructorArgs([
+ [
+ 'adapter' => $adapter,
+ 'platform' => new MySQLPlatform(),
+ 'tablePrefix' => 'nctest',
+ 'primary' => $connectionParamsPrimary,
+ 'replica' => [
+ $connectionParamsReplica,
+ ],
+ ],
+ $driver,
+ $configuration,
+ ])
+ ->getMock();
+ $driverConnection = $this->createMock(DriverConnection::class);
+ $connection->expects(self::exactly(2))
+ ->method('connectTo')
+ ->willReturn($driverConnection);
+
+ $connection->ensureConnectedToReplica();
+ $connection->ensureConnectedToPrimary();
+ $connection->ensureConnectedToReplica();
+ }
+
+}