Improvements to tests

This commit is contained in:
Daniil Gentili 2024-03-31 21:36:36 +02:00
parent 17b57d0600
commit 56cf1eb678
3 changed files with 104 additions and 65 deletions

View File

@ -170,8 +170,12 @@ final class CacheContainer
}
foreach ($updatedValues as $key => $value) {
if (($newValues[$key] = $this->cache[$key]) === $value) {
// The value we wrote is equal to the latest value,
// turn into a read-cache entry
$newTtl[$key] = \time() + $this->cacheTtl;
} else {
// The value we wrote is already old,
// keep it a write-cache entry to re-write it later
$newTtl[$key] = $this->ttl[$key];
}
}

View File

@ -33,6 +33,7 @@ use danog\AsyncOrm\DbArrayBuilder;
use danog\AsyncOrm\DbObject;
use danog\AsyncOrm\Driver\MemoryArray;
use danog\AsyncOrm\Internal\Containers\CacheContainer;
use danog\AsyncOrm\Internal\Containers\ObjectContainer;
use danog\AsyncOrm\Internal\Driver\CachedArray;
use danog\AsyncOrm\Internal\Driver\ObjectArray;
use danog\AsyncOrm\KeyType;
@ -510,50 +511,72 @@ final class OrmTest extends TestCase
(new TestObject)->save();
}
public function testCache(): void
#[DataProvider('provideKeyValues')]
public function testCache(int $tablePostfix, KeyType $keyType, string|int $key, ValueType $valueType, mixed $value): void
{
$field = new DbArrayBuilder("testCache", new RedisSettings(
if ($value instanceof TestObject) {
$value = new TestObject;
}
$field = new DbArrayBuilder("testCache_{$tablePostfix}", new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 1
), KeyType::INT, ValueType::INT);
$fieldNoCache = new DbArrayBuilder("testCache", new RedisSettings(
), $keyType, $valueType);
$fieldNoCache = new DbArrayBuilder("testCache_{$tablePostfix}", new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 0
), KeyType::INT, ValueType::INT);
), $keyType, $valueType);
$orm = $field->build();
$ormUnCached = $fieldNoCache->build();
$orm->set(0, 1);
$orm->set($key, $value);
if ($value === ValueType::OBJECT) {
$this->assertCount(1, $ormUnCached);
} else {
$this->assertCount(0, $ormUnCached);
delay(0.1);
$this->assertCount(0, $ormUnCached);
delay(0.9);
$this->assertCount(1, $ormUnCached);
}
delay(1.0);
if ($value instanceof TestObject) {
unset($value);
$c = (new ReflectionProperty(ObjectArray::class, 'cache'))->getValue($orm);
$c->flushCache();
$this->assertCount(0, (new ReflectionProperty(ObjectContainer::class, 'cache'))->getValue($c));
$f1 = async($orm->get(...), $key);
$f2 = async($orm->get(...), $key);
$value = $f1->await();
$this->assertSame($value, $f1->await());
$this->assertSame($value, $f2->await());
} else {
/** @var CacheContainer */
$c = (new ReflectionProperty(CachedArray::class, 'cache'))->getValue($orm);
$this->assertCount(0, (new ReflectionProperty(CacheContainer::class, 'cache'))->getValue($c));
$f1 = async($orm->get(...), 0);
$f2 = async($orm->get(...), 0);
$this->assertSame(1, $f1->await());
$this->assertSame(1, $f2->await());
$f1 = async($orm->get(...), $key);
$f2 = async($orm->get(...), $key);
$this->assertSame($value, $f1->await());
$this->assertSame($value, $f2->await());
}
$orm->clear();
}
public function testCacheStandalone(): void
{
$obj = new TestObject;
$obj->initDbProperties(new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 1
), 'testCacheMore_');
), "testCacheStandalone_");
$fieldNoCache2 = new DbArrayBuilder("testCacheMore_arr2", new RedisSettings(
$fieldNoCache2 = new DbArrayBuilder("testCacheStandalone_arr2", new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 0
), KeyType::INT, ValueType::INT);
$orm2Uncached = $fieldNoCache2->build();
$fieldNoCache4 = new DbArrayBuilder("testCacheMore_arr4", new RedisSettings(
$fieldNoCache4 = new DbArrayBuilder("testCacheStandalone_arr4", new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 0
), KeyType::INT, ValueType::INT);
@ -576,6 +599,28 @@ final class OrmTest extends TestCase
{
$key = 0;
foreach (self::provideSettings() as [, $settings]) {
foreach (self::provideKeyValues() as [, $keyType, $key, $valueType, $value]) {
if ($valueType === ValueType::OBJECT && (
$settings instanceof MemorySettings
|| $settings->serializer instanceof Json
)) {
continue;
}
yield [
$key++,
$settings,
$keyType,
$key,
$valueType,
$value
];
}
}
}
public static function provideKeyValues(): \Generator
{
$key = 0;
foreach ([
[ValueType::INT, 123],
[ValueType::STRING, '123'],
@ -592,15 +637,8 @@ final class OrmTest extends TestCase
[ValueType::SCALAR, ['test' => 123]],
[ValueType::SCALAR, 123.321],
] as [$valueType, $value]) {
if ($valueType === ValueType::OBJECT && (
$settings instanceof MemorySettings
|| $settings->serializer instanceof Json
)) {
continue;
}
yield [
$key++,
$settings,
KeyType::INT,
1234,
$valueType,
@ -608,7 +646,6 @@ final class OrmTest extends TestCase
];
yield [
$key++,
$settings,
KeyType::STRING,
'test',
$valueType,
@ -616,7 +653,6 @@ final class OrmTest extends TestCase
];
yield [
$key++,
$settings,
KeyType::STRING,
'4321',
$valueType,
@ -624,14 +660,13 @@ final class OrmTest extends TestCase
];
yield [
$key++,
$settings,
KeyType::STRING_OR_INT,
'test_2',
$valueType,
$value
];
}
}
}
public static function provideSettings(): \Generator

View File

@ -54,7 +54,7 @@ final class TestObject extends DbObject
#[OrmMappedArray(
KeyType::INT,
ValueType::INT,
ValueType::OBJECT,
cacheTtl: 0,
)]
public DbArray $arr3;