mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-07 20:08:53 +00:00
[ticket/10103] Test for flock lock class, with concurrency no less.
PHPBB3-10103
This commit is contained in:
parent
4010f4085a
commit
f72e435759
1 changed files with 108 additions and 0 deletions
108
tests/lock/flock_test.php
Normal file
108
tests/lock/flock_test.php
Normal file
|
@ -0,0 +1,108 @@
|
|||
<?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';
|
||||
|
||||
if ($pid = pcntl_fork())
|
||||
{
|
||||
// parent
|
||||
// wait 0.5 s, acquire the lock, note how long it took
|
||||
sleep(0.5);
|
||||
|
||||
$lock = new phpbb_lock_flock($path);
|
||||
$start = time();
|
||||
$ok = $lock->acquire();
|
||||
$delta = time() - $start;
|
||||
$this->assertTrue($ok);
|
||||
$this->assertTrue($delta > 0.5);
|
||||
|
||||
$lock->release();
|
||||
|
||||
// acquire again, this should be instantaneous
|
||||
$start = time();
|
||||
$ok = $lock->acquire();
|
||||
$delta = time() - $start;
|
||||
$this->assertTrue($ok);
|
||||
$this->assertTrue($delta < 0.1);
|
||||
|
||||
// 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'));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue