-
Notifications
You must be signed in to change notification settings - Fork 54
/
RoundRobinClientPoolSpec.php
86 lines (69 loc) · 3.16 KB
/
RoundRobinClientPoolSpec.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace spec\Http\Client\Common\HttpClientPool;
use Http\Client\Common\HttpClientPool\HttpClientPoolItemImpl;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Promise\Promise;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Http\Client\Common\HttpClientPool\RoundRobinClientPool;
use Http\Client\Common\Exception\HttpClientNotFoundException;
use Http\Client\Exception\HttpException;
class RoundRobinClientPoolSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType(RoundRobinClientPool::class);
}
public function it_is_an_http_client()
{
$this->shouldImplement(HttpClient::class);
}
public function it_is_an_async_http_client()
{
$this->shouldImplement(HttpAsyncClient::class);
}
public function it_throw_exception_with_no_client(RequestInterface $request)
{
$this->shouldThrow(HttpClientNotFoundException::class)->duringSendRequest($request);
$this->shouldThrow(HttpClientNotFoundException::class)->duringSendAsyncRequest($request);
}
public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response)
{
$this->addHttpClient($httpClient);
$httpClient->sendRequest($request)->willReturn($response);
$this->sendRequest($request)->shouldReturn($response);
}
public function it_sends_async_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise)
{
$this->addHttpClient($httpAsyncClient);
$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);
$promise->then(Argument::type('callable'), Argument::type('callable'))->willReturn($promise);
$this->sendAsyncRequest($request)->shouldReturn($promise);
}
public function it_throw_exception_if_no_more_enable_client(HttpClient $client, RequestInterface $request)
{
$this->addHttpClient($client);
$client->sendRequest($request)->willThrow(HttpException::class);
$this->shouldThrow(HttpException::class)->duringSendRequest($request);
$this->shouldThrow(HttpClientNotFoundException::class)->duringSendRequest($request);
}
public function it_reenable_client(HttpClient $client, RequestInterface $request)
{
$this->addHttpClient(new HttpClientPoolItemImpl($client->getWrappedObject(), 0));
$client->sendRequest($request)->willThrow(HttpException::class);
$this->shouldThrow(HttpException::class)->duringSendRequest($request);
$this->shouldThrow(HttpException::class)->duringSendRequest($request);
}
public function it_round_between_clients(HttpClient $client1, HttpClient $client2, RequestInterface $request, ResponseInterface $response)
{
$this->addHttpClient($client1);
$this->addHttpClient($client2);
$client1->sendRequest($request)->willReturn($response);
$client2->sendRequest($request)->willReturn($response);
$this->sendRequest($request)->shouldReturn($response);
$this->sendRequest($request)->shouldReturn($response);
}
}