diff options
author | Andreas Fischer <bantu@phpbb.com> | 2012-12-04 20:26:43 +0100 |
---|---|---|
committer | Andreas Fischer <bantu@phpbb.com> | 2012-12-04 20:26:43 +0100 |
commit | 2fdd039e5223b7acea3795811326945bd649bf49 (patch) | |
tree | b0efba74b1bdc2eaad2443d7e65f61c66ee66a1e /tests | |
parent | d7a23df2d9c3a9f93d8477e164edef0d263d8dcb (diff) | |
parent | 3e093c282a63df4d16b212859fd8137fd2bbca81 (diff) | |
download | forums-2fdd039e5223b7acea3795811326945bd649bf49.tar forums-2fdd039e5223b7acea3795811326945bd649bf49.tar.gz forums-2fdd039e5223b7acea3795811326945bd649bf49.tar.bz2 forums-2fdd039e5223b7acea3795811326945bd649bf49.tar.xz forums-2fdd039e5223b7acea3795811326945bd649bf49.zip |
Merge remote-tracking branch 'p/ticket/10103' into develop
* p/ticket/10103:
[ticket/10103] New and improved wording.
[ticket/10103] Assert with messages.
[ticket/10103] assertLessThan/assertGreaterThan.
[ticket/10103] Inline assignment is bad?
[ticket/10103] $rv had too few characters.
[ticket/10103] Correct flock class documentation.
[ticket/10103] Try a longer sleep for travis.
[ticket/10103] Convert the rest of the tree to flock class.
[ticket/10103] Test for flock lock class, with concurrency no less.
[ticket/10103] Use flock lock class in messenger.
[ticket/10103] Factor out flock lock class.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lock/flock_test.php | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/tests/lock/flock_test.php b/tests/lock/flock_test.php new file mode 100644 index 0000000000..1edc96b3a4 --- /dev/null +++ b/tests/lock/flock_test.php @@ -0,0 +1,109 @@ +<?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); + $lock->release(); + + $ok = $lock->acquire(); + $this->assertTrue($ok); + $lock->release(); + + $ok = $lock->acquire(); + $this->assertTrue($ok); + $lock->release(); + } + + /* 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->assertGreaterThan(0.5, $delta, 'First lock acquired too soon'); + + $lock->release(); + + // acquire again, this should be instantaneous + $start = time(); + $ok = $lock->acquire(); + $delta = time() - $start; + $this->assertTrue($ok); + $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); + sleep(2); + $lock->release(); + + // and go away silently + pcntl_exec('/usr/bin/env', array('true')); + } + } +} |