1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
use LeoCarmo\CircuitBreaker\Adapters\RedisAdapter; |
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
|
6
|
|
|
class RedisAdapterTest extends TestCase |
7
|
|
|
{ |
8
|
|
|
public function testIncrementFailure() |
9
|
|
|
{ |
10
|
|
|
$redis = $this->createMock(\Redis::class); |
11
|
|
|
|
12
|
|
|
$adapter = new RedisAdapter($redis, 'test-failure'); |
13
|
|
|
|
14
|
|
|
$redis->expects($this->once()) |
15
|
|
|
->method('ttl'); |
16
|
|
|
|
17
|
|
|
$redis->expects($this->never()) |
18
|
|
|
->method('del'); |
19
|
|
|
|
20
|
|
|
$redis->expects($this->once()) |
21
|
|
|
->method('get') |
22
|
|
|
->willReturn(false); |
23
|
|
|
|
24
|
|
|
$redis->expects($this->once()) |
25
|
|
|
->method('multi'); |
26
|
|
|
|
27
|
|
|
$redis->expects($this->once()) |
28
|
|
|
->method('incr'); |
29
|
|
|
|
30
|
|
|
$redis->expects($this->once()) |
31
|
|
|
->method('expire'); |
32
|
|
|
|
33
|
|
|
$redis->expects($this->once()) |
34
|
|
|
->method('exec'); |
35
|
|
|
|
36
|
|
|
$adapter->incrementFailure('test-service', 30); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testIncrementFailureWithKeyWithoutTtl() |
40
|
|
|
{ |
41
|
|
|
$redis = $this->createMock(\Redis::class); |
42
|
|
|
|
43
|
|
|
$adapter = new RedisAdapter($redis, 'test-failure'); |
44
|
|
|
|
45
|
|
|
$redis->expects($this->once()) |
46
|
|
|
->method('ttl') |
47
|
|
|
->willReturn(-1); |
48
|
|
|
|
49
|
|
|
$redis->expects($this->once()) |
50
|
|
|
->method('del'); |
51
|
|
|
|
52
|
|
|
$redis->expects($this->once()) |
53
|
|
|
->method('get') |
54
|
|
|
->willReturn(false); |
55
|
|
|
|
56
|
|
|
$redis->expects($this->once()) |
57
|
|
|
->method('multi'); |
58
|
|
|
|
59
|
|
|
$redis->expects($this->once()) |
60
|
|
|
->method('incr'); |
61
|
|
|
|
62
|
|
|
$redis->expects($this->once()) |
63
|
|
|
->method('expire'); |
64
|
|
|
|
65
|
|
|
$redis->expects($this->once()) |
66
|
|
|
->method('exec'); |
67
|
|
|
|
68
|
|
|
$adapter->incrementFailure('test-service', 30); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testIncrementFailureWithKeyWithoutTtlIntegratedRedis() |
72
|
|
|
{ |
73
|
|
|
$redis = new \Redis(); |
74
|
|
|
$redis->connect(getenv('REDIS_HOST')); |
75
|
|
|
|
76
|
|
|
$dummy_key = 'circuit-breaker:test-failure:test-service:failures'; |
77
|
|
|
|
78
|
|
|
// set dummy key without expire |
79
|
|
|
$redis->set($dummy_key, 1); |
80
|
|
|
$ttl = $redis->ttl($dummy_key); |
81
|
|
|
|
82
|
|
|
$this->assertEquals(-1, $ttl, 'Dummy key without ttl'); |
83
|
|
|
|
84
|
|
|
$adapter = new RedisAdapter($redis, 'test-failure'); |
85
|
|
|
|
86
|
|
|
// here, we expect that the dummy key will be removed and a new with ttl will be set |
87
|
|
|
$adapter->incrementFailure('test-service', 30); |
88
|
|
|
|
89
|
|
|
$ttl = $redis->ttl($dummy_key); |
90
|
|
|
|
91
|
|
|
$this->assertNotEquals(-1, $ttl, 'Dummy key with ttl'); |
92
|
|
|
$this->assertGreaterThan(1, $ttl, 'Dummy key more than 1s ttl'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|