Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move stream factory creation to node #51

Merged
merged 1 commit into from Nov 18, 2018
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
6 changes: 3 additions & 3 deletions src/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
interface AdapterInterface
{
const CREATION_MODE = 'rwxrw----';

/**
* Checks whether the current installation supports the adapter.
*
* @return boolean
* @return bool
*/
public static function isSupported();

Expand Down Expand Up @@ -127,7 +127,7 @@ public function touch($path, $mode = self::CREATION_MODE);
* @param string $path
* @param string $flags
* @param $mode
* @return PromiseInterface<file descriptor>
* @return PromiseInterface
*/
public function open($path, $flags, $mode = self::CREATION_MODE);

Expand Down
5 changes: 2 additions & 3 deletions src/ChildProcess/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use React\Filesystem\TypeDetectorInterface;
use React\Filesystem\PermissionFlagResolver;
use React\Filesystem\Node\NodeInterface;
use React\Filesystem\Stream\StreamFactory;
use React\Promise\PromiseInterface;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
Expand Down Expand Up @@ -212,8 +211,8 @@ public function open($path, $flags, $mode = self::CREATION_MODE)
'flags' => $flags,
'mode' => $mode,
]));
})->then(function () use ($path, $flags, &$id) {
return \React\Promise\resolve(StreamFactory::create($path, $id, $flags, $this));
})->then(function () use (&$id) {
return $id;
});
}

Expand Down
7 changes: 2 additions & 5 deletions src/Eio/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use React\Filesystem\ObjectStream;
use React\Filesystem\OpenFileLimiter;
use React\Filesystem\PermissionFlagResolver;
use React\Filesystem\Stream\StreamFactory;
use React\Filesystem\TypeDetectorInterface;
use React\Promise\Deferred;

Expand Down Expand Up @@ -102,7 +101,7 @@ protected function applyConfiguration(array $options)
}

/**
* @return boolean
* @return bool
*/
public static function isSupported()
{
Expand Down Expand Up @@ -267,9 +266,7 @@ public function open($path, $flags, $mode = self::CREATION_MODE)
$eioFlags,
$mode,
]);
})->then(function ($fileDescriptor) use ($path, $flags) {
return StreamFactory::create($path, $fileDescriptor, $flags, $this);
}, function ($error) {
})->otherwise(function ($error) {
$this->openFileLimiter->close();
return \React\Promise\reject($error);
});
Expand Down
2 changes: 1 addition & 1 deletion src/Node/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public function lsRecursiveStreaming()

/**
* @param $sourceStream
* @return Stream
* @return ObjectStream
*/
protected function processLsRecursiveContents($sourceStream)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Node/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use React\Filesystem\FilesystemInterface;
use React\Filesystem\ObjectStream;
use React\Filesystem\ObjectStreamSink;
use React\Filesystem\Stream\GenericStreamInterface;
use React\Filesystem\Stream\StreamFactory;
use React\Promise\Stream;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableStreamInterface;
Expand Down Expand Up @@ -118,10 +118,10 @@ public function open($flags, $mode = AdapterInterface::CREATION_MODE)
return \React\Promise\reject(new Exception('File is already open'));
}

return $this->adapter->open($this->path, $flags, $mode)->then(function (GenericStreamInterface $stream) {
return $this->adapter->open($this->path, $flags, $mode)->then(function ($fd) use ($flags) {
$this->open = true;
$this->fileDescriptor = $stream->getFiledescriptor();
return $stream;
$this->fileDescriptor = $fd;
return StreamFactory::create($this->path, $fd, $flags, $this->adapter);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Stream/GenericStreamTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public function getFiledescriptor()
}

/**
* @return boolean
* @return bool
*/
public function isClosed()
{
return $this->closed;
}

/**
* @param boolean $closed
* @param bool $closed
*/
public function setClosed($closed)
{
Expand Down
90 changes: 23 additions & 67 deletions tests/Node/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,23 +235,21 @@ public function testOpen()
$path = 'foo.bar';
$filesystem = $this->mockAdapter();

$stream = $this->getMock('React\Filesystem\Stream\GenericStreamInterface', [], ['foo:bar']);
$fd = 'foo:bar';
$flags = 'abc';

$filesystem
->expects($this->once())
->method('open')
->with($path, $flags)
->will($this->returnValue(new FulfilledPromise($stream)))
->will($this->returnValue(new FulfilledPromise($fd)))
;

$callbackFired = false;
(new File($path, Filesystem::createFromAdapter($filesystem)))->open($flags)->then(function ($passStream) use (&$callbackFired, $stream) {
$this->assertSame($stream, $passStream);
$callbackFired = true;
});
$fs = Filesystem::createFromAdapter($filesystem);
$pass = $this->await((new File($path, $fs))->open($flags), $fs->getAdapter()->getLoop());

$this->assertTrue($callbackFired);
$this->assertInstanceOf('\React\Filesystem\Stream\GenericStreamInterface', $pass);
$this->assertSame($fd, $pass->getFiledescriptor());
}


Expand All @@ -260,14 +258,14 @@ public function testOpenTwice()
$path = 'foo.bar';
$filesystem = $this->mockAdapter();

$stream = $this->getMock('React\Filesystem\Stream\GenericStreamInterface', [], ['foo:bar']);
$fd = 'foo:bar';
$flags = 'abc';

$filesystem
->expects($this->once())
->method('open')
->with($path, $flags)
->will($this->returnValue(new FulfilledPromise($stream)))
->will($this->returnValue(new FulfilledPromise($fd)))
;

$file = new File($path, Filesystem::createFromAdapter($filesystem));
Expand All @@ -280,14 +278,10 @@ public function testGetContents()
$path = 'foo.bar';
$fd = '0123456789abcdef';

$openPromise = $this->getMock('React\Promise\PromiseInterface', [
'then',
]);

$filesystem = $this->mockAdapter();

$filesystem
->expects($this->once())
->expects($this->any())
->method('stat')
->with($path)
->will($this->returnValue(new FulfilledPromise([
Expand All @@ -299,14 +293,14 @@ public function testGetContents()
->expects($this->once())
->method('open')
->with($path, 'r')
->will($this->returnValue($openPromise))
->will($this->returnValue(new FulfilledPromise($fd)))
;

$filesystem
->expects($this->once())
->method('read')
->with($fd, 1, 0)
->will($this->returnValue(new FulfilledPromise(str_repeat('a', 1))))
->will($this->returnValue(new FulfilledPromise('a')))
;

$filesystem
Expand All @@ -316,21 +310,6 @@ public function testGetContents()
->will($this->returnValue(new FulfilledPromise()))
;

$stream = new ReadableStream(
$path,
$fd,
$filesystem
);

$openPromise
->expects($this->once())
->method('then')
->with($this->isType('callable'))
->will($this->returnCallback(function ($resolveCb) use ($stream) {
return new FulfilledPromise($resolveCb($stream));
}))
;

$getContentsPromise = (new File($path, Filesystem::createFromAdapter($filesystem)))->getContents();
$this->assertInstanceOf('React\Promise\PromiseInterface', $getContentsPromise);
}
Expand All @@ -341,30 +320,20 @@ public function testClose()
$fd = '0123456789abcdef';
$filesystem = $this->mockAdapter();

$stream = $this->getMock('React\Filesystem\Stream\GenericStreamInterface', [
'getFiledescriptor',
], [
'foo:bar',
]);
$openPromise = new FulfilledPromise($fd);

$stream
->expects($this->once())
->method('getFiledescriptor')
->with()
->will($this->returnValue($fd))
$filesystem
->method('stat')
->with($path)
->will($this->returnValue(new FulfilledPromise([
'size' => 1,
])))
;

$openPromise = $this->getMock('React\Promise\PromiseInterface', [
'then',
]);

$openPromise
->expects($this->once())
->method('then')
->with($this->isType('callable'))
->will($this->returnCallback(function ($resolveCb) use ($stream) {
return new FulfilledPromise($resolveCb($stream));
}))
$filesystem
->method('read')
->with($path)
->will($this->returnValue(new FulfilledPromise('a')))
;

$filesystem
Expand All @@ -374,24 +343,11 @@ public function testClose()
->will($this->returnValue($openPromise))
;

$closePromise = $this->getMock('React\Promise\PromiseInterface', [
'then',
]);

$closePromise
->expects($this->once())
->method('then')
->with($this->isType('callable'))
->will($this->returnCallback(function ($resolveCb) use ($stream) {
return \React\Promise\resolve($resolveCb($stream));
}))
;

$filesystem
->expects($this->once())
->method('close')
->with($fd)
->will($this->returnValue($closePromise))
->will($this->returnValue(\React\Promise\resolve()))
;

$file = new File($path, Filesystem::createFromAdapter($filesystem));
Expand Down