Skip to content

Commit

Permalink
MDL-70965 core_dml: read_slave - mock db handles as resources
Browse files Browse the repository at this point in the history
So they comply with the type hints.
  • Loading branch information
srdjan-catalyst authored and marinaglancy committed Apr 28, 2021
1 parent a5f0b35 commit ebf19a7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/dml/tests/dml_mysqli_read_slave_test.php
Expand Up @@ -48,12 +48,12 @@ public function test_lock() : void {
$this->assertEquals(0, $DB->perf_get_reads_slave());

$DB->query_start("SELECT GET_LOCK('lock',1)", null, SQL_QUERY_SELECT);
$this->assertEquals('test_rw', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_rw());
$DB->query_end(null);
$this->assertEquals(0, $DB->perf_get_reads_slave());

$DB->query_start("SELECT RELEASE_LOCK('lock',1)", null, SQL_QUERY_SELECT);
$this->assertEquals('test_rw', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_rw());
$DB->query_end(null);
$this->assertEquals(0, $DB->perf_get_reads_slave());
}
Expand Down
8 changes: 4 additions & 4 deletions lib/dml/tests/dml_pgsql_read_slave_test.php
Expand Up @@ -62,13 +62,13 @@ public function test_cursors() : void {
// Read from the non-written to table cursor.
$sql = 'FETCH 1 FROM crs1';
$DB->query_start($sql, null, SQL_QUERY_AUX);
$this->assertEquals('test_ro', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_ro());
$DB->query_end(null);

// Read from the written to table cursor.
$sql = 'FETCH 1 FROM crs2';
$DB->query_start($sql, null, SQL_QUERY_AUX);
$this->assertEquals('test_rw', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_rw());
$DB->query_end(null);
}

Expand All @@ -83,7 +83,7 @@ public function test_read_pg_table() : void {
$this->assertEquals(0, $DB->perf_get_reads_slave());

$DB->query_start('SELECT pg_whatever(1)', null, SQL_QUERY_SELECT);
$this->assertEquals('test_ro', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_ro());
$DB->query_end(null);
$this->assertEquals(1, $DB->perf_get_reads_slave());
}
Expand All @@ -101,7 +101,7 @@ public function test_read_pg_lock_table() : void {

foreach (['pg_try_advisory_lock', 'pg_advisory_unlock'] as $fn) {
$DB->query_start("SELECT $fn(1)", null, SQL_QUERY_SELECT);
$this->assertEquals('test_rw', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_rw());
$DB->query_end(null);
$this->assertEquals(0, $DB->perf_get_reads_slave());
}
Expand Down
37 changes: 35 additions & 2 deletions lib/dml/tests/fixtures/test_moodle_read_slave_trait.php
Expand Up @@ -46,14 +46,47 @@ public function __construct($external = false) {
// @codingStandardsIgnoreEnd
parent::__construct($external);

$rw = fopen("php://memory", 'r+');
fputs($rw, 'rw');

$ro = fopen("php://memory", 'r+');
fputs($ro, 'ro');

$this->wantreadslave = true;
$this->dbhwrite = 'test_rw';
$this->dbhreadonly = 'test_ro';
$this->dbhwrite = $rw;
$this->dbhreadonly = $ro;
$this->set_db_handle($this->dbhwrite);

$this->temptables = new moodle_temptables($this);
}

/**
* 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;
}

/**
* Check db handle is rw
* @return bool
*/
public function db_handle_is_rw() {
return $this->db_handle_is('rw');
}

/**
* Check db handle is ro
* @return bool
*/
public function db_handle_is_ro() {
return $this->db_handle_is('ro');
}

/**
* Upgrade to public
* @return resource
Expand Down

0 comments on commit ebf19a7

Please sign in to comment.