Skip to content

Commit

Permalink
Debug
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed May 17, 2018
1 parent 0e88d14 commit e4c073b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
38 changes: 28 additions & 10 deletions includes/lib/fasterimage/FasterImage.php
Expand Up @@ -40,22 +40,34 @@ public function batch(array $urls)

$multi = curl_multi_init();
$results = array();
$conn = array();

// Create the curl handles and add them to the multi_request
foreach ( array_values($urls) as $count => $uri ) {
foreach ( array_values( $urls ) as $count => $uri ) {

$results[$uri] = [];

$$count = $this->handle($uri, $results[$uri]);
$conn[ $count ] = $this->handle($uri, $results[$uri]);

$code = curl_multi_add_handle($multi, $$count);
$code = curl_multi_add_handle($multi, $conn[ $count ] );

if ( $code != CURLM_OK ) {
throw new \Exception("Curl handle for $uri could not be added");
}
}

// Perform the requests
$multi_info = array();
do {
$status = curl_multi_exec( $multi, $active );

if ( ( $info = curl_multi_info_read( $multi ) ) !== false ) {
$multi_info[ array_search( $info['handle'], $conn ) ] = $info;
}

} while ( $status === CURLM_CALL_MULTI_PERFORM || $active );

/*
do {
while ( ($mrc = curl_multi_exec($multi, $active)) == CURLM_CALL_MULTI_PERFORM ) ;
if ( $mrc != CURLM_OK && $mrc != CURLM_CALL_MULTI_PERFORM ) {
Expand All @@ -68,14 +80,20 @@ public function batch(array $urls)
usleep(250);
}
} while ( $active );
*/

// Figure out why individual requests may have failed
foreach ( array_values($urls) as $count => $uri ) {
$error = curl_error($$count);

if ( $error ) {
$results[$uri]['failure_reason'] = sprintf( '%s (error code: %s)', $error, curl_errno( $$count ) );
}
foreach ( array_values( $urls ) as $count => $url ) {
if ( isset( $multi_info[ $count ] ) ) {
$info = $multi_info[ $count ];
if ( ! empty( $info['result'] ) ) {
$results[ $url ]['failure_reason'] = sprintf( 'Error code: %d.', $info['result'] );
if ( function_exists( 'curl_strerror' ) ) {
$results[ $url ]['failure_reason'] .= ' ' . curl_strerror( $info['result'] );
}
}
}
curl_close( $conn[ $count ] );
}

echo __METHOD__ . ':' . __LINE__ . PHP_EOL;
Expand Down Expand Up @@ -113,7 +131,7 @@ protected function handle($url, & $result)
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 2*256);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Expand Down
9 changes: 2 additions & 7 deletions tests/test-amp-image-dimension-extractor.php
Expand Up @@ -10,8 +10,8 @@
// Not ideal to use remote URLs (since the remote service can change); mocking would be better.
define( 'IMG_350', 'http://i0.wp.com/amptest.files.wordpress.com/2017/03/350x150.png' );
define( 'IMG_1024', 'http://i0.wp.com/amptest.files.wordpress.com/2017/03/1024x768.png' );
define( 'IMG_SVG', 'https://gist.githubusercontent.com/westonruter/90fbaaced3851bf6ef762996c8c4375d/raw/316f589ce7f0c809a8ff101745f7ba5ea94505fb/amp.svg' );
define( 'IMG_SVG_VIEWPORT', 'https://gist.githubusercontent.com/westonruter/90fbaaced3851bf6ef762996c8c4375d/raw/316f589ce7f0c809a8ff101745f7ba5ea94505fb/google.svg' );
define( 'IMG_SVG', 'https://gist.githubusercontent.com/westonruter/90fbaaced3851bf6ef762996c8c4375d/raw/6ac97b70d5064d63c5663ff04e69d5a011b13bd9/amp.svg' );
define( 'IMG_SVG_VIEWPORT', 'https://gist.githubusercontent.com/westonruter/90fbaaced3851bf6ef762996c8c4375d/raw/6ac97b70d5064d63c5663ff04e69d5a011b13bd9/google.svg' );

/**
* Tests for AMP_Image_Dimension_Extractor.
Expand Down Expand Up @@ -184,11 +184,6 @@ public function test__multiple_valid_image_files() {
),
);

// Temp start.
$sources = wp_array_slice_assoc( $sources, array( IMG_SVG ) );
$expected = wp_array_slice_assoc( $expected, array( IMG_SVG ) );

// Temp end.
$dimensions = AMP_Image_Dimension_Extractor::extract_by_downloading_images( $sources );

echo "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n";
Expand Down

0 comments on commit e4c073b

Please sign in to comment.