1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the hogosha-monitor package |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* Feel free to edit as you please, and have fun. |
12
|
|
|
* |
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Hogosha\Monitor\Middleware; |
17
|
|
|
|
18
|
|
|
use GuzzleHttp\Client; |
19
|
|
|
use GuzzleHttp\Exception\ConnectException; |
20
|
|
|
use GuzzleHttp\HandlerStack; |
21
|
|
|
use GuzzleHttp\Handler\MockHandler; |
22
|
|
|
use GuzzleHttp\Middleware; |
23
|
|
|
use GuzzleHttp\Psr7\Request; |
24
|
|
|
use GuzzleHttp\Psr7\Response; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Guillaume Cavana <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class BackoffTest extends \PHPUnit_Framework_TestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* testRetriesConnectException. |
33
|
|
|
*/ |
34
|
|
|
public function testRetriesConnectException() |
35
|
|
|
{ |
36
|
|
|
$mock = new MockHandler( |
37
|
|
|
[ |
38
|
|
|
new ConnectException('Error 1', new Request('GET', 'test')), |
39
|
|
|
new Response(200, ['X-Foo' => 'Bar']), |
40
|
|
|
] |
41
|
|
|
); |
42
|
|
|
$handler = HandlerStack::create($mock); |
43
|
|
|
$handler->push(Middleware::retry(Backoff::decider(), Backoff::delay())); |
44
|
|
|
$client = new Client(['handler' => $handler]); |
45
|
|
|
|
46
|
|
|
$this->assertEquals(200, $client->request('GET', '/')->getStatusCode()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* testRetryLimit. |
51
|
|
|
*/ |
52
|
|
|
public function testRetryLimit() |
53
|
|
|
{ |
54
|
|
|
$mock = new MockHandler( |
55
|
|
|
[ |
56
|
|
|
new ConnectException('Error 1', new Request('GET', 'test')), |
57
|
|
|
new ConnectException('Error 2', new Request('GET', 'test')), |
58
|
|
|
new ConnectException('Error 3', new Request('GET', 'test')), |
59
|
|
|
new ConnectException('Error 4', new Request('GET', 'test')), |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
$handler = HandlerStack::create($mock); |
63
|
|
|
$handler->push(Middleware::retry(Backoff::decider(), Backoff::delay())); |
64
|
|
|
$client = new Client(['handler' => $handler]); |
65
|
|
|
$this->setExpectedException( |
66
|
|
|
'GuzzleHttp\Exception\ConnectException', |
67
|
|
|
'Error 4' |
68
|
|
|
); |
69
|
|
|
$client->request('GET', '/')->getStatusCode(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* testRetryDelay. |
74
|
|
|
*/ |
75
|
|
|
public function testRetryDelay() |
76
|
|
|
{ |
77
|
|
|
$mock = new MockHandler( |
78
|
|
|
[ |
79
|
|
|
new ConnectException('+1 second delay', new Request('GET', 'test')), |
80
|
|
|
new ConnectException('+2 second delay', new Request('GET', 'test')), |
81
|
|
|
new Response(200), |
82
|
|
|
] |
83
|
|
|
); |
84
|
|
|
$handler = HandlerStack::create($mock); |
85
|
|
|
$handler->push(Middleware::retry(Backoff::decider(), Backoff::delay())); |
86
|
|
|
$client = new Client(['handler' => $handler]); |
87
|
|
|
$startTime = time(); |
88
|
|
|
$client->request('GET', '/')->getStatusCode(); |
89
|
|
|
$endTime = time(); |
90
|
|
|
$this->assertGreaterThan($startTime + 2, $endTime); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* testRetries500Errors. |
95
|
|
|
*/ |
96
|
|
|
public function testRetries500Errors() |
97
|
|
|
{ |
98
|
|
|
$mock = new MockHandler( |
99
|
|
|
[ |
100
|
|
|
new Response(500), |
101
|
|
|
new Response(200), |
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
$handler = HandlerStack::create($mock); |
105
|
|
|
$handler->push(Middleware::retry(Backoff::decider(), Backoff::delay())); |
106
|
|
|
$client = new Client(['handler' => $handler]); |
107
|
|
|
$this->assertEquals(200, $client->request('GET', '/')->getStatusCode()); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|