Skip to content

Commit 7b34df8

Browse files
authored
Merge pull request #49 from php-http/revert-47-no-pool
Revert "Removed the HTTPClientPool feature"
2 parents 52b1c26 + e7d14e4 commit 7b34df8

11 files changed

+829
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Added
66

7+
- Add HttpClientPool client to leverage load balancing and fallback mechanism [see the documentation](http://docs.php-http.org/en/latest/components/client-common.html) for more details.
78
- `PluginClientFactory` to create `PluginClient` instances.
89
- Added new option 'delay' for `RetryPlugin`.
910
- Added new option 'decider' for `RetryPlugin`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Common\HttpClientPool;
4+
5+
use Http\Client\Common\HttpClientPoolItem;
6+
use Http\Client\HttpAsyncClient;
7+
use Http\Client\HttpClient;
8+
use Http\Promise\Promise;
9+
use PhpSpec\Exception\Example\SkippingException;
10+
use PhpSpec\ObjectBehavior;
11+
use Prophecy\Argument;
12+
use Psr\Http\Message\RequestInterface;
13+
use Psr\Http\Message\ResponseInterface;
14+
15+
class LeastUsedClientPoolSpec extends ObjectBehavior
16+
{
17+
public function it_is_initializable()
18+
{
19+
$this->shouldHaveType('Http\Client\Common\HttpClientPool\LeastUsedClientPool');
20+
}
21+
22+
public function it_is_an_http_client()
23+
{
24+
$this->shouldImplement('Http\Client\HttpClient');
25+
}
26+
27+
public function it_is_an_async_http_client()
28+
{
29+
$this->shouldImplement('Http\Client\HttpAsyncClient');
30+
}
31+
32+
public function it_throw_exception_with_no_client(RequestInterface $request)
33+
{
34+
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
35+
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendAsyncRequest($request);
36+
}
37+
38+
public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response)
39+
{
40+
$this->addHttpClient($httpClient);
41+
$httpClient->sendRequest($request)->willReturn($response);
42+
43+
$this->sendRequest($request)->shouldReturn($response);
44+
}
45+
46+
public function it_sends_async_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise)
47+
{
48+
$this->addHttpClient($httpAsyncClient);
49+
$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);
50+
$promise->then(Argument::type('callable'), Argument::type('callable'))->willReturn($promise);
51+
52+
$this->sendAsyncRequest($request)->shouldReturn($promise);
53+
}
54+
55+
public function it_throw_exception_if_no_more_enable_client(HttpClient $client, RequestInterface $request)
56+
{
57+
$this->addHttpClient($client);
58+
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');
59+
60+
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
61+
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
62+
}
63+
64+
public function it_reenable_client(HttpClient $client, RequestInterface $request)
65+
{
66+
$this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0));
67+
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');
68+
69+
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
70+
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
71+
}
72+
73+
public function it_uses_the_lowest_request_client(HttpClientPoolItem $client1, HttpClientPoolItem $client2, RequestInterface $request, ResponseInterface $response)
74+
{
75+
if (extension_loaded('xdebug')) {
76+
throw new SkippingException('This test fail when xdebug is enable on PHP < 7');
77+
}
78+
79+
$this->addHttpClient($client1);
80+
$this->addHttpClient($client2);
81+
82+
$client1->getSendingRequestCount()->willReturn(10);
83+
$client2->getSendingRequestCount()->willReturn(2);
84+
85+
$client1->isDisabled()->willReturn(false);
86+
$client2->isDisabled()->willReturn(false);
87+
88+
$client1->sendRequest($request)->shouldNotBeCalled();
89+
$client2->sendRequest($request)->willReturn($response);
90+
91+
$this->sendRequest($request)->shouldReturn($response);
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Common\HttpClientPool;
4+
5+
use Http\Client\Common\HttpClientPoolItem;
6+
use Http\Client\HttpAsyncClient;
7+
use Http\Client\HttpClient;
8+
use Http\Promise\Promise;
9+
use PhpSpec\ObjectBehavior;
10+
use Prophecy\Argument;
11+
use Psr\Http\Message\RequestInterface;
12+
use Psr\Http\Message\ResponseInterface;
13+
14+
class RandomClientPoolSpec extends ObjectBehavior
15+
{
16+
public function it_is_initializable()
17+
{
18+
$this->shouldHaveType('Http\Client\Common\HttpClientPool\RandomClientPool');
19+
}
20+
21+
public function it_is_an_http_client()
22+
{
23+
$this->shouldImplement('Http\Client\HttpClient');
24+
}
25+
26+
public function it_is_an_async_http_client()
27+
{
28+
$this->shouldImplement('Http\Client\HttpAsyncClient');
29+
}
30+
31+
public function it_throw_exception_with_no_client(RequestInterface $request)
32+
{
33+
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
34+
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendAsyncRequest($request);
35+
}
36+
37+
public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response)
38+
{
39+
$this->addHttpClient($httpClient);
40+
$httpClient->sendRequest($request)->willReturn($response);
41+
42+
$this->sendRequest($request)->shouldReturn($response);
43+
}
44+
45+
public function it_sends_async_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise)
46+
{
47+
$this->addHttpClient($httpAsyncClient);
48+
$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);
49+
$promise->then(Argument::type('callable'), Argument::type('callable'))->willReturn($promise);
50+
51+
$this->sendAsyncRequest($request)->shouldReturn($promise);
52+
}
53+
54+
public function it_throw_exception_if_no_more_enable_client(HttpClient $client, RequestInterface $request)
55+
{
56+
$this->addHttpClient($client);
57+
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');
58+
59+
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
60+
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
61+
}
62+
63+
public function it_reenable_client(HttpClient $client, RequestInterface $request)
64+
{
65+
$this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0));
66+
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');
67+
68+
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
69+
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Common\HttpClientPool;
4+
5+
use Http\Client\Common\HttpClientPoolItem;
6+
use Http\Client\HttpAsyncClient;
7+
use Http\Client\HttpClient;
8+
use Http\Promise\Promise;
9+
use PhpSpec\ObjectBehavior;
10+
use Prophecy\Argument;
11+
use Psr\Http\Message\RequestInterface;
12+
use Psr\Http\Message\ResponseInterface;
13+
14+
class RoundRobinClientPoolSpec extends ObjectBehavior
15+
{
16+
public function it_is_initializable()
17+
{
18+
$this->shouldHaveType('Http\Client\Common\HttpClientPool\RoundRobinClientPool');
19+
}
20+
21+
public function it_is_an_http_client()
22+
{
23+
$this->shouldImplement('Http\Client\HttpClient');
24+
}
25+
26+
public function it_is_an_async_http_client()
27+
{
28+
$this->shouldImplement('Http\Client\HttpAsyncClient');
29+
}
30+
31+
public function it_throw_exception_with_no_client(RequestInterface $request)
32+
{
33+
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
34+
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendAsyncRequest($request);
35+
}
36+
37+
public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response)
38+
{
39+
$this->addHttpClient($httpClient);
40+
$httpClient->sendRequest($request)->willReturn($response);
41+
42+
$this->sendRequest($request)->shouldReturn($response);
43+
}
44+
45+
public function it_sends_async_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise)
46+
{
47+
$this->addHttpClient($httpAsyncClient);
48+
$httpAsyncClient->sendAsyncRequest($request)->willReturn($promise);
49+
$promise->then(Argument::type('callable'), Argument::type('callable'))->willReturn($promise);
50+
51+
$this->sendAsyncRequest($request)->shouldReturn($promise);
52+
}
53+
54+
public function it_throw_exception_if_no_more_enable_client(HttpClient $client, RequestInterface $request)
55+
{
56+
$this->addHttpClient($client);
57+
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');
58+
59+
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
60+
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
61+
}
62+
63+
public function it_reenable_client(HttpClient $client, RequestInterface $request)
64+
{
65+
$this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0));
66+
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');
67+
68+
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
69+
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
70+
}
71+
72+
public function it_round_between_clients(HttpClient $client1, HttpClient $client2, RequestInterface $request, ResponseInterface $response)
73+
{
74+
$this->addHttpClient($client1);
75+
$this->addHttpClient($client2);
76+
77+
$client1->sendRequest($request)->willReturn($response);
78+
$client2->sendRequest($request)->willReturn($response);
79+
80+
$this->sendRequest($request)->shouldReturn($response);
81+
$this->sendRequest($request)->shouldReturn($response);
82+
}
83+
}

0 commit comments

Comments
 (0)