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) { foreach ($updatedValues as $key => $value) {
if (($newValues[$key] = $this->cache[$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; $newTtl[$key] = \time() + $this->cacheTtl;
} else { } else {
// The value we wrote is already old,
// keep it a write-cache entry to re-write it later
$newTtl[$key] = $this->ttl[$key]; $newTtl[$key] = $this->ttl[$key];
} }
} }

View File

@ -33,6 +33,7 @@ use danog\AsyncOrm\DbArrayBuilder;
use danog\AsyncOrm\DbObject; use danog\AsyncOrm\DbObject;
use danog\AsyncOrm\Driver\MemoryArray; use danog\AsyncOrm\Driver\MemoryArray;
use danog\AsyncOrm\Internal\Containers\CacheContainer; use danog\AsyncOrm\Internal\Containers\CacheContainer;
use danog\AsyncOrm\Internal\Containers\ObjectContainer;
use danog\AsyncOrm\Internal\Driver\CachedArray; use danog\AsyncOrm\Internal\Driver\CachedArray;
use danog\AsyncOrm\Internal\Driver\ObjectArray; use danog\AsyncOrm\Internal\Driver\ObjectArray;
use danog\AsyncOrm\KeyType; use danog\AsyncOrm\KeyType;
@ -510,50 +511,72 @@ final class OrmTest extends TestCase
(new TestObject)->save(); (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"), RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 1 cacheTtl: 1
), KeyType::INT, ValueType::INT); ), $keyType, $valueType);
$fieldNoCache = new DbArrayBuilder("testCache", new RedisSettings( $fieldNoCache = new DbArrayBuilder("testCache_{$tablePostfix}", new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"), RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 0 cacheTtl: 0
), KeyType::INT, ValueType::INT); ), $keyType, $valueType);
$orm = $field->build(); $orm = $field->build();
$ormUnCached = $fieldNoCache->build(); $ormUnCached = $fieldNoCache->build();
$orm->set(0, 1); $orm->set($key, $value);
$this->assertCount(0, $ormUnCached); if ($value === ValueType::OBJECT) {
delay(0.1); $this->assertCount(1, $ormUnCached);
$this->assertCount(0, $ormUnCached); } else {
delay(0.9); $this->assertCount(0, $ormUnCached);
$this->assertCount(1, $ormUnCached); delay(0.1);
$this->assertCount(0, $ormUnCached);
delay(0.9);
}
delay(1.0); delay(1.0);
/** @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); if ($value instanceof TestObject) {
$f2 = async($orm->get(...), 0); unset($value);
$this->assertSame(1, $f1->await()); $c = (new ReflectionProperty(ObjectArray::class, 'cache'))->getValue($orm);
$this->assertSame(1, $f2->await()); $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(...), $key);
$f2 = async($orm->get(...), $key);
$this->assertSame($value, $f1->await());
$this->assertSame($value, $f2->await());
}
$orm->clear(); $orm->clear();
}
public function testCacheStandalone(): void
{
$obj = new TestObject; $obj = new TestObject;
$obj->initDbProperties(new RedisSettings( $obj->initDbProperties(new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"), RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 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"), RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 0 cacheTtl: 0
), KeyType::INT, ValueType::INT); ), KeyType::INT, ValueType::INT);
$orm2Uncached = $fieldNoCache2->build(); $orm2Uncached = $fieldNoCache2->build();
$fieldNoCache4 = new DbArrayBuilder("testCacheMore_arr4", new RedisSettings( $fieldNoCache4 = new DbArrayBuilder("testCacheStandalone_arr4", new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"), RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 0 cacheTtl: 0
), KeyType::INT, ValueType::INT); ), KeyType::INT, ValueType::INT);
@ -576,22 +599,7 @@ final class OrmTest extends TestCase
{ {
$key = 0; $key = 0;
foreach (self::provideSettings() as [, $settings]) { foreach (self::provideSettings() as [, $settings]) {
foreach ([ foreach (self::provideKeyValues() as [, $keyType, $key, $valueType, $value]) {
[ValueType::INT, 123],
[ValueType::STRING, '123'],
[ValueType::STRING, 'test'],
[ValueType::FLOAT, 123.321],
[ValueType::BOOL, true],
[ValueType::BOOL, false],
// Uncomment when segfaults are fixed
[ValueType::OBJECT, new TestObject],
[ValueType::SCALAR, 'test'],
[ValueType::SCALAR, 123],
[ValueType::SCALAR, ['test' => 123]],
[ValueType::SCALAR, 123.321],
] as [$valueType, $value]) {
if ($valueType === ValueType::OBJECT && ( if ($valueType === ValueType::OBJECT && (
$settings instanceof MemorySettings $settings instanceof MemorySettings
|| $settings->serializer instanceof Json || $settings->serializer instanceof Json
@ -601,32 +609,8 @@ final class OrmTest extends TestCase
yield [ yield [
$key++, $key++,
$settings, $settings,
KeyType::INT, $keyType,
1234, $key,
$valueType,
$value
];
yield [
$key++,
$settings,
KeyType::STRING,
'test',
$valueType,
$value
];
yield [
$key++,
$settings,
KeyType::STRING,
'4321',
$valueType,
$value
];
yield [
$key++,
$settings,
KeyType::STRING_OR_INT,
'test_2',
$valueType, $valueType,
$value $value
]; ];
@ -634,6 +618,57 @@ final class OrmTest extends TestCase
} }
} }
public static function provideKeyValues(): \Generator
{
$key = 0;
foreach ([
[ValueType::INT, 123],
[ValueType::STRING, '123'],
[ValueType::STRING, 'test'],
[ValueType::FLOAT, 123.321],
[ValueType::BOOL, true],
[ValueType::BOOL, false],
// Uncomment when segfaults are fixed
[ValueType::OBJECT, new TestObject],
[ValueType::SCALAR, 'test'],
[ValueType::SCALAR, 123],
[ValueType::SCALAR, ['test' => 123]],
[ValueType::SCALAR, 123.321],
] as [$valueType, $value]) {
yield [
$key++,
KeyType::INT,
1234,
$valueType,
$value
];
yield [
$key++,
KeyType::STRING,
'test',
$valueType,
$value
];
yield [
$key++,
KeyType::STRING,
'4321',
$valueType,
$value
];
yield [
$key++,
KeyType::STRING_OR_INT,
'test_2',
$valueType,
$value
];
}
}
public static function provideSettings(): \Generator public static function provideSettings(): \Generator
{ {
$key = 0; $key = 0;

View File

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