1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-04-20 07:42:09 +02:00

[ticket/11495] Add owns_lock() method to lock classes

PHPBB3-11495
This commit is contained in:
Joas Schilling 2013-04-30 15:48:29 +02:00
parent 055ee41065
commit 714092ab4e
4 changed files with 47 additions and 0 deletions

View File

@ -116,6 +116,17 @@ class phpbb_lock_db
return $this->locked;
}
/**
* Does this process own the lock?
*
* @return bool true if lock is owned
* false otherwise
*/
public function owns_lock()
{
return (bool) $this->locked;
}
/**
* Releases the lock.
*

View File

@ -110,6 +110,17 @@ class phpbb_lock_flock
return (bool) $this->lock_fp;
}
/**
* Does this process own the lock?
*
* @return bool true if lock is owned
* false otherwise
*/
public function owns_lock()
{
return (bool) $this->lock_fp;
}
/**
* Releases the lock.
*

View File

@ -32,13 +32,18 @@ class phpbb_lock_db_test extends phpbb_database_test_case
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');
}
@ -50,31 +55,40 @@ class phpbb_lock_db_test extends phpbb_database_test_case
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();

View File

@ -26,15 +26,21 @@ class phpbb_lock_flock_test extends phpbb_test_case
$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.
@ -77,15 +83,18 @@ class phpbb_lock_flock_test extends phpbb_test_case
$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
@ -99,8 +108,10 @@ class phpbb_lock_flock_test extends phpbb_test_case
$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'));