Skip to content

Revert "Removed the HTTPClientPool feature" #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added

- 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.
- `PluginClientFactory` to create `PluginClient` instances.
- Added new option 'delay' for `RetryPlugin`.
- Added new option 'decider' for `RetryPlugin`.
Expand Down
93 changes: 93 additions & 0 deletions spec/HttpClientPool/LeastUsedClientPoolSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace spec\Http\Client\Common\HttpClientPool;

use Http\Client\Common\HttpClientPoolItem;
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Promise\Promise;
use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class LeastUsedClientPoolSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Common\HttpClientPool\LeastUsedClientPool');
}

public function it_is_an_http_client()
{
$this->shouldImplement('Http\Client\HttpClient');
}

public function it_is_an_async_http_client()
{
$this->shouldImplement('Http\Client\HttpAsyncClient');
}

public function it_throw_exception_with_no_client(RequestInterface $request)
{
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->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('Http\Client\Exception\HttpException');

$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
}

public function it_reenable_client(HttpClient $client, RequestInterface $request)
{
$this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0));
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');

$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
}

public function it_uses_the_lowest_request_client(HttpClientPoolItem $client1, HttpClientPoolItem $client2, RequestInterface $request, ResponseInterface $response)
{
if (extension_loaded('xdebug')) {
throw new SkippingException('This test fail when xdebug is enable on PHP < 7');
}

$this->addHttpClient($client1);
$this->addHttpClient($client2);

$client1->getSendingRequestCount()->willReturn(10);
$client2->getSendingRequestCount()->willReturn(2);

$client1->isDisabled()->willReturn(false);
$client2->isDisabled()->willReturn(false);

$client1->sendRequest($request)->shouldNotBeCalled();
$client2->sendRequest($request)->willReturn($response);

$this->sendRequest($request)->shouldReturn($response);
}
}
71 changes: 71 additions & 0 deletions spec/HttpClientPool/RandomClientPoolSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace spec\Http\Client\Common\HttpClientPool;

use Http\Client\Common\HttpClientPoolItem;
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;

class RandomClientPoolSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Common\HttpClientPool\RandomClientPool');
}

public function it_is_an_http_client()
{
$this->shouldImplement('Http\Client\HttpClient');
}

public function it_is_an_async_http_client()
{
$this->shouldImplement('Http\Client\HttpAsyncClient');
}

public function it_throw_exception_with_no_client(RequestInterface $request)
{
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->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('Http\Client\Exception\HttpException');

$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
}

public function it_reenable_client(HttpClient $client, RequestInterface $request)
{
$this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0));
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');

$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
}
}
83 changes: 83 additions & 0 deletions spec/HttpClientPool/RoundRobinClientPoolSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace spec\Http\Client\Common\HttpClientPool;

use Http\Client\Common\HttpClientPoolItem;
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;

class RoundRobinClientPoolSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Common\HttpClientPool\RoundRobinClientPool');
}

public function it_is_an_http_client()
{
$this->shouldImplement('Http\Client\HttpClient');
}

public function it_is_an_async_http_client()
{
$this->shouldImplement('Http\Client\HttpAsyncClient');
}

public function it_throw_exception_with_no_client(RequestInterface $request)
{
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->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('Http\Client\Exception\HttpException');

$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
$this->shouldThrow('Http\Client\Common\Exception\HttpClientNotFoundException')->duringSendRequest($request);
}

public function it_reenable_client(HttpClient $client, RequestInterface $request)
{
$this->addHttpClient(new HttpClientPoolItem($client->getWrappedObject(), 0));
$client->sendRequest($request)->willThrow('Http\Client\Exception\HttpException');

$this->shouldThrow('Http\Client\Exception\HttpException')->duringSendRequest($request);
$this->shouldThrow('Http\Client\Exception\HttpException')->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);
}
}
Loading