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 testIncrementFailureWithKeyWithTtl() |
72
|
|
|
{ |
73
|
|
|
$redis = $this->createMock(\Redis::class); |
74
|
|
|
|
75
|
|
|
$adapter = new RedisAdapter($redis, 'test-failure'); |
76
|
|
|
|
77
|
|
|
$redis->expects($this->once()) |
78
|
|
|
->method('ttl') |
79
|
|
|
->willReturn(20); |
80
|
|
|
|
81
|
|
|
$redis->expects($this->never()) |
82
|
|
|
->method('del'); |
83
|
|
|
|
84
|
|
|
$redis->expects($this->once()) |
85
|
|
|
->method('get') |
86
|
|
|
->willReturn(true); |
87
|
|
|
|
88
|
|
|
$redis->expects($this->never()) |
89
|
|
|
->method('multi'); |
90
|
|
|
|
91
|
|
|
$redis->expects($this->never()) |
92
|
|
|
->method('expire'); |
93
|
|
|
|
94
|
|
|
$redis->expects($this->never()) |
95
|
|
|
->method('exec'); |
96
|
|
|
|
97
|
|
|
$redis->expects($this->once()) |
98
|
|
|
->method('incr'); |
99
|
|
|
|
100
|
|
|
$adapter->incrementFailure('test-service', 30); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testIncrementFailureWithKeyWithoutTtlIntegratedRedis() |
104
|
|
|
{ |
105
|
|
|
$redis = new \Redis(); |
106
|
|
|
$redis->connect(getenv('REDIS_HOST')); |
107
|
|
|
|
108
|
|
|
$dummy_key = 'circuit-breaker:test-failure:test-service:failures'; |
109
|
|
|
|
110
|
|
|
// set dummy key without expire |
111
|
|
|
$redis->set($dummy_key, 1); |
112
|
|
|
$ttl = $redis->ttl($dummy_key); |
113
|
|
|
|
114
|
|
|
$this->assertEquals(-1, $ttl, 'Dummy key without ttl'); |
115
|
|
|
|
116
|
|
|
$adapter = new RedisAdapter($redis, 'test-failure'); |
117
|
|
|
|
118
|
|
|
// here, we expect that the dummy key will be removed and a new with ttl will be set |
119
|
|
|
$adapter->incrementFailure('test-service', 30); |
120
|
|
|
|
121
|
|
|
$ttl = $redis->ttl($dummy_key); |
122
|
|
|
|
123
|
|
|
$this->assertNotEquals(-1, $ttl, 'Dummy key with ttl'); |
124
|
|
|
$this->assertGreaterThan(1, $ttl, 'Dummy key more than 1s ttl'); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|