MDL-66935 core_lock: Fix resource key clashes in db and postgres locks

This commit is contained in:
Brendan Heywood 2019-10-17 11:01:33 +11:00
parent 9f997f9bd7
commit 7e086935e3
5 changed files with 86 additions and 32 deletions

View file

@ -46,31 +46,57 @@ class lock_config_testcase extends advanced_testcase {
if (isset($CFG->lock_factory)) {
$original = $CFG->lock_factory;
}
$originalfilelocking = null;
if (isset($CFG->preventfilelocking)) {
$originalfilelocking = $CFG->preventfilelocking;
}
// Test no configuration.
unset($CFG->lock_factory);
$CFG->preventfilelocking = 0;
$factory = \core\lock\lock_config::get_lock_factory('cache');
$this->assertNotEmpty($factory, 'Get a default factory with no configuration');
$CFG->lock_factory = '\core\lock\file_lock_factory';
// Test explicit broken lock.
$CFG->lock_factory = '\core\lock\not_a_lock_factory';
try {
$factory = \core\lock\lock_config::get_lock_factory('cache');
$this->fail('Exception expected');
} catch (moodle_exception $ex) {
$this->assertInstanceOf('coding_exception', $ex);
}
// Test explicit file locks.
$CFG->lock_factory = '\core\lock\file_lock_factory';
$factory = \core\lock\lock_config::get_lock_factory('cache');
$this->assertTrue($factory instanceof \core\lock\file_lock_factory,
'Get a default factory with a set configuration');
'Get an explicit file lock factory');
// Test explicit file locks but with file locks prevented.
$CFG->preventfilelocking = 1;
try {
$factory = \core\lock\lock_config::get_lock_factory('cache');
$this->fail('Exception expected');
} catch (moodle_exception $ex) {
$this->assertInstanceOf('coding_exception', $ex);
}
// Test explicit db locks.
$CFG->lock_factory = '\core\lock\db_record_lock_factory';
$factory = \core\lock\lock_config::get_lock_factory('cache');
$this->assertTrue($factory instanceof \core\lock\db_record_lock_factory,
'Get a default factory with a changed configuration');
'Get an explicit db record lock factory');
if ($original) {
$CFG->lock_factory = $original;
} else {
unset($CFG->lock_factory);
}
if ($originalfilelocking) {
$CFG->preventfilelocking = $originalfilelocking;
} else {
unset($CFG->preventfilelocking);
}
}
}

View file

@ -44,11 +44,26 @@ class lock_testcase extends advanced_testcase {
}
/**
* Run a suite of tests on a lock factory.
* @param \core\lock\lock_factory $lockfactory - A lock factory to test
* Run a suite of tests on a lock factory class.
*
* @param class $lockfactoryclass - A lock factory class to test
*/
protected function run_on_lock_factory(\core\lock\lock_factory $lockfactory) {
protected function run_on_lock_factory($lockfactoryclass) {
$modassignfactory = new $lockfactoryclass('mod_assign');
$tooltaskfactory = new $lockfactoryclass('tool_task');
// Test for lock clashes between lock stores.
$assignlock = $modassignfactory->get_lock('abc', 0);
$this->assertNotEmpty($assignlock, 'Get a lock "abc" from store "mod_assign"');
$tasklock = $tooltaskfactory->get_lock('abc', 0);
$this->assertNotEmpty($tasklock, 'Get a lock "abc" from store "tool_task"');
$assignlock->release();
$tasklock->release();
$lockfactory = new $lockfactoryclass('default');
if ($lockfactory->is_available()) {
// This should work.
$lock1 = $lockfactory->get_lock('abc', 2);
@ -111,20 +126,16 @@ class lock_testcase extends advanced_testcase {
}
/**
* Tests the testable lock factories.
* Tests the testable lock factories classes.
* @return void
*/
public function test_locks() {
// Run the suite on the current configured default (may be non-core).
$defaultfactory = \core\lock\lock_config::get_lock_factory('default');
$this->run_on_lock_factory($defaultfactory);
$this->run_on_lock_factory(\core\lock\lock_config::get_lock_factory_class());
// Manually create the core no-configuration factories.
$dblockfactory = new \core\lock\db_record_lock_factory('test');
$this->run_on_lock_factory($dblockfactory);
$filelockfactory = new \core\lock\file_lock_factory('test');
$this->run_on_lock_factory($filelockfactory);
$this->run_on_lock_factory(\core\lock\db_record_lock_factory::class);
$this->run_on_lock_factory(\core\lock\file_lock_factory::class);
}