From 05f53236efc2a4ef87aa44c9ebe4df658c339b86 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 28 Apr 2021 13:43:30 +0800 Subject: [PATCH 1/1] 70965-wip --- lib/dml/tests/dml_pgsql_read_slave_test.php | 33 ++++++++++++- .../fixtures/test_moodle_read_slave_trait.php | 49 +++++++------------ 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/lib/dml/tests/dml_pgsql_read_slave_test.php b/lib/dml/tests/dml_pgsql_read_slave_test.php index daa8208fc4..745dd2c828 100644 --- a/lib/dml/tests/dml_pgsql_read_slave_test.php +++ b/lib/dml/tests/dml_pgsql_read_slave_test.php @@ -35,7 +35,7 @@ require_once(__DIR__.'/fixtures/read_slave_moodle_database_mock_pgsql.php'); * @copyright 2018 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_dml_pgsql_read_slave_testcase extends base_testcase { +class dml_pgsql_read_slave_test extends base_testcase { /** * Test correct database handles are used for cursors * @@ -107,6 +107,37 @@ class core_dml_pgsql_read_slave_testcase extends base_testcase { } } + /** + * Test readonly handle is not used for reading from temptables + * and getting temptables metadata. + * This test is only possible because of no pg_query error reporting. + * It may need to be removed in the future if we decide to handle null + * results in pgsql_native_moodle_database differently. + * + * @return void + */ + public function test_temp_table() : void { + $DB = new read_slave_moodle_database_mock_pgsql(); + + $this->assertEquals(0, $DB->perf_get_reads_slave()); + + $dbman = $DB->get_manager(); + $table = new xmldb_table('silly_test_table'); + $table->add_field('id', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE); + $table->add_field('msg', XMLDB_TYPE_CHAR, 255); + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $dbman->create_temp_table($table); + $this->assertTrue($DB->db_handle_is_rw()); + $this->assertFalse($DB->db_handle_is_ro()); + $this->assertEquals(0, $DB->perf_get_reads_slave()); + + $DB->get_columns('silly_test_table'); + $DB->get_records('silly_test_table'); + $this->assertTrue($DB->db_handle_is_rw()); + $this->assertFalse($DB->db_handle_is_ro()); + $this->assertEquals(0, $DB->perf_get_reads_slave()); + } + /** * Test readonly connection failure with real pgsql connection * diff --git a/lib/dml/tests/fixtures/test_moodle_read_slave_trait.php b/lib/dml/tests/fixtures/test_moodle_read_slave_trait.php index 27a729343e..441566169f 100644 --- a/lib/dml/tests/fixtures/test_moodle_read_slave_trait.php +++ b/lib/dml/tests/fixtures/test_moodle_read_slave_trait.php @@ -36,39 +36,28 @@ require_once(__DIR__.'/../../pgsql_native_moodle_database.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ trait test_moodle_read_slave_trait { - // @codingStandardsIgnoreStart - /** - * Constructs a mock db driver - * - * @param bool $external - */ public function __construct($external = false) { - // @codingStandardsIgnoreEnd - parent::__construct($external); + global $CFG; - $rw = fopen("php://memory", 'r+'); - fputs($rw, 'rw'); - - $ro = fopen("php://memory", 'r+'); - fputs($ro, 'ro'); + parent::__construct($external); - $this->wantreadslave = true; - $this->dbhwrite = $rw; - $this->dbhreadonly = $ro; - $this->set_db_handle($this->dbhwrite); + $dboptions = $CFG->dboptions; + if ($dboptions === null) { + $dboptions = []; + } - $this->temptables = new moodle_temptables($this); - } + $dboptions['readonly'] = [ + 'instance' => [$CFG->dbhost], + ]; - /** - * Check db handle - * @param string $id - * @return bool - */ - public function db_handle_is($id) { - $dbh = $this->get_db_handle(); - rewind($dbh); - return stream_get_contents($dbh) == $id; + $this->connect( + $CFG->dbhost, + $CFG->dbuser, + $CFG->dbpass, + $CFG->dbname, + $CFG->prefix, + $dboptions + ); } /** @@ -76,7 +65,7 @@ trait test_moodle_read_slave_trait { * @return bool */ public function db_handle_is_rw() { - return $this->db_handle_is('rw'); + return $this->dbhwrite === $this->pgsql; } /** @@ -84,7 +73,7 @@ trait test_moodle_read_slave_trait { * @return bool */ public function db_handle_is_ro() { - return $this->db_handle_is('ro'); + return !$this->db_handle_is_rw(); } /** -- 2.30.0