|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Tests; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Algolia; |
| 6 | +use Algolia\AlgoliaSearch\Api\SearchClient; |
| 7 | +use Algolia\AlgoliaSearch\Cache\NullCacheDriver; |
| 8 | +use Algolia\AlgoliaSearch\Configuration\SearchConfig; |
| 9 | +use Algolia\AlgoliaSearch\Exceptions\TimeoutException; |
| 10 | +use Algolia\AlgoliaSearch\Http\HttpClientInterface; |
| 11 | +use Algolia\AlgoliaSearch\Http\Psr7\Response; |
| 12 | +use Algolia\AlgoliaSearch\RequestOptions\RequestOptionsFactory; |
| 13 | +use Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper; |
| 14 | +use Algolia\AlgoliaSearch\RetryStrategy\ClusterHosts; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Psr\Http\Message\RequestInterface; |
| 17 | +use Psr\SimpleCache\CacheInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Contract test for the host state cache: `resetHost()` only writes to the cache when it |
| 21 | + * actually changes a host, so a successful request on a healthy host does not hit the cache. |
| 22 | + * |
| 23 | + * @internal |
| 24 | + * |
| 25 | + * @coversNothing |
| 26 | + */ |
| 27 | +class HostStateCacheTest extends TestCase |
| 28 | +{ |
| 29 | + private const HOSTS = ['host-one.test', 'host-two.test']; |
| 30 | + |
| 31 | + private $cache; |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $this->cache = $this->countingCache(); |
| 36 | + Algolia::setCache($this->cache); |
| 37 | + } |
| 38 | + |
| 39 | + protected function tearDown(): void |
| 40 | + { |
| 41 | + Algolia::setCache(new NullCacheDriver()); |
| 42 | + } |
| 43 | + |
| 44 | + public function testResetOfAHealthyHostDoesNotWriteTheCache(): void |
| 45 | + { |
| 46 | + $clusterHosts = ClusterHosts::create(self::HOSTS)->setCacheKey('hosts'); |
| 47 | + |
| 48 | + $clusterHosts->resetHost('host-one.test'); |
| 49 | + |
| 50 | + $this->assertSame(0, $this->cache->sets); |
| 51 | + } |
| 52 | + |
| 53 | + public function testResetOfAChangedHostWritesTheCache(): void |
| 54 | + { |
| 55 | + $clusterHosts = ClusterHosts::create(self::HOSTS)->setCacheKey('hosts'); |
| 56 | + |
| 57 | + $clusterHosts->failed('host-one.test'); |
| 58 | + $this->assertSame(1, $this->cache->sets); |
| 59 | + |
| 60 | + $clusterHosts->resetHost('host-one.test'); |
| 61 | + $this->assertSame(2, $this->cache->sets); |
| 62 | + |
| 63 | + $clusterHosts->timedOut('host-two.test'); |
| 64 | + $this->assertSame(3, $this->cache->sets); |
| 65 | + |
| 66 | + // a timed out host is still up, but its retry count has to be cleared |
| 67 | + $clusterHosts->resetHost('host-two.test'); |
| 68 | + $this->assertSame(4, $this->cache->sets); |
| 69 | + } |
| 70 | + |
| 71 | + public function testSuccessfulRequestsOnAHealthyHostDoNotWriteTheCache(): void |
| 72 | + { |
| 73 | + $client = $this->makeClient($this->recorder(function () { |
| 74 | + return new Response(200, [], '{}'); |
| 75 | + })); |
| 76 | + |
| 77 | + $client->customGet('1/test'); |
| 78 | + $sets = $this->cache->sets; |
| 79 | + |
| 80 | + $client->customGet('1/test'); |
| 81 | + $client->customGet('1/test'); |
| 82 | + |
| 83 | + $this->assertSame($sets, $this->cache->sets); |
| 84 | + } |
| 85 | + |
| 86 | + public function testSuccessAfterATimeoutWritesTheCacheOnce(): void |
| 87 | + { |
| 88 | + $client = $this->makeClient($this->recorder(function ($call) { |
| 89 | + if (1 === $call) { |
| 90 | + throw new TimeoutException('timed out'); |
| 91 | + } |
| 92 | + |
| 93 | + return new Response(200, [], '{}'); |
| 94 | + })); |
| 95 | + |
| 96 | + // first call: timeout on host one (write), success on host two (no change, no write) |
| 97 | + $client->customGet('1/test'); |
| 98 | + $sets = $this->cache->sets; |
| 99 | + |
| 100 | + // host one is tried first again: its retry count is cleared (write), then nothing changes |
| 101 | + $client->customGet('1/test'); |
| 102 | + $client->customGet('1/test'); |
| 103 | + |
| 104 | + $this->assertSame($sets + 1, $this->cache->sets); |
| 105 | + } |
| 106 | + |
| 107 | + private function countingCache(): CacheInterface |
| 108 | + { |
| 109 | + return new class implements CacheInterface { |
| 110 | + public $sets = 0; |
| 111 | + |
| 112 | + private $values = []; |
| 113 | + |
| 114 | + public function get($key, $default = null): mixed |
| 115 | + { |
| 116 | + return $this->values[$key] ?? $default; |
| 117 | + } |
| 118 | + |
| 119 | + public function set($key, $value, $ttl = null): bool |
| 120 | + { |
| 121 | + ++$this->sets; |
| 122 | + $this->values[$key] = $value; |
| 123 | + |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + public function delete($key): bool |
| 128 | + { |
| 129 | + unset($this->values[$key]); |
| 130 | + |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + public function clear(): bool |
| 135 | + { |
| 136 | + $this->values = []; |
| 137 | + |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + public function getMultiple($keys, $default = null): iterable |
| 142 | + { |
| 143 | + $values = []; |
| 144 | + foreach ($keys as $key) { |
| 145 | + $values[$key] = $this->get($key, $default); |
| 146 | + } |
| 147 | + |
| 148 | + return $values; |
| 149 | + } |
| 150 | + |
| 151 | + public function setMultiple($values, $ttl = null): bool |
| 152 | + { |
| 153 | + foreach ($values as $key => $value) { |
| 154 | + $this->set($key, $value, $ttl); |
| 155 | + } |
| 156 | + |
| 157 | + return true; |
| 158 | + } |
| 159 | + |
| 160 | + public function deleteMultiple($keys): bool |
| 161 | + { |
| 162 | + foreach ($keys as $key) { |
| 163 | + $this->delete($key); |
| 164 | + } |
| 165 | + |
| 166 | + return true; |
| 167 | + } |
| 168 | + |
| 169 | + public function has($key): bool |
| 170 | + { |
| 171 | + return isset($this->values[$key]); |
| 172 | + } |
| 173 | + }; |
| 174 | + } |
| 175 | + |
| 176 | + private function recorder(callable $respond): HttpClientInterface |
| 177 | + { |
| 178 | + return new class($respond) implements HttpClientInterface { |
| 179 | + public $calls = 0; |
| 180 | + |
| 181 | + private $respond; |
| 182 | + |
| 183 | + public function __construct(callable $respond) |
| 184 | + { |
| 185 | + $this->respond = $respond; |
| 186 | + } |
| 187 | + |
| 188 | + public function sendRequest(RequestInterface $request, $timeout, $connectTimeout) |
| 189 | + { |
| 190 | + ++$this->calls; |
| 191 | + |
| 192 | + return ($this->respond)($this->calls); |
| 193 | + } |
| 194 | + }; |
| 195 | + } |
| 196 | + |
| 197 | + private function makeClient(HttpClientInterface $mockHttp): SearchClient |
| 198 | + { |
| 199 | + $config = SearchConfig::create('test-app-id', 'test-api-key'); |
| 200 | + |
| 201 | + return new SearchClient( |
| 202 | + new ApiWrapper( |
| 203 | + $mockHttp, |
| 204 | + $config, |
| 205 | + ClusterHosts::create(self::HOSTS)->setCacheKey('hosts'), |
| 206 | + new RequestOptionsFactory($config) |
| 207 | + ), |
| 208 | + $config |
| 209 | + ); |
| 210 | + } |
| 211 | +} |
0 commit comments