diff options
Diffstat (limited to 'tests/lock')
-rw-r--r-- | tests/lock/db_test.php | 97 | ||||
-rw-r--r-- | tests/lock/fixtures/config.xml | 13 | ||||
-rw-r--r-- | tests/lock/flock_test.php | 120 |
3 files changed, 230 insertions, 0 deletions
diff --git a/tests/lock/db_test.php b/tests/lock/db_test.php new file mode 100644 index 0000000000..da689b7fa3 --- /dev/null +++ b/tests/lock/db_test.php @@ -0,0 +1,97 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_lock_db_test extends phpbb_database_test_case +{ + private $db; + private $config; + private $lock; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); + } + + public function setUp() + { + global $db, $config; + + $db = $this->db = $this->new_dbal(); + $config = $this->config = new \phpbb\config\config(array('rand_seed' => '', 'rand_seed_last_update' => '0')); + set_config(null, null, null, $this->config); + $this->lock = new \phpbb\lock\db('test_lock', $this->config, $this->db); + } + + public function test_new_lock() + { + $this->assertFalse($this->lock->owns_lock()); + + $this->assertTrue($this->lock->acquire()); + $this->assertTrue($this->lock->owns_lock()); + $this->assertTrue(isset($this->config['test_lock']), 'Lock was created'); + + $lock2 = new \phpbb\lock\db('test_lock', $this->config, $this->db); + $this->assertFalse($lock2->acquire()); + $this->assertFalse($lock2->owns_lock()); + + $this->lock->release(); + $this->assertFalse($this->lock->owns_lock()); + $this->assertEquals('0', $this->config['test_lock'], 'Lock was released'); + } + + public function test_expire_lock() + { + $lock = new \phpbb\lock\db('foo_lock', $this->config, $this->db); + $this->assertTrue($lock->acquire()); + } + + public function test_double_lock() + { + $this->assertFalse($this->lock->owns_lock()); + + $this->assertTrue($this->lock->acquire()); + $this->assertTrue($this->lock->owns_lock()); + $this->assertTrue(isset($this->config['test_lock']), 'Lock was created'); + + $value = $this->config['test_lock']; + + $this->assertFalse($this->lock->acquire()); + $this->assertTrue($this->lock->owns_lock()); + $this->assertEquals($value, $this->config['test_lock'], 'Second lock failed'); + + $this->lock->release(); + $this->assertFalse($this->lock->owns_lock()); + $this->assertEquals('0', $this->config['test_lock'], 'Lock was released'); + } + + public function test_double_unlock() + { + $this->assertTrue($this->lock->acquire()); + $this->assertTrue($this->lock->owns_lock()); + $this->assertFalse(empty($this->config['test_lock']), 'First lock is acquired'); + + $this->lock->release(); + $this->assertFalse($this->lock->owns_lock()); + $this->assertEquals('0', $this->config['test_lock'], 'First lock is released'); + + $lock2 = new \phpbb\lock\db('test_lock', $this->config, $this->db); + $this->assertTrue($lock2->acquire()); + $this->assertTrue($lock2->owns_lock()); + $this->assertFalse(empty($this->config['test_lock']), 'Second lock is acquired'); + + $this->lock->release(); + $this->assertTrue($lock2->owns_lock()); + $this->assertFalse(empty($this->config['test_lock']), 'Double release of first lock is ignored'); + + $lock2->release(); + $this->assertEquals('0', $this->config['test_lock'], 'Second lock is released'); + } +} diff --git a/tests/lock/fixtures/config.xml b/tests/lock/fixtures/config.xml new file mode 100644 index 0000000000..f36c8b929a --- /dev/null +++ b/tests/lock/fixtures/config.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_config"> + <column>config_name</column> + <column>config_value</column> + <column>is_dynamic</column> + <row> + <value>foo_lock</value> + <value>1 abcd</value> + <value>1</value> + </row> + </table> +</dataset> diff --git a/tests/lock/flock_test.php b/tests/lock/flock_test.php new file mode 100644 index 0000000000..5e5ac5aa78 --- /dev/null +++ b/tests/lock/flock_test.php @@ -0,0 +1,120 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_lock_flock_test extends phpbb_test_case +{ + public function test_lock() + { + $path = __DIR__ . '/../tmp/precious'; + + $lock = new \phpbb\lock\flock($path); + $ok = $lock->acquire(); + $this->assertTrue($ok); + $lock->release(); + } + + public function test_consecutive_locking() + { + $path = __DIR__ . '/../tmp/precious'; + + $lock = new \phpbb\lock\flock($path); + $ok = $lock->acquire(); + $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); + $lock->release(); + $this->assertFalse($lock->owns_lock()); + + $ok = $lock->acquire(); + $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); + $lock->release(); + $this->assertFalse($lock->owns_lock()); + + $ok = $lock->acquire(); + $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); + $lock->release(); + $this->assertFalse($lock->owns_lock()); + } + + /* This hangs the process. + public function test_concurrent_locking_fail() + { + $path = __DIR__ . '/../tmp/precious'; + + $lock1 = new \phpbb\lock\flock($path); + $ok = $lock1->acquire(); + $this->assertTrue($ok); + + $lock2 = new \phpbb\lock\flock($path); + $ok = $lock2->acquire(); + $this->assertFalse($ok); + + $lock->release(); + $ok = $lock2->acquire(); + $this->assertTrue($ok); + } + */ + + public function test_concurrent_locking() + { + if (!function_exists('pcntl_fork')) + { + $this->markTestSkipped('pcntl extension and pcntl_fork are required for this test'); + } + + $path = __DIR__ . '/../tmp/precious'; + + $pid = pcntl_fork(); + if ($pid) + { + // parent + // wait 0.5 s, acquire the lock, note how long it took + sleep(1); + + $lock = new \phpbb\lock\flock($path); + $start = time(); + $ok = $lock->acquire(); + $delta = time() - $start; + $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); + $this->assertGreaterThan(0.5, $delta, 'First lock acquired too soon'); + + $lock->release(); + $this->assertFalse($lock->owns_lock()); + + // acquire again, this should be instantaneous + $start = time(); + $ok = $lock->acquire(); + $delta = time() - $start; + $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); + $this->assertLessThan(0.1, $delta, 'Second lock not acquired instantaneously'); + + // reap the child + $status = null; + pcntl_waitpid($pid, $status); + } + else + { + // child + // immediately acquire the lock and sleep for 2 s + $lock = new \phpbb\lock\flock($path); + $ok = $lock->acquire(); + $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); + sleep(2); + $lock->release(); + $this->assertFalse($lock->owns_lock()); + + // and go away silently + pcntl_exec('/usr/bin/env', array('true')); + } + } +} |