Skip to content
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: php

sudo: false
sudo: required

cache:
directories:
Expand Down
7 changes: 7 additions & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public function startServer($name)
{
$filename = __DIR__ . '/server/' . $name . '.php';
$pipes = [];

if (!Semaphore::acquire()) {
$this->fail('Could not connect to server');
}

$this->servers[$name] = proc_open('php '. $filename, [], $pipes);
sleep(1);
}
Expand All @@ -28,5 +33,7 @@ public function tearDown()
foreach (array_keys($this->servers) as $name) {
$this->stopServer($name);
}

Semaphore::release();
}
}
40 changes: 40 additions & 0 deletions tests/Semaphore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Http\Client\Socket\Tests;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class Semaphore
{
private static $openConnections = 0;

/**
* Busy waiting for my turn to go.
*
* @return bool
*/
public static function acquire()
{
$tries = 1;
while (self::$openConnections > 0) {
sleep($tries++);
if ($tries > 5) {
return false;
}
}
self::$openConnections++;

return true;
}

/**
* Signal that you are done.
*/
public static function release()
{
// Do no be too quick
usleep(500000);
self::$openConnections--;
}
}
13 changes: 6 additions & 7 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Http\Client\Socket\Tests;

use Http\Client\Socket\Exception\TimeoutException;
use Http\Client\Socket\Stream;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -108,18 +109,16 @@ public function testIsReadable()
$this->assertTrue($stream->isReadable());
}

/**
* @expectedException \Http\Client\Socket\Exception\TimeoutException
*/
public function testTimeout()
{
$socket = fsockopen("php.net", 80);
socket_set_timeout($socket, 0, 100);

$stream = new Stream($socket);

try {
$stream->getContents();
} catch (\Exception $e) {
$this->assertInstanceOf('Http\Socket\Exception\TimeoutException', $e);
}
$stream = new Stream($socket, 50);
$stream->getContents();
}

public function testMetadatas()
Expand Down
2 changes: 1 addition & 1 deletion tests/server/ssl/file.srl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2D
2F
3 changes: 3 additions & 0 deletions tests/server/tcp-bugous-server.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

require_once __DIR__."/../Semaphore.php";

$socketServer = stream_socket_server('127.0.0.1:19999');
$client = stream_socket_accept($socketServer);

fclose($client);
\Http\Client\Socket\Tests\Semaphore::release();
5 changes: 4 additions & 1 deletion tests/server/tcp-server.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

require_once __DIR__."/../Semaphore.php";

$socketServer = stream_socket_server('127.0.0.1:19999');
$client = stream_socket_accept($socketServer);
$client = stream_socket_accept($socketServer);

fwrite($client, str_replace("\n", "\r\n", <<<EOR
HTTP/1.1 200 OK
Expand All @@ -14,3 +16,4 @@
while (!@feof($client)) {
@fread($client, 1000);
}
\Http\Client\Socket\Tests\Semaphore::release();
3 changes: 3 additions & 0 deletions tests/server/tcp-ssl-server-client.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once __DIR__."/../Semaphore.php";

$context = stream_context_create([
'ssl' => [
'local_cert' => __DIR__ . '/ssl/server-and-key.pem',
Expand Down Expand Up @@ -44,3 +46,4 @@
while (!@feof($client)) {
@fread($client, 1000);
}
\Http\Client\Socket\Tests\Semaphore::release();
6 changes: 4 additions & 2 deletions tests/server/tcp-ssl-server.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once __DIR__."/../Semaphore.php";

$context = stream_context_create([
'ssl' => [
'local_cert' => __DIR__ . '/ssl/server-and-key.pem'
Expand Down Expand Up @@ -30,8 +32,8 @@
}




while (!@feof($client)) {
@fread($client, 1000);
}

\Http\Client\Socket\Tests\Semaphore::release();
3 changes: 3 additions & 0 deletions tests/server/unix-domain-server.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once __DIR__."/../Semaphore.php";

if (file_exists(__DIR__.'/server.sock')) {
unlink(__DIR__.'/server.sock');
}
Expand All @@ -20,3 +22,4 @@
}

unlink(__DIR__.'/server.sock');
\Http\Client\Socket\Tests\Semaphore::release();