| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\ObjectCache; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWikiCoversValidator; |
| 7 |
use PHPUnit\Framework\TestCase; |
| 8 |
use Psr\Log\NullLogger; |
| 9 |
use UDPTransport; |
| 10 |
use Wikimedia\ObjectCache\BagOStuff; |
| 11 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 12 |
use Wikimedia\Stats\Metrics\MetricInterface; |
| 13 |
use Wikimedia\Stats\NullStatsdDataFactory; |
| 14 |
use Wikimedia\Stats\OutputFormats; |
| 15 |
use Wikimedia\Stats\StatsCache; |
| 16 |
use Wikimedia\Stats\StatsFactory; |
| 17 |
use Wikimedia\TestingAccessWrapper; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class HashBagOStuffTest extends TestCase { |
| 26 |
use MediaWikiCoversValidator; |
| 27 |
|
| 28 |
public function testConstruct() { |
| 29 |
$this->assertInstanceOf( |
| 30 |
HashBagOStuff::class, |
| 31 |
new HashBagOStuff() |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
public function testQoS() { |
| 36 |
$bag = new HashBagOStuff(); |
| 37 |
|
| 38 |
$this->assertSame( |
| 39 |
BagOStuff::QOS_DURABILITY_SCRIPT, |
| 40 |
$bag->getQoS( BagOStuff::ATTR_DURABILITY ) |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
public function testConstructBadZero() { |
| 45 |
$this->expectException( InvalidArgumentException::class ); |
| 46 |
$cache = new HashBagOStuff( [ 'maxKeys' => 0 ] ); |
| 47 |
} |
| 48 |
|
| 49 |
public function testConstructBadNeg() { |
| 50 |
$this->expectException( InvalidArgumentException::class ); |
| 51 |
$cache = new HashBagOStuff( [ 'maxKeys' => -1 ] ); |
| 52 |
} |
| 53 |
|
| 54 |
public function testConstructBadType() { |
| 55 |
$this->expectException( InvalidArgumentException::class ); |
| 56 |
$cache = new HashBagOStuff( [ 'maxKeys' => 'x' ] ); |
| 57 |
} |
| 58 |
|
| 59 |
public function testDelete() { |
| 60 |
$cache = new HashBagOStuff(); |
| 61 |
for ( $i = 0; $i < 10; $i++ ) { |
| 62 |
$cache->set( "key$i", 1 ); |
| 63 |
$this->assertSame( 1, $cache->get( "key$i" ) ); |
| 64 |
$cache->delete( "key$i" ); |
| 65 |
$this->assertFalse( $cache->get( "key$i" ) ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public function testClear() { |
| 70 |
$cache = new HashBagOStuff(); |
| 71 |
for ( $i = 0; $i < 10; $i++ ) { |
| 72 |
$cache->set( "key$i", 1 ); |
| 73 |
$this->assertSame( 1, $cache->get( "key$i" ) ); |
| 74 |
} |
| 75 |
$cache->clear(); |
| 76 |
for ( $i = 0; $i < 10; $i++ ) { |
| 77 |
$this->assertFalse( $cache->get( "key$i" ) ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public function testExpire() { |
| 82 |
$cache = new HashBagOStuff(); |
| 83 |
$cacheInternal = TestingAccessWrapper::newFromObject( $cache ); |
| 84 |
$cache->set( 'foo', 1 ); |
| 85 |
$cache->set( 'bar', 1, 10 ); |
| 86 |
$cache->set( 'baz', 1, -10 ); |
| 87 |
|
| 88 |
$this->assertSame( 0, $cacheInternal->bag['foo'][$cache::KEY_EXP], 'Indefinite' ); |
| 89 |
|
| 90 |
$this->assertEqualsWithDelta( |
| 91 |
time() + 10, |
| 92 |
$cacheInternal->bag['bar'][$cache::KEY_EXP], |
| 93 |
2, |
| 94 |
'Future' |
| 95 |
); |
| 96 |
$this->assertEqualsWithDelta( |
| 97 |
time() - 10, |
| 98 |
$cacheInternal->bag['baz'][$cache::KEY_EXP], |
| 99 |
2, |
| 100 |
'Past' |
| 101 |
); |
| 102 |
|
| 103 |
$this->assertSame( 1, $cache->get( 'bar' ), 'Key not expired' ); |
| 104 |
$this->assertFalse( $cache->get( 'baz' ), 'Key expired' ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function testEvictionAdd() { |
| 111 |
$cache = new HashBagOStuff( [ 'maxKeys' => 10 ] ); |
| 112 |
for ( $i = 0; $i < 10; $i++ ) { |
| 113 |
$cache->set( "key$i", 1 ); |
| 114 |
$this->assertSame( 1, $cache->get( "key$i" ) ); |
| 115 |
} |
| 116 |
for ( $i = 10; $i < 20; $i++ ) { |
| 117 |
$cache->set( "key$i", 1 ); |
| 118 |
$this->assertSame( 1, $cache->get( "key$i" ) ); |
| 119 |
$this->assertFalse( $cache->get( "key" . ( $i - 10 ) ) ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function testEvictionSet() { |
| 128 |
$cache = new HashBagOStuff( [ 'maxKeys' => 3 ] ); |
| 129 |
|
| 130 |
foreach ( [ 'foo', 'bar', 'baz' ] as $key ) { |
| 131 |
$cache->set( $key, 1 ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
$cache->set( 'foo', 1 ); |
| 136 |
|
| 137 |
|
| 138 |
$cache->set( 'quux', 1 ); |
| 139 |
|
| 140 |
|
| 141 |
foreach ( [ 'foo', 'baz', 'quux' ] as $key ) { |
| 142 |
$this->assertSame( 1, $cache->get( $key ), "Kept $key" ); |
| 143 |
} |
| 144 |
$this->assertFalse( $cache->get( 'bar' ), 'Evicted bar' ); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function testEvictionGet() { |
| 151 |
$cache = new HashBagOStuff( [ 'maxKeys' => 3 ] ); |
| 152 |
|
| 153 |
foreach ( [ 'foo', 'bar', 'baz' ] as $key ) { |
| 154 |
$cache->set( $key, 1 ); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
$cache->get( 'foo', 1 ); |
| 159 |
|
| 160 |
|
| 161 |
$cache->set( 'quux', 1 ); |
| 162 |
|
| 163 |
|
| 164 |
foreach ( [ 'foo', 'baz', 'quux' ] as $key ) { |
| 165 |
$this->assertSame( 1, $cache->get( $key ), "Kept $key" ); |
| 166 |
} |
| 167 |
$this->assertFalse( $cache->get( 'bar' ), 'Evicted bar' ); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function testUpdateOpStats() { |
| 174 |
$statsCache = new StatsCache(); |
| 175 |
$emitter = OutputFormats::getNewEmitter( |
| 176 |
'mediawiki', |
| 177 |
$statsCache, |
| 178 |
OutputFormats::getNewFormatter( OutputFormats::DOGSTATSD ) |
| 179 |
); |
| 180 |
|
| 181 |
$transport = $this->createMock( UDPTransport::class ); |
| 182 |
$transport->expects( $this->once() )->method( "emit" ) |
| 183 |
->with( |
| 184 |
"mediawiki.bagostuff_call_total:1|c|#keygroup:Foo,operation:frob |
| 185 |
mediawiki.bagostuff_call_total:1|c|#keygroup:Bar,operation:frob |
| 186 |
mediawiki.bagostuff_call_total:1|c|#keygroup:UNKNOWN,operation:frob |
| 187 |
mediawiki.bagostuff_bytes_sent_total:5|c|#keygroup:Bar,operation:frob |
| 188 |
mediawiki.bagostuff_bytes_sent_total:5|c|#keygroup:UNKNOWN,operation:frob |
| 189 |
mediawiki.bagostuff_bytes_read_total:3|c|#keygroup:Bar,operation:frob |
| 190 |
mediawiki.bagostuff_bytes_read_total:3|c|#keygroup:UNKNOWN,operation:frob |
| 191 |
mediawiki.stats_buffered_total:7|c\n" |
| 192 |
); |
| 193 |
$emitter = $emitter->withTransport( $transport ); |
| 194 |
$stats = new StatsFactory( $statsCache, $emitter, new NullLogger ); |
| 195 |
|
| 196 |
$stats->withStatsdDataFactory( new NullStatsdDataFactory() ); |
| 197 |
$cache = new HashBagOStuff( [ |
| 198 |
'stats' => $stats |
| 199 |
] ); |
| 200 |
|
| 201 |
$cache = TestingAccessWrapper::newFromObject( $cache ); |
| 202 |
$cache->updateOpStats( |
| 203 |
'frob', |
| 204 |
[ |
| 205 |
|
| 206 |
$cache->makeKey( 'Foo', '123456' ), |
| 207 |
|
| 208 |
|
| 209 |
$cache->makeGlobalKey( 'Bar', '123456' ) => [ 5, 3 ], |
| 210 |
|
| 211 |
|
| 212 |
'1337BABE-123456' => [ 5, 3 ], |
| 213 |
] |
| 214 |
); |
| 215 |
|
| 216 |
|
| 217 |
$metrics = ( TestingAccessWrapper::newFromObject( $stats ) )->cache->getAllMetrics(); |
| 218 |
$this->assertCount( 3, $metrics ); |
| 219 |
|
| 220 |
|
| 221 |
$stats->flush(); |
| 222 |
} |
| 223 |
} |