* @return bool Success
*/
protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
- if ( !$this->lock( $key, 6 ) ) {
+ if ( $attempts <= 1 ) {
+ $timeout = 0; // clearly intended to be "non-blocking"
+ } else {
+ $timeout = 3;
+ }
+
+ if ( !$this->lock( $key, $timeout ) ) {
return false;
}
* @covers BagOStuff::mergeViaCas
*/
public function testMerge() {
+ $calls = 0;
$key = $this->cache->makeKey( self::TEST_KEY );
- $callback = function ( BagOStuff $cache, $key, $oldVal ) {
+ $callback = function ( BagOStuff $cache, $key, $oldVal ) use ( &$calls ) {
+ ++$calls;
+
return ( $oldVal === false ) ? 'merged' : $oldVal . 'merged';
};
$merged = $this->cache->merge( $key, $callback, 5 );
$this->assertTrue( $merged );
$this->assertEquals( 'mergedmerged', $this->cache->get( $key ) );
+
+ $calls = 0;
+ $this->cache->lock( $key );
+ $this->assertFalse( $this->cache->merge( $key, $callback, 1 ), 'Non-blocking merge' );
+ $this->cache->unlock( $key );
+ $this->assertEquals( 0, $calls );
}
/**
DeferredUpdates::doUpdates();
}
+
+ /**
+ * @covers BagOStuff::lock()
+ * @covers BagOStuff::unlock()
+ */
+ public function testLocking() {
+ $key = 'test';
+ $this->assertTrue( $this->cache->lock( $key ) );
+ $this->assertFalse( $this->cache->lock( $key ) );
+ $this->assertTrue( $this->cache->unlock( $key ) );
+
+ $key2 = 'test2';
+ $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
+ $this->assertTrue( $this->cache->lock( $key2, 5, 5, 'rclass' ) );
+ $this->assertTrue( $this->cache->unlock( $key2 ) );
+ $this->assertTrue( $this->cache->unlock( $key2 ) );
+ }
}