| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\ObjectCache; |
| 4 |
|
| 5 |
use MediaWikiCoversValidator; |
| 6 |
use PHPUnit\Framework\TestCase; |
| 7 |
use Wikimedia\LightweightObjectStore\StorageAwareness; |
| 8 |
use Wikimedia\ObjectCache\CachedBagOStuff; |
| 9 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 10 |
use Wikimedia\TestingAccessWrapper; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class CachedBagOStuffTest extends TestCase { |
| 17 |
|
| 18 |
use MediaWikiCoversValidator; |
| 19 |
|
| 20 |
public function testGetFromBackend() { |
| 21 |
$backend = new HashBagOStuff; |
| 22 |
$cache = new CachedBagOStuff( $backend ); |
| 23 |
|
| 24 |
$backend->set( 'foo', 'bar' ); |
| 25 |
$this->assertEquals( 'bar', $cache->get( 'foo' ) ); |
| 26 |
|
| 27 |
$backend->set( 'foo', 'baz' ); |
| 28 |
$this->assertEquals( 'bar', $cache->get( 'foo' ), 'cached' ); |
| 29 |
} |
| 30 |
|
| 31 |
public function testSetAndDelete() { |
| 32 |
$backend = new HashBagOStuff; |
| 33 |
$cache = new CachedBagOStuff( $backend ); |
| 34 |
|
| 35 |
for ( $i = 0; $i < 10; $i++ ) { |
| 36 |
$cache->set( "key$i", 1 ); |
| 37 |
$this->assertSame( 1, $cache->get( "key$i" ) ); |
| 38 |
$this->assertSame( 1, $backend->get( "key$i" ) ); |
| 39 |
|
| 40 |
$cache->delete( "key$i" ); |
| 41 |
$this->assertFalse( $cache->get( "key$i" ) ); |
| 42 |
$this->assertFalse( $backend->get( "key$i" ) ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public function testWriteCacheOnly() { |
| 47 |
$backend = new HashBagOStuff; |
| 48 |
$cache = new CachedBagOStuff( $backend ); |
| 49 |
|
| 50 |
$cache->set( 'foo', 'bar', 0, CachedBagOStuff::WRITE_CACHE_ONLY ); |
| 51 |
$this->assertEquals( 'bar', $cache->get( 'foo' ) ); |
| 52 |
$this->assertFalse( $backend->get( 'foo' ) ); |
| 53 |
|
| 54 |
$cache->set( 'foo', 'old' ); |
| 55 |
$this->assertEquals( 'old', $cache->get( 'foo' ) ); |
| 56 |
$this->assertEquals( 'old', $backend->get( 'foo' ) ); |
| 57 |
|
| 58 |
$cache->set( 'foo', 'new', 0, CachedBagOStuff::WRITE_CACHE_ONLY ); |
| 59 |
$this->assertEquals( 'new', $cache->get( 'foo' ) ); |
| 60 |
$this->assertEquals( 'old', $backend->get( 'foo' ) ); |
| 61 |
|
| 62 |
$cache->delete( 'foo', CachedBagOStuff::WRITE_CACHE_ONLY ); |
| 63 |
$this->assertEquals( 'old', $cache->get( 'foo' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
public function testCacheBackendMisses() { |
| 67 |
$backend = new HashBagOStuff; |
| 68 |
$cache = new CachedBagOStuff( $backend ); |
| 69 |
|
| 70 |
|
| 71 |
$this->assertFalse( $cache->get( 'foo' ) ); |
| 72 |
|
| 73 |
|
| 74 |
$backend->set( 'foo', true ); |
| 75 |
|
| 76 |
|
| 77 |
$this->assertFalse( $cache->get( 'foo' ) ); |
| 78 |
|
| 79 |
|
| 80 |
$backend->set( 'bar', true ); |
| 81 |
$this->assertTrue( $cache->get( 'bar' ) ); |
| 82 |
} |
| 83 |
|
| 84 |
public function testExpire() { |
| 85 |
$backend = $this->getMockBuilder( HashBagOStuff::class ) |
| 86 |
->onlyMethods( [ 'deleteObjectsExpiringBefore' ] ) |
| 87 |
->getMock(); |
| 88 |
$backend->expects( $this->once() ) |
| 89 |
->method( 'deleteObjectsExpiringBefore' ) |
| 90 |
->willReturn( false ); |
| 91 |
|
| 92 |
$cache = new CachedBagOStuff( $backend ); |
| 93 |
$cache->deleteObjectsExpiringBefore( '20110401000000' ); |
| 94 |
} |
| 95 |
|
| 96 |
public function testMakeKey() { |
| 97 |
$backend = $this->getMockBuilder( HashBagOStuff::class ) |
| 98 |
->setConstructorArgs( [ [ 'keyspace' => 'magic' ] ] ) |
| 99 |
->onlyMethods( [ 'makeKey' ] ) |
| 100 |
->getMock(); |
| 101 |
$backend->method( 'makeKey' ) |
| 102 |
->willReturn( 'special/logic' ); |
| 103 |
|
| 104 |
$cache = new CachedBagOStuff( $backend ); |
| 105 |
|
| 106 |
$this->assertSame( 'special/logic', $backend->makeKey( 'special', 'logic' ) ); |
| 107 |
$this->assertSame( |
| 108 |
'magic:special:logic', |
| 109 |
$cache->makeKey( 'special', 'logic' ), |
| 110 |
"Backend keyspace used" |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
public function testMakeGlobalKey() { |
| 115 |
$backend = $this->getMockBuilder( HashBagOStuff::class ) |
| 116 |
->setConstructorArgs( [ [ 'keyspace' => 'magic' ] ] ) |
| 117 |
->onlyMethods( [ 'makeGlobalKey' ] ) |
| 118 |
->getMock(); |
| 119 |
$backend->method( 'makeGlobalKey' ) |
| 120 |
->willReturn( 'special/logic' ); |
| 121 |
|
| 122 |
$cache = new CachedBagOStuff( $backend ); |
| 123 |
|
| 124 |
$this->assertSame( 'special/logic', $backend->makeGlobalKey( 'special', 'logic' ) ); |
| 125 |
$this->assertSame( 'global:special:logic', $cache->makeGlobalKey( 'special', 'logic' ) ); |
| 126 |
} |
| 127 |
|
| 128 |
public function testErrorHandling() { |
| 129 |
$backend = new HashBagOStuff; |
| 130 |
$cache = new CachedBagOStuff( $backend ); |
| 131 |
$wrapper = TestingAccessWrapper::newFromObject( $cache ); |
| 132 |
$key = $cache->makeKey( 'test' ); |
| 133 |
|
| 134 |
$wp = $cache->watchErrors(); |
| 135 |
$cache->get( $key ); |
| 136 |
$this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp ) ); |
| 137 |
|
| 138 |
$wrapper->setLastError( StorageAwareness::ERR_UNREACHABLE ); |
| 139 |
$this->assertSame( StorageAwareness::ERR_UNREACHABLE, $cache->getLastError() ); |
| 140 |
$this->assertSame( StorageAwareness::ERR_UNREACHABLE, $cache->getLastError( $wp ) ); |
| 141 |
|
| 142 |
$wp = $cache->watchErrors(); |
| 143 |
$wrapper->setLastError( StorageAwareness::ERR_UNEXPECTED ); |
| 144 |
$wp2 = $cache->watchErrors(); |
| 145 |
$this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError() ); |
| 146 |
$this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp ) ); |
| 147 |
$this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp2 ) ); |
| 148 |
|
| 149 |
$cache->get( $key ); |
| 150 |
$this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError() ); |
| 151 |
$this->assertSame( StorageAwareness::ERR_UNEXPECTED, $cache->getLastError( $wp ) ); |
| 152 |
$this->assertSame( StorageAwareness::ERR_NONE, $cache->getLastError( $wp2 ) ); |
| 153 |
} |
| 154 |
} |