wiki.techinc.nl/tests/phpunit/unit/includes/Settings/Source/FileSourceTest.php
Dan Duvall a824da0f34 Support stale cached settings for failover
Require a `CacheableSource` implementation to signal whether stale
settings may be served from the cache by implementing an
`allowsStaleLoad()` method. This will be useful for sources (e.g. etcd)
that expect some degree of unavailability and failover during
unavailability is desired.

In order to support stale results, `CachedSource` sets cache items with
an indefinite TTL but uses the computed expiry timestamp in the envelope
to consider whether the item is considered expired as opposed to a true
miss where the item is malformed or not present in the cache store.  If
a `SettingsBuilderException` is thrown during the source's `load()`
method but the cache check was not a real miss, the stale cached results
are returned.

Note there is currently no pruning of cache items so it is advised that
`CacheableSource` implementations for which stale loads are allowed also
implement an immutable `getHashKey()` based on constructor arguments.

Bug: T296771
Change-Id: Ida0698a237dc0939230799700b6f54956f033d50
2022-01-21 13:31:09 -08:00

70 lines
1.8 KiB
PHP

<?php
namespace MediaWiki\Tests\Unit\Settings\Source;
use MediaWiki\Settings\SettingsBuilderException;
use MediaWiki\Settings\Source\FileSource;
use MediaWiki\Settings\Source\Format\JsonFormat;
use PHPUnit\Framework\TestCase;
/**
* @covers \MediaWiki\Settings\Source\FileSource
*/
class FileSourceTest extends TestCase {
public function testAllowsStaleLoad() {
$source = new FileSource( __DIR__ . 'foo.json' );
$this->assertFalse( $source->allowsStaleLoad() );
}
public function testLoad() {
$source = new FileSource( __DIR__ . '/fixtures/settings.json' );
$settings = $source->load();
$this->assertSame(
[ 'config' => [ 'MySetting' => 'BlaBla' ] ],
$source->load()
);
}
public function testLoadFormat() {
$source = new FileSource( __DIR__ . '/fixtures/settings.json', new JsonFormat() );
$settings = $source->load();
$this->assertSame(
[ 'config' => [ 'MySetting' => 'BlaBla' ] ],
$source->load()
);
}
public function testLoadBadFormat() {
$source = new FileSource( __DIR__ . '/fixtures/bad.json', new JsonFormat() );
$this->expectException( SettingsBuilderException::class );
$settings = $source->load();
}
public function testLoadDirectory() {
$source = new FileSource( __DIR__ . '/fixtures/dir.json' );
$this->expectException( SettingsBuilderException::class );
$settings = $source->load();
}
public function testLoadNoSuitableFormats() {
$source = new FileSource( __DIR__ . '/fixtures/settings.toml', new JsonFormat() );
$this->expectException( SettingsBuilderException::class );
$settings = $source->load();
}
public function testGetHashKey() {
$source = new FileSource( __DIR__ . '/fixtures/settings.json' );
// We can't reliably mock the filesystem stat so simply ensure the
// method returns and is non-zero in length
$this->assertNotEmpty( $source->getHashKey() );
}
}