Skip to content

Commit

Permalink
Hide testing bool from public API
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Feb 11, 2020
1 parent 050748c commit 82ade8b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
18 changes: 9 additions & 9 deletions lib/Context/Internal/ProcessHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class ProcessHub
private $toUnlink;

/**
* Constructor.
*
* @param boolean $useFIFO Whether to use FIFOs instead of the more reliable UNIX socket server (CHOSEN AUTOMATICALLY, only for testing purposes)
*/
public function __construct(bool $useFIFO = false)
Expand All @@ -49,8 +47,8 @@ public function __construct(bool $useFIFO = false)
$this->uri = "tcp://127.0.0.1:0";
} else {
$suffix = \bin2hex(\random_bytes(10));
$path = \sys_get_temp_dir()."/amp-parallel-ipc-".$suffix.".sock";
$this->uri = "unix://".$path;
$path = \sys_get_temp_dir() . "/amp-parallel-ipc-" . $suffix . ".sock";
$this->uri = "unix://" . $path;
$this->toUnlink = $path;
}

Expand All @@ -77,29 +75,32 @@ public function __construct(bool $useFIFO = false)
if ($isWindows) {
$name = \stream_socket_get_name($this->server, false);
$port = \substr($name, \strrpos($name, ":") + 1);
$this->uri = "tcp://127.0.0.1:".$port;
$this->uri = "tcp://127.0.0.1:" . $port;
}

$keys = &$this->keys;
$acceptor = &$this->acceptor;
$this->watcher = Loop::onReadable($this->server, static function (string $watcher, $server) use (&$keys, &$acceptor, &$fifo): \Generator {
$this->watcher = Loop::onReadable($this->server, static function (string $watcher, $server) use (&$keys, &$acceptor, $fifo): \Generator {
if ($fifo) {
$length = \unpack('v', \fread($server, 2))[1];
if (!$length) {
return; // Could not accept, wrong length read
}

$prefix = \fread($server, $length);
$sockets = [
$prefix."1",
$prefix."2",
$prefix . '1',
$prefix . '2',
];

foreach ($sockets as $k => &$socket) {
if (@\filetype($socket) !== 'fifo') {
if ($k) {
\fclose($sockets[0]);
}
return; // Is not a FIFO
}

// Open in either read or write mode to send a close signal when done
if (!$socket = \fopen($socket, $k ? 'w' : 'r')) {
if ($k) {
Expand All @@ -117,7 +118,6 @@ public function __construct(bool $useFIFO = false)
$channel = new ChannelledSocket($client, $client);
}


try {
$received = yield Promise\timeout($channel->receive(), self::KEY_RECEIVE_TIMEOUT);
} catch (\Throwable $exception) {
Expand Down
5 changes: 2 additions & 3 deletions lib/Context/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@ public static function run($script, string $cwd = null, array $env = [], string
* @param string|null $cwd Working directory.
* @param mixed[] $env Array of environment variables.
* @param string $binary Path to PHP binary. Null will attempt to automatically locate the binary.
* @param boolean $useFIFO Whether to use FIFOs instead of the more reliable UNIX socket server (CHOSEN AUTOMATICALLY, only for testing purposes)
*
* @throws \Error If the PHP binary path given cannot be found or is not executable.
*/
public function __construct($script, string $cwd = null, array $env = [], string $binary = null, bool $useFIFO = false)
public function __construct($script, string $cwd = null, array $env = [], string $binary = null)
{
$this->hub = Loop::getState(self::class);
if (!$this->hub instanceof Internal\ProcessHub) {
$this->hub = new Internal\ProcessHub($useFIFO);
$this->hub = new Internal\ProcessHub;
Loop::setState(self::class, $this->hub);
}

Expand Down
12 changes: 8 additions & 4 deletions test/Context/ProcessFifoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

namespace Amp\Parallel\Test\Context;

use Amp\Loop;
use Amp\Parallel\Context\Context;
use Amp\Parallel\Context\Internal\ProcessHub;
use Amp\Parallel\Context\Process;

/**
* @requires OS Linux
*/
class ProcessFifoTest extends AbstractContextTest
{
public function createContext($script): Context
{
return new Process($script, null, [], null, true);
if (\strncasecmp(\PHP_OS, "WIN", 3) === 0) {
$this->markTestSkipped('FIFO pipes do not work on Windows');
}

Loop::setState(Process::class, new ProcessHub(true)); // Manually set ProcessHub using FIFO pipes.
return new Process($script);
}
}
3 changes: 3 additions & 0 deletions test/Context/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace Amp\Parallel\Test\Context;

use Amp\Loop;
use Amp\Parallel\Context\Context;
use Amp\Parallel\Context\Internal\ProcessHub;
use Amp\Parallel\Context\Process;

class ProcessTest extends AbstractContextTest
{
public function createContext($script): Context
{
Loop::setState(Process::class, new ProcessHub(false)); // Manually set ProcessHub using socket server.
return new Process($script);
}
}

0 comments on commit 82ade8b

Please sign in to comment.