| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use Wikimedia\FileBackend\MemoryFileBackend; |
| 5 |
use Wikimedia\Rdbms\LBFactory; |
| 6 |
use Wikimedia\Rdbms\LoadBalancer; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ExternalStoreFactoryTest extends MediaWikiIntegrationTestCase { |
| 13 |
|
| 14 |
public function testExternalStoreFactory_noStores1() { |
| 15 |
$factory = new ExternalStoreFactory( [], [], 'test-id' ); |
| 16 |
$this->expectException( ExternalStoreException::class ); |
| 17 |
$factory->getStore( 'ForTesting' ); |
| 18 |
} |
| 19 |
|
| 20 |
public function testExternalStoreFactory_noStores2() { |
| 21 |
$factory = new ExternalStoreFactory( [], [], 'test-id' ); |
| 22 |
$this->expectException( ExternalStoreException::class ); |
| 23 |
$factory->getStore( 'foo' ); |
| 24 |
} |
| 25 |
|
| 26 |
public static function provideStoreNames() { |
| 27 |
yield 'Same case as construction' => [ 'ForTesting' ]; |
| 28 |
yield 'All lower case' => [ 'fortesting' ]; |
| 29 |
yield 'All upper case' => [ 'FORTESTING' ]; |
| 30 |
yield 'Mix of cases' => [ 'FOrTEsTInG' ]; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function testExternalStoreFactory_someStore_protoMatch( $proto ) { |
| 37 |
$factory = new ExternalStoreFactory( [ 'ForTesting' ], [], 'test-id' ); |
| 38 |
$store = $factory->getStore( $proto ); |
| 39 |
$this->assertInstanceOf( ExternalStoreForTesting::class, $store ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function testExternalStoreFactory_someStore_noProtoMatch( $proto ) { |
| 46 |
$factory = new ExternalStoreFactory( [ 'SomeOtherClassName' ], [], 'test-id' ); |
| 47 |
$this->expectException( ExternalStoreException::class ); |
| 48 |
$factory->getStore( $proto ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function testStoreFactoryBasic() { |
| 57 |
$active = [ 'memory', 'mwstore' ]; |
| 58 |
$defaults = [ 'memory://cluster1', 'memory://cluster2', 'mwstore://memstore1' ]; |
| 59 |
$esFactory = new ExternalStoreFactory( $active, $defaults, 'db-prefix' ); |
| 60 |
$this->overrideConfigValue( MainConfigNames::FileBackends, [ |
| 61 |
[ |
| 62 |
'name' => 'memstore1', |
| 63 |
'class' => MemoryFileBackend::class, |
| 64 |
'domain' => 'its-all-in-your-head', |
| 65 |
'readOnly' => 'reason is a lie', |
| 66 |
'lockManager' => 'nullLockManager' |
| 67 |
] |
| 68 |
] ); |
| 69 |
|
| 70 |
$this->assertEquals( $active, $esFactory->getProtocols() ); |
| 71 |
$this->assertEquals( $defaults, $esFactory->getWriteBaseUrls() ); |
| 72 |
|
| 73 |
|
| 74 |
$store = $esFactory->getStore( 'memory' ); |
| 75 |
$this->assertInstanceOf( ExternalStoreMemory::class, $store ); |
| 76 |
$this->assertFalse( $store->isReadOnly( 'cluster1' ), "Location is writable" ); |
| 77 |
$this->assertFalse( $store->isReadOnly( 'cluster2' ), "Location is writable" ); |
| 78 |
|
| 79 |
$mwStore = $esFactory->getStore( 'mwstore' ); |
| 80 |
$this->assertTrue( $mwStore->isReadOnly( 'memstore1' ), "Location is read-only" ); |
| 81 |
|
| 82 |
$lb = $this->createMock( LoadBalancer::class ); |
| 83 |
$lb->method( 'getReadOnlyReason' )->willReturn( 'Locked' ); |
| 84 |
$lbFactory = $this->createMock( LBFactory::class ); |
| 85 |
$lbFactory->method( 'getExternalLB' )->willReturn( $lb ); |
| 86 |
|
| 87 |
$this->setService( 'DBLoadBalancerFactory', $lbFactory ); |
| 88 |
|
| 89 |
$active = [ 'db', 'mwstore' ]; |
| 90 |
$defaults = [ 'db://clusterX' ]; |
| 91 |
$esFactory = new ExternalStoreFactory( $active, $defaults, 'db-prefix' ); |
| 92 |
$this->assertEquals( $active, $esFactory->getProtocols() ); |
| 93 |
$this->assertEquals( $defaults, $esFactory->getWriteBaseUrls() ); |
| 94 |
|
| 95 |
$store->clear(); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function testStoreFactoryReadWrite() { |
| 103 |
$active = [ 'memory' ]; |
| 104 |
$defaults = [ 'memory://cluster1', 'memory://cluster2' ]; |
| 105 |
$esFactory = new ExternalStoreFactory( $active, $defaults, 'db-prefix' ); |
| 106 |
$access = new ExternalStoreAccess( $esFactory ); |
| 107 |
|
| 108 |
|
| 109 |
$storeLocal = $esFactory->getStore( 'memory' ); |
| 110 |
|
| 111 |
$storeOther = $esFactory->getStore( 'memory', [ 'domain' => 'other' ] ); |
| 112 |
$this->assertInstanceOf( ExternalStoreMemory::class, $storeLocal ); |
| 113 |
$this->assertInstanceOf( ExternalStoreMemory::class, $storeOther ); |
| 114 |
|
| 115 |
$v1 = wfRandomString(); |
| 116 |
$v2 = wfRandomString(); |
| 117 |
$v3 = wfRandomString(); |
| 118 |
|
| 119 |
$this->assertFalse( $storeLocal->fetchFromURL( 'memory://cluster1/1' ) ); |
| 120 |
|
| 121 |
$url1 = 'memory://cluster1/1'; |
| 122 |
$this->assertEquals( |
| 123 |
$url1, |
| 124 |
$esFactory->getStoreForUrl( 'memory://cluster1' ) |
| 125 |
->store( $esFactory->getStoreLocationFromUrl( 'memory://cluster1' ), $v1 ) |
| 126 |
); |
| 127 |
$this->assertEquals( |
| 128 |
$v1, |
| 129 |
$esFactory->getStoreForUrl( 'memory://cluster1/1' ) |
| 130 |
->fetchFromURL( 'memory://cluster1/1' ) |
| 131 |
); |
| 132 |
$this->assertEquals( $v1, $storeLocal->fetchFromURL( 'memory://cluster1/1' ) ); |
| 133 |
|
| 134 |
$url2 = $access->insert( $v2 ); |
| 135 |
$url3 = $access->insert( $v3, [ 'domain' => 'other' ] ); |
| 136 |
$this->assertNotFalse( $url2 ); |
| 137 |
$this->assertNotFalse( $url3 ); |
| 138 |
|
| 139 |
$this->assertEquals( $v2, $storeLocal->fetchFromURL( $url2 ) ); |
| 140 |
$this->assertEquals( $v3, $storeOther->fetchFromURL( $url3 ) ); |
| 141 |
$this->assertFalse( $storeOther->fetchFromURL( $url2 ) ); |
| 142 |
$this->assertFalse( $storeLocal->fetchFromURL( $url3 ) ); |
| 143 |
|
| 144 |
$res = $access->fetchFromURLs( [ $url1, $url2, $url3 ] ); |
| 145 |
$this->assertEquals( [ $url1 => $v1, $url2 => $v2, $url3 => false ], $res, "Local-only" ); |
| 146 |
|
| 147 |
$storeLocal->clear(); |
| 148 |
$storeOther->clear(); |
| 149 |
} |
| 150 |
} |