Passed
Push — master ( 072d04...55dded )
by Jorge
01:39
created

FixWaitTimeToRetryTest::it_report_as_available()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace JVelasco\CircuitBreaker\AvailabilityStrategy;
4
5
use JVelasco\CircuitBreaker\StorageException;
6
use PHPUnit\Framework\TestCase;
7
8
final class FixWaitTimeToRetryTest extends TestCase
9
{
10
    const A_SERVICE = "a service";
11
    const MAX_FAILURES = 1;
12
    const ONE_SECOND = 1;
13
14
    /** @test */
15
    public function it_report_as_available()
16
    {
17
        $storage = $this->prophesize(Storage::class);
18
        $strategy = new FixedWaitTimeToRetry(
19
            $storage->reveal(),
20
            self::MAX_FAILURES,
21
            self::ONE_SECOND
22
        );
23
24
        $storage->numberOfFailures(self::A_SERVICE)->willReturn(self::MAX_FAILURES-1);
25
        $this->assertTrue($strategy->isAvailable(self::A_SERVICE));
26
    }
27
28
    /** @test */
29
    public function it_reports_as_non_available()
30
    {
31
        $storage = $this->prophesize(Storage::class);
32
        $strategy = new FixedWaitTimeToRetry(
33
            $storage->reveal(),
34
            self::MAX_FAILURES,
35
            self::ONE_SECOND
36
        );
37
38
        $storage->numberOfFailures(self::A_SERVICE)->willReturn(self::MAX_FAILURES);
39
        $storage->getStrategyData($strategy, "last_try");
40
        $this->assertFalse($strategy->isAvailable(self::A_SERVICE));
41
    }
42
43
    /** @test */
44
    public function it_close_the_circuit_after_timeout()
45
    {
46
        $storage = $this->prophesize(Storage::class);
47
        $strategy = new FixedWaitTimeToRetry(
48
            $storage->reveal(),
49
            self::MAX_FAILURES,
50
            self::ONE_SECOND
51
        );
52
53
        $storage->numberOfFailures(self::A_SERVICE)->willReturn(self::MAX_FAILURES);
54
        $twoSecondsAgo = new \DateTime();
55
        $twoSecondsAgo->modify("-2 sec");
56
        $storage->getStrategyData($strategy, "last_try")
57
            ->willReturn((string) $twoSecondsAgo->getTimestamp());
58
59
        $this->assertTrue($strategy->isAvailable(self::A_SERVICE));
60
    }
61
62
    /** @test */
63
    public function it_reports_as_available_on_storage_failures()
64
    {
65
        $storage = $this->prophesize(Storage::class);
66
        $strategy = new FixedWaitTimeToRetry(
67
            $storage->reveal(),
68
            self::MAX_FAILURES,
69
            self::ONE_SECOND
70
        );
71
72
        $storage->numberOfFailures(self::A_SERVICE)->willThrow(StorageException::class);
73
        $this->assertTrue($strategy->isAvailable(self::A_SERVICE));
74
    }
75
}
76