See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * DML read/read-write database handle tests for mysqli_native_moodle_database 19 * 20 * @package core 21 * @category dml 22 * @copyright 2018 Srdjan Janković, Catalyst IT 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace core; 27 28 use moodle_database; 29 30 defined('MOODLE_INTERNAL') || die(); 31 32 require_once (__DIR__.'/fixtures/read_slave_moodle_database_mock_mysqli.php'); 33 34 /** 35 * DML mysqli_native_moodle_database read slave specific tests 36 * 37 * @package core 38 * @category dml 39 * @copyright 2018 Catalyst IT 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 * @covers \mysqli_native_moodle_database 42 */ 43 class dml_mysqli_read_slave_test extends \base_testcase { 44 /** 45 * Test readonly handle is not used for reading from special pg_*() call queries, 46 * pg_try_advisory_lock and pg_advisory_unlock. 47 * 48 * @return void 49 */ 50 public function test_lock(): void { 51 $DB = new read_slave_moodle_database_mock_mysqli(); 52 53 $this->assertEquals(0, $DB->perf_get_reads_slave()); 54 55 $DB->query_start("SELECT GET_LOCK('lock',1)", null, SQL_QUERY_SELECT); 56 $this->assertTrue($DB->db_handle_is_rw()); 57 $DB->query_end(null); 58 $this->assertEquals(0, $DB->perf_get_reads_slave()); 59 60 $DB->query_start("SELECT RELEASE_LOCK('lock',1)", null, SQL_QUERY_SELECT); 61 $this->assertTrue($DB->db_handle_is_rw()); 62 $DB->query_end(null); 63 $this->assertEquals(0, $DB->perf_get_reads_slave()); 64 } 65 66 /** 67 * Test readonly handle is used for SQL_QUERY_AUX_READONLY queries. 68 * 69 * @return void 70 */ 71 public function test_aux_readonly(): void { 72 global $DB; 73 74 if ($DB->get_dbfamily() != 'mysql') { 75 $this->markTestSkipped("Not mysql"); 76 } 77 78 // Open second connection. 79 $cfg = $DB->export_dbconfig(); 80 if (!isset($cfg->dboptions)) { 81 $cfg->dboptions = []; 82 } 83 $cfg->dboptions['readonly'] = [ 84 'instance' => [$cfg->dbhost] 85 ]; 86 $cfg->dboptions['dbengine'] = null; 87 $cfg->dboptions['bulkinsertsize'] = null; 88 89 // Get a separate disposable db connection handle with guaranteed 'readonly' config. 90 $db2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary); 91 $db2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions); 92 93 $reads = $db2->perf_get_reads(); 94 $readsprimary = $reads - $db2->perf_get_reads_slave(); 95 96 // Readonly handle queries. 97 98 $db2->setup_is_unicodedb(); 99 $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads()); 100 $this->assertEquals($readsprimary, $reads - $db2->perf_get_reads_slave()); 101 102 $db2->get_tables(); 103 $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads()); 104 $this->assertEquals($readsprimary, $reads - $db2->perf_get_reads_slave()); 105 106 $db2->get_indexes('course'); 107 $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads()); 108 $this->assertEquals($readsprimary, $reads - $db2->perf_get_reads_slave()); 109 110 $db2->get_columns('course'); 111 $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads()); 112 $this->assertEquals($readsprimary, $reads - $db2->perf_get_reads_slave()); 113 114 // Readwrite handle queries. 115 116 if (PHP_INT_SIZE !== 4) { 117 $rc = new \ReflectionClass(\mysqli_native_moodle_database::class); 118 $rcm = $rc->getMethod('insert_chunk_size'); 119 120 $rcm->setAccessible(true); 121 $rcm->invoke($db2); 122 $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads()); 123 $this->assertGreaterThan($readsprimary, $readsprimary = $reads - $db2->perf_get_reads_slave()); 124 } 125 126 $db2->get_dbengine(); 127 $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads()); 128 $this->assertGreaterThan($readsprimary, $readsprimary = $reads - $db2->perf_get_reads_slave()); 129 130 $db2->diagnose(); 131 $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads()); 132 $this->assertGreaterThan($readsprimary, $readsprimary = $reads - $db2->perf_get_reads_slave()); 133 134 $db2->get_row_format('course'); 135 $this->assertGreaterThan($reads, $reads = $db2->perf_get_reads()); 136 $this->assertGreaterThan($readsprimary, $readsprimary = $reads - $db2->perf_get_reads_slave()); 137 } 138 139 /** 140 * Test readonly connection failure with real mysqli connection 141 * 142 * @return void 143 */ 144 public function test_real_readslave_connect_fail(): void { 145 global $DB; 146 147 if ($DB->get_dbfamily() != 'mysql') { 148 $this->markTestSkipped('Not mysql'); 149 } 150 151 // Open second connection. 152 $cfg = $DB->export_dbconfig(); 153 if (!isset($cfg->dboptions)) { 154 $cfg->dboptions = []; 155 } 156 $cfg->dboptions['readonly'] = [ 157 'instance' => ['host.that.is.not'], 158 'connecttimeout' => 1 159 ]; 160 161 $db2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary); 162 $db2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions); 163 $this->assertTrue(count($db2->get_records('user')) > 0); 164 } 165 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body