Skip to content

Commit

Permalink
update/test file_sendfile for usage without libuv, update headers…
Browse files Browse the repository at this point in the history
… for remote `uri` calls
  • Loading branch information
TheTechsTech committed Mar 14, 2020
1 parent d73bac4 commit f632a29
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
8 changes: 7 additions & 1 deletion Coroutine/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Async\Coroutine\FileSystem;
use Async\Processor\Channel as Channeled;
use Async\Processor\ChannelInterface;
use SebastianBergmann\CodeCoverage\Node\File;

if (!\function_exists('coroutine_run')) {
\define('MILLISECOND', 0.001);
Expand Down Expand Up @@ -598,7 +599,12 @@ function file_fstat($fd, $info = null)
*/
function file_sendfile($out_fd, $in_fd, int $offset = 0, int $length = 8192)
{
return FileSystem::sendfile($out_fd, $in_fd, $offset, $length);
$written = yield FileSystem::sendfile($out_fd, $in_fd, $offset, $length);
if (FileSystem::useUvFs()) {
yield FileSystem::fdatasync($out_fd);
}

return $written;
}

/**
Expand Down
24 changes: 19 additions & 5 deletions Coroutine/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ final class FileSystem
'c+' => \UV::O_RDWR | \UV::O_CREAT,
);

/**
* Set of key => value pairs to include as default options/headers with `open` **uri** calls.
*/
protected static $fileOpenUriContext = [
'http' => [
'method' => 'GET',
Expand All @@ -46,7 +49,11 @@ final class FileSystem
'timeout' => 1,
'user_agent' => 'Symplely Coroutine',
'headers' => [
'Accept' => '*/*'
'Accept' => '*/*',
'Accept-Charset' => 'utf-8',
'Accept-Language' => 'en-US,en;q=0.9',
'X-Powered-By' => 'PHP/' . \PHP_VERSION,
'Connection' => 'close'
],
],
'ssl' => [
Expand All @@ -66,7 +73,7 @@ final class FileSystem
*
* @return bool
*/
protected static function useUvFs(): bool
public static function useUvFs(): bool
{
return \function_exists('uv_default_loop') && self::$useUV;
}
Expand Down Expand Up @@ -104,6 +111,13 @@ protected static function spawnLstat($path, $info = null)
return empty($info) ? $result : $result[$info];
}

protected static function fdStat($fd, $info = null)
{
$result = \fstat($fd);

return yield \result((empty($info) ? $result : $result[$info]));
}

/**
* Renames a file or directory.
*
Expand Down Expand Up @@ -713,6 +727,8 @@ function ($fd, $result) use ($task, $coroutine, $info) {
}
);
}

return self::fdStat($fd, $info);
}

/**
Expand Down Expand Up @@ -824,9 +840,6 @@ function ($fd, int $result) use ($task, $coroutine) {
}
}

/**
* @codeCoverageIgnore
*/
protected static function send($out_fd, $in_fd, int $offset = 0, int $length)
{
if (!\is_resource($out_fd) || !\is_resource($in_fd)) {
Expand All @@ -841,6 +854,7 @@ protected static function send($out_fd, $in_fd, int $offset = 0, int $length)
return yield \result(false);
}

@\rewind($out_fd);
yield Coroutine::value($count);
}
}
Expand Down
24 changes: 23 additions & 1 deletion tests/FileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ public function taskFileSystemSendfile()
$written = yield \file_sendfile($outFd, $fd, 0, $size);

$this->assertEquals($size, $written);
yield \file_fdatasync($outFd);
$data = yield \file_contents($outFd);
$this->assertEquals('Hello', \trim($data));
$this->assertGreaterThanOrEqual(6, $this->counterResult);
Expand All @@ -302,6 +301,29 @@ public function testFileSystemSendfile()
\coroutine_run($this->taskFileSystemSendfile());
}

public function taskFileSendfile()
{
\file_operation();
yield \away($this->counterTask());
$fd = yield \file_open(FIXTURE_PATH, 'r');
$size = yield \file_fstat($fd, 'size');
$outFd = yield \file_open('php://temp', 'w+');
$written = yield \file_sendfile($outFd, $fd, 0, $size);
$this->assertEquals($size, $written);
$data = yield \file_contents($outFd);
$this->assertEquals('Hello', \trim($data));
$this->assertGreaterThanOrEqual(8, $this->counterResult);
yield \file_close($fd);
yield \file_close($outFd);

yield \shutdown();
}

public function testFileSendfile()
{
\coroutine_run($this->taskFileSendfile());
}

public function taskSystemScandir()
{
\file_operation();
Expand Down

0 comments on commit f632a29

Please sign in to comment.