From b7bbdd9a88a9177acf10902e5cf8e134e1fbbf5f Mon Sep 17 00:00:00 2001 From: Anton Ukhanev Date: Fri, 13 Oct 2023 12:06:11 +0200 Subject: [PATCH 1/3] Add test to illustrate issue #17 --- tests/functional/CachePoolTest.php | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/functional/CachePoolTest.php b/tests/functional/CachePoolTest.php index 8110f84..50e86b8 100644 --- a/tests/functional/CachePoolTest.php +++ b/tests/functional/CachePoolTest.php @@ -359,4 +359,35 @@ public function testHas() $this->assertFalse($subject->has($notThereKey)); } } + + /** + * Imitates a scenario where `set_transient()` erroneously returns `false` if set value is same as existing. + */ + public function testWritingSameValueSucceeds() + { + { + $poolName = uniqid('pool'); + $defaultValue = uniqid('default'); + $wpdb = $this->createWpdb(); + $key = uniqid('key'); + $value = uniqid('value'); + $subject = $this->createInstance($wpdb, $poolName, $defaultValue); + } + + { + Functions\expect('get_transient') + ->andReturn($value); + Functions\expect('set_transient') + ->andReturn(); + } + + { + // If no exception - success + $subject->set($key, $value, rand(1, 99999)); + + // Problem setting a different value results in an exception + $this->expectException(CacheException::class); + $subject->set($key, uniqid('other-value'), rand(1, 99999)); + } + } } From 6787e82afc8a9eb82149bf3646379fdad4d39765 Mon Sep 17 00:00:00 2001 From: Anton Ukhanev Date: Fri, 13 Oct 2023 12:11:53 +0200 Subject: [PATCH 2/3] Add check for same value to prevent false-negative result Should fix #17. --- src/CachePool.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/CachePool.php b/src/CachePool.php index 3fdd365..a78fac0 100644 --- a/src/CachePool.php +++ b/src/CachePool.php @@ -350,6 +350,7 @@ protected function getTransientOriginal(string $key) * @param int $ttl The amount of seconds after which the transient will expire. * * @throws RangeException If key invalid. + * @throws UnexpectedValueException If could not write the value to WP Transients API. * @throws RuntimeException If problem setting. */ protected function setTransient(string $key, $value, int $ttl): void @@ -357,7 +358,12 @@ protected function setTransient(string $key, $value, int $ttl): void $this->validateTransientKey($key); if (!set_transient($key, $value, $ttl)) { - throw new RuntimeException(sprintf('set_transient() failed with key "%1$s" with TTL %2$ss', $key, $ttl)); + $actualValue = $this->getTransient($key); + if ($actualValue !== $value) { + throw new UnexpectedValueException( + sprintf('set_transient() failed with key "%1$s" with TTL %2$ss', $key, $ttl) + ); + } } } From 23ffda030426a570e826740ccb51141e4437fc5d Mon Sep 17 00:00:00 2001 From: Anton Ukhanev Date: Fri, 13 Oct 2023 12:13:57 +0200 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9755d6f..aae2d5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [[*next-version*]] - YYYY-MM-DD +### Fixed +- Setting same value as existing caused an exception due to false-negative result (#22). ## [0.2.0-alpha1] - 2023-08-30 ### Removed