php-http / curl-client

cURL client
http://httplug.io
MIT License
445 stars 28 forks source link

Combining CurlClient with StopwatchPlugin causes Promise onRejected handler to never be invoked #26

Closed StephenClouse closed 8 years ago

StephenClouse commented 8 years ago

Not sure which project to file this under, but curl-client seems the most appropriate as this behavior does not occur with guzzle6-adapter.

Basically, if the Stopwatch plugin is attached to the client (as it would be in a default configuration running under Symfony with httplug-bundle), the CurlClient ignores the reject handler on any request.

Test script:

use Http\Client\Curl\Client;
use Http\Client\Common\PluginClient;
use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Client\Common\Plugin\StopwatchPlugin;
use GuzzleHttp\Psr7\Request;
use Symfony\Component\Stopwatch\Stopwatch;

$clients = [
    'normal_client' => new PluginClient(new Client(), [
        new ErrorPlugin()
    ]),
    'stopwatch_client' => new PluginClient(new Client(), [
        new ErrorPlugin(),
        new StopwatchPlugin(new Stopwatch()),
    ]),
];

$urls = [
    'http://www.google.com/',
    'http://localhost:8000/asdfasdfasddf',
    'http://www.nosuch.domain/foo'
];
$promises = [];

foreach ($clients as $clientName => $client) {
    foreach ($urls as $url) {
        $promises[] = $client->sendAsyncRequest(new Request('GET', $url))
               ->then(
                    function ($response) use ($clientName, $url) {
                        echo "[$clientName] $url SUCCESS " . $response->getStatusCode() . ' ' . $response->getReasonPhrase() . "\n";
                    }, function ($e) use ($clientName, $url) {
                        echo "[$clientName] $url ERROR " . $e->getMessage() . "\n";
                    }
                );
    }
}

foreach ($promises as $promise) {
    try {
        $promise->wait();
    } catch (\Exception $e) {
    }
}

Expected output:

[normal_client] http://www.nosuch.domain/foo ERROR Could not resolve host: www.nosuch.domain
[normal_client] http://localhost:8000/asdfasdfasddf ERROR Not Found
[normal_client] http://www.google.com/ SUCCESS 200 OK
[stopwatch_client] http://www.nosuch.domain/foo ERROR Could not resolve host: www.nosuch.domain
[stopwatch_client] http://localhost:8000/asdfasdfasddf ERROR Not Found
[stopwatch_client] http://www.google.com/ SUCCESS 200 OK

Actual output:

[normal_client] http://www.nosuch.domain/foo ERROR Could not resolve host: www.nosuch.domain
[normal_client] http://localhost:8000/asdfasdfasddf ERROR Not Found
[normal_client] http://www.google.com/ SUCCESS 200 OK
[stopwatch_client] http://www.google.com/ SUCCESS 200 OK

If you swap out Curl\Client for Guzzle6\Client, the expected output (with slightly different error messages) is seen.

mekras commented 8 years ago

Yea, It seems this is a CurlClient problem.

mekras commented 8 years ago

Fixed in 1.5.1.