Skip to content

Commit

Permalink
Merge 799bd57 into 4aabf32
Browse files Browse the repository at this point in the history
  • Loading branch information
hirak committed Jan 31, 2016
2 parents 4aabf32 + 799bd57 commit 8481f2d
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 43 deletions.
1 change: 0 additions & 1 deletion .coveralls.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
service_name: travis-ci
repo_token: UWp1EuHuGdBmMnyvryRJQW3rTXl7zBtpc

# for php-coveralls
coverage_clover: artifacts/clover.xml
Expand Down
9 changes: 7 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="always"
bootstrap="vendor/autoload.php">
colors="true"
bootstrap="tests/bootstrap.php">
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
Expand All @@ -20,7 +20,12 @@
<php>
<ini name="error_reporting" value="-1"/>
</php>

<testsuite name="unit tests">
<directory>tests/unit/</directory>
</testsuite>

<testsuite name="functional tests">
<directory>tests/functional/</directory>
</testsuite>
</phpunit>
1 change: 1 addition & 0 deletions src/CurlRemoteFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Composer\Config;
use Composer\IO;
use Composer\Downloader;
use Composer\Util;

/**
Expand Down
82 changes: 43 additions & 39 deletions src/ParallelDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function download(array $packages, array $pluginConfig)
$unused[] = curl_init();
}

/// @codeCoverageIgnoreStart
// @codeCoverageIgnoreStart
if (function_exists('curl_share_init')) {
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
Expand All @@ -61,7 +61,7 @@ public function download(array $packages, array $pluginConfig)
curl_multi_setopt($mh, CURLMOPT_PIPELINING, true);
}
}
/// @codeCoverageIgnoreEnd
// @codeCoverageIgnoreEnd

$cachedir = rtrim($this->config->get('cache-files-dir'), '\/');

Expand Down Expand Up @@ -117,46 +117,50 @@ public function download(array $packages, array $pluginConfig)
// wait for any event
do {
// start multi download
while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running));
$runningBefore = $running;

$eventCount = curl_multi_select($mh, 5);

if ($eventCount === -1) {
usleep(200 * 1000);
continue;
}

if ($eventCount === 0) {
continue;
}

while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running));

if ($running === $runningBefore) {
continue;
}

do {
$stat = curl_multi_exec($mh, $running);
} while ($stat === CURLM_CALL_MULTI_PERFORM);

switch (curl_multi_select($mh, 5)) {
case -1:
usleep(250);
// fall through
case 0:
continue 2;
default:
do {
$stat = curl_multi_exec($mh, $running);
} while ($stat === CURLM_CALL_MULTI_PERFORM);

do {
if ($raised = curl_multi_info_read($mh, $remains)) {
$ch = $raised['handle'];
$errno = curl_errno($ch);
$info = curl_getinfo($ch);
curl_setopt($ch, CURLOPT_FILE, STDOUT);
$index = (int)$ch;
$outputFile = $chFpMap[$index];
unset($chFpMap[$index]);
if (CURLE_OK === $errno && 200 === $info['http_code']) {
++$this->successCnt;
} else {
++$this->failureCnt;
$outputFile->setFailure();
}
unset($outputFile);
$this->io->write($this->makeDownloadingText($info['url']));
curl_multi_remove_handle($mh, $ch);
$unused[] = $ch;
if ($raised = curl_multi_info_read($mh, $remains)) {
$ch = $raised['handle'];
$errno = curl_errno($ch);
$info = curl_getinfo($ch);
curl_setopt($ch, CURLOPT_FILE, STDOUT);
$index = (int)$ch;
$outputFile = $chFpMap[$index];
unset($chFpMap[$index]);
if (CURLE_OK === $errno && 200 === $info['http_code']) {
++$this->successCnt;
} else {
++$this->failureCnt;
$outputFile->setFailure();
}
} while ($remains > 0);

if (count($packages) > 0) {
break 2;
unset($outputFile);
$this->io->write($this->makeDownloadingText($info['url']));
curl_multi_remove_handle($mh, $ch);
$unused[] = $ch;
}
} while ($remains > 0);

if (count($packages) > 0) {
break;
}
} while ($running);
} while (count($packages) > 0);
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Plugin implements
/** @var IO\IOInterface */
private $io;

/** @var Config */
/** @var Composer\Config */
private $config;

public function activate(Composer $composer, IO\IOInterface $io)
Expand Down
18 changes: 18 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';

// start test server
execInBackground('php ' . __DIR__ . '/workspace/sampleapi.php');

function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) === 'Windows') {
pclose(popen('start /B ' . $cmd, 'r'));
} else {
exec($cmd . ' >/dev/null &');
}
}

register_shutdown_function(function(){
$ch = curl_init('http://localhost:1337/?exit=1');
curl_exec($ch);
});
109 changes: 109 additions & 0 deletions tests/unit/CurlRemoteFilesystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
namespace Hirak\Prestissimo;

use Composer\Config;
use Composer\IO;

class CurlRemoteFilesystemTest extends \PHPUnit_Framework_TestCase
{
protected $rfs;

protected function setUp()
{
$io = new IO\NullIO;
$config = new Config;
$this->rfs = new CurlRemoteFilesystem($io, $config);

$this->rfs->setPluginConfig(array(
'maxConnections' => 6,
'minConnections' => 3,
'pipeline' => false,
'verbose' => false,
'insecure' => false,
'capath' => '',
'privatePackages' => array(),
));
}

public static function tearDownAfterClass()
{
$targetFile = 'tests/workspace/target/0.txt';
if (file_exists($targetFile)) {
unlink($targetFile);
rmdir(dirname($targetFile));
}
}

public function testConstruct()
{
self::assertInstanceOf('Hirak\Prestissimo\CurlRemoteFilesystem', $this->rfs);
}

public function testGetOptions()
{
self::assertInternalType('array', $this->rfs->getOptions());
}

public function testGetLastHeaders()
{
self::assertInternalType('array', $this->rfs->getLastHeaders());
}

public function testCopy()
{
$targetUrl = 'http://localhost:1337/?wait=0';
$targetFile = 'tests/workspace/target/0.txt';

$this->rfs->copy('localhost', $targetUrl, $targetFile);

self::assertFileExists($targetFile);
self::assertStringEqualsFile($targetFile, '0');

// clean
unlink($targetFile);
rmdir(dirname($targetFile));
}

public function testCopyFailure()
{
$targetUrl = 'http://localhost:1337/?status=404';
$targetFile = 'tests/workspace/target/0.txt';

$this->rfs->copy('localhost', $targetUrl, $targetFile);

self::assertFileNotExists($targetFile);
self::assertFileNotExists(dirname($targetFile));
}

/**
* @expectedException Composer\Downloader\TransportException
*/
public function testCopyPromptAndRetry1()
{
$targetUrl = 'http://localhost:1337/?status=401';
$targetFile = 'tests/workspace/target/0.txt';

$this->rfs->copy('localhost', $targetUrl, $targetFile);
}

/**
* @expectedException Composer\Downloader\TransportException
*/
public function testCopyPromptAndRetry2()
{
$targetUrl = 'http://localhost:1337/?status=403';
$targetFile = 'tests/workspace/target/0.txt';

$this->rfs->copy('localhost', $targetUrl, $targetFile);
}

public function testGetContents()
{
$targetUrl = 'http://localhost:1337/?wait=0';

$progress = false;
$response = $this->rfs->getContents('localhost', $targetUrl, $progress);

self::assertEquals('0', $response);
}
}
52 changes: 52 additions & 0 deletions tests/unit/ParallelDownloaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace Hirak\Prestissimo;

use Composer\Config;
use Composer\IO;
use Composer\Package;

class ParallelDownloaderTest extends \PHPUnit_Framework_TestCase
{
protected $downloader;

protected $pluginConfig = array(
'maxConnections' => 6,
'minConnections' => 3,
'pipeline' => false,
'verbose' => false,
'insecure' => false,
'capath' => '',
'privatePackages' => array(),
);

protected function setUp()
{
$io = new IO\NullIO;
$config = new Config;
$this->downloader = new ParallelDownloader($io, $config);
}

public function testConstruct()
{
self::assertInstanceOf('Hirak\Prestissimo\ParallelDownloader', $this->downloader);
}

public function testDownloadSimple()
{
$packages = array(
self::createPackage('vendor/package1', 'http://localhost:1337/', '1234'),
self::createPackage('vendor/package2', 'http://localhost:1337/', '2345'),
);

$this->markTestIncomplete();
}

private static function createPackage($name, $url, $ref)
{
$package = new Package\Package($name, '1.0.0', 'v1.0.0');
$package->setDistUrl($url);
$package->setDistReference($ref);

return $package;
}
}

0 comments on commit 8481f2d

Please sign in to comment.