File: EndpointCacheTest.php

package info (click to toggle)
php-async-aws-core 1.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 988 kB
  • sloc: php: 6,837; makefile: 32
file content (60 lines) | stat: -rw-r--r-- 2,010 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

declare(strict_types=1);

namespace AsyncAws\Core\Tests\Unit\EndpointDiscovery;

use AsyncAws\Core\EndpointDiscovery\EndpointCache;
use AsyncAws\Core\EndpointDiscovery\EndpointInterface;
use PHPUnit\Framework\TestCase;

class EndpointCacheTest extends TestCase
{
    public function testAddEndpoints()
    {
        $cache = new EndpointCache();

        $cache->addEndpoints('r1', [$this->getEndpoint('foo.com', -10)]);
        self::assertNull($cache->getActiveEndpoint('r1'));
        self::assertSame('https://foo.com', $cache->getExpiredEndpoint('r1'));

        $cache->addEndpoints('r1', [$this->getEndpoint('bar.com', -1), $this->getEndpoint('bar.com', -2)]);
        self::assertNull($cache->getActiveEndpoint('r1'));
        self::assertSame('https://bar.com', $cache->getExpiredEndpoint('r1'));

        $cache->addEndpoints('r2', [$this->getEndpoint('qux.com', 10)]);
        self::assertNull($cache->getActiveEndpoint('r1'));
        self::assertSame('https://qux.com', $cache->getActiveEndpoint('r2'));

        $cache->addEndpoints('r2', [$this->getEndpoint('foofoo.com', 20), $this->getEndpoint('barfoo.com', 15)]);
        self::assertSame('https://foofoo.com', $cache->getActiveEndpoint('r2'));

        $cache->removeEndpoint('https://foofoo.com');
        self::assertSame('https://barfoo.com', $cache->getActiveEndpoint('r2'));
    }

    private function getEndpoint(string $address, int $ttl): EndpointInterface
    {
        return new class($address, $ttl) implements EndpointInterface {
            private $address;

            private $ttl;

            public function __construct(string $address, int $ttl)
            {
                $this->address = $address;
                $this->ttl = $ttl;
            }

            public function getAddress(): string
            {
                return $this->address;
            }

            public function getCachePeriodInMinutes(): int
            {
                return $this->ttl;
            }
        };
    }
}