From a9ad1311dd2834340055e3cc0884f173ba67ed93 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Thu, 4 Apr 2019 22:11:44 +0200 Subject: [PATCH] Remove traces of call invokers --- src/AdapterInterface.php | 8 - src/CallInvokerInterface.php | 32 --- src/ChildProcess/Adapter.php | 37 +--- src/Eio/Adapter.php | 50 ++--- src/Filesystem.php | 8 - src/FilesystemInterface.php | 5 - src/InstantInvoker.php | 48 ----- src/PooledInvoker.php | 119 ----------- src/QueuedInvoker.php | 105 ---------- src/ThrottledQueuedInvoker.php | 133 ------------ src/functions.php | 16 -- src/functions_include.php | 2 +- tests/CallInvokerProvider.php | 64 ------ tests/ChildProcess/AdapterTest.php | 249 ++++++++--------------- tests/ChildProcess/SingletonPoolStub.php | 80 ++++++++ tests/FilesystemTest.php | 18 -- tests/FunctionsTest.php | 21 -- tests/InstantInvokerTest.php | 33 --- tests/PooledInvokerTest.php | 62 ------ tests/QueuedInvokerTest.php | 62 ------ tests/ThrottledQueuedInvokerTest.php | 71 ------- 21 files changed, 194 insertions(+), 1029 deletions(-) delete mode 100644 src/CallInvokerInterface.php delete mode 100644 src/InstantInvoker.php delete mode 100644 src/PooledInvoker.php delete mode 100644 src/QueuedInvoker.php delete mode 100644 src/ThrottledQueuedInvoker.php delete mode 100644 tests/CallInvokerProvider.php create mode 100644 tests/ChildProcess/SingletonPoolStub.php delete mode 100644 tests/InstantInvokerTest.php delete mode 100644 tests/PooledInvokerTest.php delete mode 100644 tests/QueuedInvokerTest.php delete mode 100644 tests/ThrottledQueuedInvokerTest.php diff --git a/src/AdapterInterface.php b/src/AdapterInterface.php index 9d94496d..945d2f3a 100644 --- a/src/AdapterInterface.php +++ b/src/AdapterInterface.php @@ -33,14 +33,6 @@ public function getLoop(); */ public function setFilesystem(FilesystemInterface $filesystem); - /** - * Set the call invoker for this adapter. - * - * @param CallInvokerInterface $invoker - * @return void - */ - public function setInvoker(CallInvokerInterface $invoker); - /** * Call the underlying filesystem. * diff --git a/src/CallInvokerInterface.php b/src/CallInvokerInterface.php deleted file mode 100644 index 7327ad40..00000000 --- a/src/CallInvokerInterface.php +++ /dev/null @@ -1,32 +0,0 @@ -loop = $loop; - $this->invoker = \React\Filesystem\getInvoker($this, $options, 'invoker', 'React\Filesystem\InstantInvoker'); $this->openFileLimiter = new OpenFileLimiter(\React\Filesystem\getOpenFileLimit($options)); $this->permissionFlagResolver = new PermissionFlagResolver(); @@ -145,15 +139,6 @@ public function setFilesystem(FilesystemInterface $filesystem) ]; } - /** - * @param CallInvokerInterface $invoker - * @return void - */ - public function setInvoker(CallInvokerInterface $invoker) - { - $this->invoker = $invoker; - } - /** * @param string $function * @param array $args @@ -176,7 +161,7 @@ public function callFilesystem($function, $args, $errorResultCode = -1) */ public function chmod($path, $mode) { - return $this->invoker->invokeCall('chmod', [ + return $this->callFilesystem('chmod', [ 'path' => $path, 'mode' => decoct($mode), ]); @@ -189,7 +174,7 @@ public function chmod($path, $mode) */ public function mkdir($path, $mode = self::CREATION_MODE) { - return $this->invoker->invokeCall('mkdir', [ + return $this->callFilesystem('mkdir', [ 'path' => $path, 'mode' => decoct($this->permissionFlagResolver->resolve($mode)), ]); @@ -271,7 +256,7 @@ public function close($fd) */ public function rmdir($path) { - return $this->invoker->invokeCall('rmdir', [ + return $this->callFilesystem('rmdir', [ 'path' => $path, ]); } @@ -282,7 +267,7 @@ public function rmdir($path) */ public function unlink($path) { - return $this->invoker->invokeCall('unlink', [ + return $this->callFilesystem('unlink', [ 'path' => $path, ]); } @@ -295,7 +280,7 @@ public function unlink($path) */ public function chown($path, $uid, $gid) { - return $this->invoker->invokeCall('chown', [ + return $this->callFilesystem('chown', [ 'path' => $path, 'uid' => $uid, 'gid' => $gid, @@ -308,7 +293,7 @@ public function chown($path, $uid, $gid) */ public function stat($filename) { - return $this->invoker->invokeCall('stat', [ + return $this->callFilesystem('stat', [ 'path' => $filename, ])->then(function ($stat) { $stat['atime'] = new DateTime('@' . $stat['atime']); @@ -335,7 +320,7 @@ public function lsStream($path) { $stream = new ObjectStream(); - $this->invoker->invokeCall('readdir', [ + $this->callFilesystem('readdir', [ 'path' => $path, 'flags' => $this->options['lsFlags'], ])->then(function ($result) use ($path, $stream) { @@ -372,7 +357,7 @@ protected function processLsContents($basePath, $result, ObjectStream $stream) */ public function touch($path, $mode = self::CREATION_MODE) { - return $this->invoker->invokeCall('touch', [ + return $this->callFilesystem('touch', [ 'path' => $path, 'mode' => decoct($this->permissionFlagResolver->resolve($mode)), ]); @@ -385,7 +370,7 @@ public function touch($path, $mode = self::CREATION_MODE) */ public function rename($fromPath, $toPath) { - return $this->invoker->invokeCall('rename', [ + return $this->callFilesystem('rename', [ 'from' => $fromPath, 'to' => $toPath, ]); @@ -397,7 +382,7 @@ public function rename($fromPath, $toPath) */ public function readlink($path) { - return $this->invoker->invokeCall('readlink', [ + return $this->callFilesystem('readlink', [ 'path' => $path, ])->then(function ($result) { return \React\Promise\resolve($result['path']); @@ -411,7 +396,7 @@ public function readlink($path) */ public function symlink($fromPath, $toPath) { - return $this->invoker->invokeCall('symlink', [ + return $this->callFilesystem('symlink', [ 'from' => $fromPath, 'to' => $toPath, ])->then(function ($result) { diff --git a/src/Eio/Adapter.php b/src/Eio/Adapter.php index 361224ec..a57f0138 100644 --- a/src/Eio/Adapter.php +++ b/src/Eio/Adapter.php @@ -44,16 +44,6 @@ class Adapter implements AdapterInterface */ protected $permissionFlagResolver; - /** - * @var CallInvokerInterface - */ - protected $invoker; - - /** - * @var CallInvokerInterface - */ - protected $readDirInvoker; - /** * @var FilesystemInterface */ @@ -96,8 +86,6 @@ public function __construct(LoopInterface $loop, array $options = []) */ protected function applyConfiguration(array $options) { - $this->invoker = \React\Filesystem\getInvoker($this, $options, 'invoker', 'React\Filesystem\InstantInvoker'); - $this->readDirInvoker = \React\Filesystem\getInvoker($this, $options, 'read_dir_invoker', 'React\Filesystem\InstantInvoker'); $this->openFileLimiter = new OpenFileLimiter(\React\Filesystem\getOpenFileLimit($options)); $this->options = array_merge_recursive($this->options, $options); } @@ -118,14 +106,6 @@ public function getLoop() return $this->loop; } - /** - * {@inheritDoc} - */ - public function setInvoker(CallInvokerInterface $invoker) - { - $this->invoker = $invoker; - } - /** * {@inheritDoc} */ @@ -152,7 +132,7 @@ public function setReadDirInvoker(CallInvokerInterface $invoker) */ public function stat($filename) { - return $this->invoker->invokeCall('eio_lstat', [$filename])->then(function ($stat) { + return $this->callFilesystem('eio_lstat', [$filename])->then(function ($stat) { $stat['atime'] = new DateTime('@' .$stat['atime']); $stat['mtime'] = new DateTime('@' .$stat['mtime']); $stat['ctime'] = new DateTime('@' .$stat['ctime']); @@ -165,7 +145,7 @@ public function stat($filename) */ public function unlink($filename) { - return $this->invoker->invokeCall('eio_unlink', [$filename]); + return $this->callFilesystem('eio_unlink', [$filename]); } /** @@ -173,7 +153,7 @@ public function unlink($filename) */ public function rename($fromFilename, $toFilename) { - return $this->invoker->invokeCall('eio_rename', [$fromFilename, $toFilename]); + return $this->callFilesystem('eio_rename', [$fromFilename, $toFilename]); } /** @@ -181,7 +161,7 @@ public function rename($fromFilename, $toFilename) */ public function chmod($path, $mode) { - return $this->invoker->invokeCall('eio_chmod', [$path, $mode]); + return $this->callFilesystem('eio_chmod', [$path, $mode]); } /** @@ -189,7 +169,7 @@ public function chmod($path, $mode) */ public function chown($path, $uid, $gid) { - return $this->invoker->invokeCall('eio_chown', [$path, $uid, $gid]); + return $this->callFilesystem('eio_chown', [$path, $uid, $gid]); } /** @@ -250,7 +230,7 @@ protected function processLsContents($basePath, $result, ObjectStream $stream) */ public function mkdir($path, $mode = self::CREATION_MODE) { - return $this->invoker->invokeCall('eio_mkdir', [ + return $this->callFilesystem('eio_mkdir', [ $path, $this->permissionFlagResolver->resolve($mode), ]); @@ -261,7 +241,7 @@ public function mkdir($path, $mode = self::CREATION_MODE) */ public function rmdir($path) { - return $this->invoker->invokeCall('eio_rmdir', [$path]); + return $this->callFilesystem('eio_rmdir', [$path]); } /** @@ -272,7 +252,7 @@ public function open($path, $flags, $mode = self::CREATION_MODE) $eioFlags = $this->openFlagResolver->resolve($flags); $mode = $this->permissionFlagResolver->resolve($mode); return $this->openFileLimiter->open()->then(function () use ($path, $eioFlags, $mode) { - return $this->invoker->invokeCall('eio_open', [ + return $this->callFilesystem('eio_open', [ $path, $eioFlags, $mode, @@ -288,7 +268,7 @@ public function open($path, $flags, $mode = self::CREATION_MODE) */ public function close($fd) { - return $this->invoker->invokeCall('eio_close', [$fd])->always(function () { + return $this->callFilesystem('eio_close', [$fd])->always(function () { $this->openFileLimiter->close(); }); } @@ -302,14 +282,14 @@ public function touch($path, $mode = self::CREATION_MODE, $time = null) if ($time === null) { $time = microtime(true); } - return $this->invoker->invokeCall('eio_utime', [ + return $this->callFilesystem('eio_utime', [ $path, $time, $time, ]); }, function () use ($path, $mode) { return $this->openFileLimiter->open()->then(function () use ($path, $mode) { - return $this->invoker->invokeCall('eio_open', [ + return $this->callFilesystem('eio_open', [ $path, EIO_O_CREAT, $this->permissionFlagResolver->resolve($mode), @@ -325,7 +305,7 @@ public function touch($path, $mode = self::CREATION_MODE, $time = null) */ public function read($fileDescriptor, $length, $offset) { - return $this->invoker->invokeCall('eio_read', [ + return $this->callFilesystem('eio_read', [ $fileDescriptor, $length, $offset, @@ -337,7 +317,7 @@ public function read($fileDescriptor, $length, $offset) */ public function write($fileDescriptor, $data, $length, $offset) { - return $this->invoker->invokeCall('eio_write', [ + return $this->callFilesystem('eio_write', [ $fileDescriptor, $data, $length, @@ -350,7 +330,7 @@ public function write($fileDescriptor, $data, $length, $offset) */ public function readlink($path) { - return $this->invoker->invokeCall('eio_readlink', [ + return $this->callFilesystem('eio_readlink', [ $path, ]); } @@ -360,7 +340,7 @@ public function readlink($path) */ public function symlink($fromPath, $toPath) { - return $this->invoker->invokeCall('eio_symlink', [ + return $this->callFilesystem('eio_symlink', [ $fromPath, $toPath, ]); diff --git a/src/Filesystem.php b/src/Filesystem.php index db596906..d2bd474e 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -137,12 +137,4 @@ public function getContents($filename) return $file->getContents(); }); } - - /** - * @param CallInvokerInterface $invoker - */ - public function setInvoker(CallInvokerInterface $invoker) - { - $this->adapter->setInvoker($invoker); - } } diff --git a/src/FilesystemInterface.php b/src/FilesystemInterface.php index a2c4d1be..25d70794 100644 --- a/src/FilesystemInterface.php +++ b/src/FilesystemInterface.php @@ -56,9 +56,4 @@ public function link($path, Node\NodeInterface $destination); * @return \React\Promise\PromiseInterface */ public function getContents($filename); - - /** - * @param CallInvokerInterface $invoker - */ - public function setInvoker(CallInvokerInterface $invoker); } diff --git a/src/InstantInvoker.php b/src/InstantInvoker.php deleted file mode 100644 index 66653b60..00000000 --- a/src/InstantInvoker.php +++ /dev/null @@ -1,48 +0,0 @@ -loop = $adapter->getLoop(); - $this->adapter = $adapter; - } - - /** - * @param string $function - * @param array $args - * @param int $errorResultCode - * @return \React\Promise\ExtendedPromiseInterface - */ - public function invokeCall($function, $args, $errorResultCode = -1) - { - return $this-> - adapter-> - callFilesystem($function, $args, $errorResultCode); - } - - /** - * @return bool - */ - public function isEmpty() - { - return true; - } -} diff --git a/src/PooledInvoker.php b/src/PooledInvoker.php deleted file mode 100644 index 715139ab..00000000 --- a/src/PooledInvoker.php +++ /dev/null @@ -1,119 +0,0 @@ -loop = $adapter->getLoop(); - $this->adapter = $adapter; - $this->callQueue = new \SplQueue(); - $this->maxSimultaneousOperations = $maxSimultaneousOperations; - } - - /** - * @param string $function - * @param array $args - * @param int $errorResultCode - * @return \React\Promise\ExtendedPromiseInterface - */ - public function invokeCall($function, $args, $errorResultCode = -1) - { - $this->callQueueActive = true; - $deferred = new Deferred(); - - $this->callQueue->enqueue(new QueuedCall($deferred, $function, $args, $errorResultCode)); - - if (!$this->callQueue->isEmpty() && $this->runningOperations < $this->maxSimultaneousOperations) { - $this->processQueue(); - } - - return $deferred->promise()->then(function ($data) { - return $this-> - adapter-> - callFilesystem($data['function'], $data['args'], $data['errorResultCode'])-> - then($this->filesystemResultHandler('React\Promise\resolve'), $this->filesystemResultHandler('React\Promise\reject')); - }); - } - - /** - * @return bool - */ - public function isEmpty() - { - return $this->callQueue->isEmpty(); - } - - protected function processQueue() - { - $this->loop->futureTick(function () { - if ($this->callQueue->isEmpty()) { - return; - } - - $this->runningOperations++; - - $message = $this->callQueue->dequeue(); - $data = [ - 'function' => $message->getFunction(), - 'args' => $message->getArgs(), - 'errorResultCode' => $message->getErrorResultCode(), - ]; - - $message->getDeferred()->resolve($data); - }); - } - - protected function filesystemResultHandler($func) - { - return function ($mixed) use ($func) { - if ($this->callQueue->count() == 0) { - $this->callQueueActive = false; - } else { - $this->processQueue(); - } - - $this->runningOperations--; - - return $func($mixed); - }; - } -} diff --git a/src/QueuedInvoker.php b/src/QueuedInvoker.php deleted file mode 100644 index d6b18b43..00000000 --- a/src/QueuedInvoker.php +++ /dev/null @@ -1,105 +0,0 @@ -loop = $adapter->getLoop(); - $this->adapter = $adapter; - $this->callQueue = new \SplQueue(); - } - - /** - * @param string $function - * @param array $args - * @param int $errorResultCode - * @return \React\Promise\ExtendedPromiseInterface - */ - public function invokeCall($function, $args, $errorResultCode = -1) - { - $this->callQueueActive = true; - $deferred = new Deferred(); - - $this->callQueue->enqueue(new QueuedCall($deferred, $function, $args, $errorResultCode)); - - if (!$this->callQueue->isEmpty()) { - $this->processQueue(); - } - - return $deferred->promise()->then(function ($data) { - return $this-> - adapter-> - callFilesystem($data['function'], $data['args'], $data['errorResultCode'])-> - then($this->filesystemResultHandler('React\Promise\resolve'), $this->filesystemResultHandler('React\Promise\reject')); - }); - } - - /** - * @return bool - */ - public function isEmpty() - { - return $this->callQueue->isEmpty(); - } - - protected function processQueue() - { - $this->loop->futureTick(function () { - if ($this->callQueue->isEmpty()) { - return; - } - - $message = $this->callQueue->dequeue(); - $data = [ - 'function' => $message->getFunction(), - 'args' => $message->getArgs(), - 'errorResultCode' => $message->getErrorResultCode(), - ]; - $message->getDeferred()->resolve($data); - }); - } - - /** - * @param callable $func - * @return callable - */ - protected function filesystemResultHandler(callable $func) - { - return function ($mixed) use ($func) { - if ($this->callQueue->count() == 0) { - $this->callQueueActive = false; - } else { - $this->processQueue(); - } - return $func($mixed); - }; - } -} diff --git a/src/ThrottledQueuedInvoker.php b/src/ThrottledQueuedInvoker.php deleted file mode 100644 index f12f48a9..00000000 --- a/src/ThrottledQueuedInvoker.php +++ /dev/null @@ -1,133 +0,0 @@ -loop = $adapter->getLoop(); - $this->adapter = $adapter; - $this->callQueue = new \SplQueue(); - $this->interval = $interval; - } - /** - * @param float $interval - */ - public function setInterval($interval) - { - $this->interval = $interval; - } - - /** - * @return float - */ - public function getInterval() - { - return $this->interval; - } - - /** - * @param string $function - * @param array $args - * @param int $errorResultCode - * @return \React\Promise\ExtendedPromiseInterface - */ - public function invokeCall($function, $args, $errorResultCode = -1) - { - $this->callQueueActive = true; - $deferred = new Deferred(); - - $this->callQueue->enqueue(new QueuedCall($deferred, $function, $args, $errorResultCode)); - - if (!$this->callQueue->isEmpty()) { - $this->processQueue(); - } - - return $deferred->promise()->then(function ($data) { - return $this-> - adapter-> - callFilesystem($data['function'], $data['args'], $data['errorResultCode'])-> - then($this->filesystemResultHandler('React\Promise\resolve'), $this->filesystemResultHandler('React\Promise\reject')); - }); - } - - /** - * @return bool - */ - public function isEmpty() - { - return $this->callQueue->isEmpty(); - } - - protected function processQueue() - { - $this->loop->addTimer($this->interval, function () { - if ($this->callQueue->isEmpty()) { - return; - } - - $message = $this->callQueue->dequeue(); - $data = [ - 'function' => $message->getFunction(), - 'args' => $message->getArgs(), - 'errorResultCode' => $message->getErrorResultCode(), - ]; - $message->getDeferred()->resolve($data); - }); - } - - /** - * @param $func - * - * @return callable - */ - protected function filesystemResultHandler($func) - { - return function ($mixed) use ($func) { - if ($this->callQueue->count() == 0) { - $this->callQueueActive = false; - } else { - $this->processQueue(); - } - return $func($mixed); - }; - } -} diff --git a/src/functions.php b/src/functions.php index 184f3796..741f70e2 100644 --- a/src/functions.php +++ b/src/functions.php @@ -4,22 +4,6 @@ use Exception; -/** - * @param AdapterInterface $adapter - * @param array $options - * @param string $key - * @param string $fallback - * @return CallInvokerInterface - */ -function getInvoker(AdapterInterface $adapter, array $options, $key, $fallback) -{ - if (isset($options[$key]) && $options[$key] instanceof CallInvokerInterface) { - return $options[$key]; - } - - return new $fallback($adapter); -} - /** * @param array $options * @return int diff --git a/src/functions_include.php b/src/functions_include.php index 2799c547..9152e111 100644 --- a/src/functions_include.php +++ b/src/functions_include.php @@ -2,6 +2,6 @@ namespace React\Filesystem; -if (!function_exists('React\Filesystem\getInvoker')) { +if (!function_exists('React\Filesystem\getOpenFileLimit')) { require __DIR__ . '/functions.php'; } diff --git a/tests/CallInvokerProvider.php b/tests/CallInvokerProvider.php deleted file mode 100644 index e0cb173f..00000000 --- a/tests/CallInvokerProvider.php +++ /dev/null @@ -1,64 +0,0 @@ -mockAdapter($loop); - } - - return [ - 'pooled' => $this->pooled($loop, $adapter), - 'instant' => $this->instant($loop, $adapter), - 'queued' => $this->queued($loop, $adapter), - 'throttledqueued' => $this->throttledqueued($loop, $adapter), - ]; - } - - protected function pooled($loop, $adapter) - { - $invoker = new PooledInvoker($adapter); - $adapter->setInvoker($invoker); - - return [$loop, $adapter, $invoker]; - } - - protected function instant($loop, $adapter) - { - $invoker = new InstantInvoker($adapter); - $adapter->setInvoker($invoker); - - return [$loop, $adapter, $invoker]; - } - - protected function queued($loop, $adapter) - { - $invoker = new QueuedInvoker($adapter); - $adapter->setInvoker($invoker); - - return [$loop, $adapter, $invoker]; - } - - protected function throttledqueued($loop, $adapter) - { - $invoker = new ThrottledQueuedInvoker($adapter); - $adapter->setInvoker($invoker); - - return [$loop, $adapter, $invoker]; - } -} diff --git a/tests/ChildProcess/AdapterTest.php b/tests/ChildProcess/AdapterTest.php index 9fa07a51..87b640c6 100644 --- a/tests/ChildProcess/AdapterTest.php +++ b/tests/ChildProcess/AdapterTest.php @@ -8,11 +8,16 @@ use React\Filesystem\Node\File; use React\Filesystem\Node\NodeInterface; use React\Promise\Deferred; -use React\Promise\FulfilledPromise; use React\Tests\Filesystem\TestCase; +use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload; class AdapterTest extends TestCase { + public function tearDown() + { + SingletonPoolStub::reset(); + } + public function testInterface() { $this->assertInstanceOf( @@ -48,7 +53,7 @@ public function callFilesystemProvider() 'mkdir', [ 'path' => 'foo.bar', - 'mode' => 760, + 'mode' => '760', ], ], ], @@ -62,7 +67,7 @@ public function callFilesystemProvider() 'mkdir', [ 'path' => 'foo.bar', - 'mode' => 777, + 'mode' => '777', ], ], ], @@ -99,7 +104,7 @@ public function callFilesystemProvider() 'touch', [ 'path' => 'foo.bar', - 'mode' => 760, + 'mode' => '760', ], ], ], @@ -143,139 +148,71 @@ public function callFilesystemProvider() 'chmod', [ 'path' => 'foo.bar', - 'mode' => 123, + 'mode' => '123', ], ], ], - ]; - } - - /** - * @dataProvider callFilesystemProvider - */ - public function testCallFilesystem($method, $arguments, $mockArguments) - { - $loop = $this->getMock('React\EventLoop\LoopInterface'); - $filesystem = new Adapter($loop, [ - 'pool' => [ - 'class' => 'WyriHaximus\React\ChildProcess\Pool\Pool\Dummy', - ], - ]); - $invoker = $this->getMock('React\Filesystem\CallInvokerInterface', [ - '__construct', - 'invokeCall', - 'isEmpty', - ]); - $filesystem->setInvoker($invoker); - - $promise = new FulfilledPromise(); - - call_user_func_array([ - $invoker - ->expects($this->once()) - ->method('invokeCall') - , - 'with', - ], $mockArguments)->will($this->returnValue($promise)); - - $this->assertSame($promise, call_user_func_array([$filesystem, $method], $arguments)); - } - - public function testReadlink() - { - $loop = $this->getMock('React\EventLoop\LoopInterface'); - $filesystem = new Adapter($loop, [ - 'pool' => [ - 'class' => 'WyriHaximus\React\ChildProcess\Pool\Pool\Dummy', - ], - ]); - $invoker = $this->getMock('React\Filesystem\CallInvokerInterface', [ - '__construct', - 'invokeCall', - 'isEmpty', - ]); - $filesystem->setInvoker($invoker); - - $invoker - ->expects($this->once()) - ->method('invokeCall') - ->with( + [ 'readlink', [ - 'path' => 'foo.bar', - ] - )->will($this->returnValue(new FulfilledPromise([ - 'path' => 'bar.foo', - ]))) - ; - - $filesystem->readlink('foo.bar'); - } - - public function testStat() - { - $loop = $this->getMock('React\EventLoop\LoopInterface'); - $filesystem = new Adapter($loop, [ - 'pool' => [ - 'class' => 'WyriHaximus\React\ChildProcess\Pool\Pool\Dummy', + 'foo.bar', + ], + [ + 'readlink', + [ + 'path' => 'foo.bar', + ], + ], ], - ]); - $invoker = $this->getMock('React\Filesystem\CallInvokerInterface', [ - '__construct', - 'invokeCall', - 'isEmpty', - ]); - $filesystem->setInvoker($invoker); - - $time = time(); - $invoker - ->expects($this->once()) - ->method('invokeCall') - ->with( + [ 'stat', [ - 'path' => 'foo.bar', - ] - )->will($this->returnValue(new FulfilledPromise([ - 'atime' => $time, - 'mtime' => $time, - 'ctime' => $time, - ]))) - ; - - $filesystem->stat('foo.bar'); + 'foo.bar', + ], + [ + 'stat', + [ + 'path' => 'foo.bar', + ], + ], + ], + [ + 'symlink', + [ + 'foo.bar', + 'bar.foo', + ], + [ + 'symlink', + [ + 'from' => 'foo.bar', + 'to' => 'bar.foo', + ], + ], + ], + ]; } - public function testSymlink() + /** + * @dataProvider callFilesystemProvider + */ + public function testCallFilesystem($method, $arguments, $mockArguments) { $loop = $this->getMock('React\EventLoop\LoopInterface'); $filesystem = new Adapter($loop, [ 'pool' => [ - 'class' => 'WyriHaximus\React\ChildProcess\Pool\Pool\Dummy', + 'class' => 'React\Tests\Filesystem\ChildProcess\SingletonPoolStub', ], ]); - $invoker = $this->getMock('React\Filesystem\CallInvokerInterface', [ - '__construct', - 'invokeCall', - 'isEmpty', - ]); - $filesystem->setInvoker($invoker); - $invoker - ->expects($this->once()) - ->method('invokeCall') - ->with( - 'symlink', - [ - 'from' => 'foo.bar', - 'to' => 'bar.foo', - ] - )->will($this->returnValue(new FulfilledPromise([ - 'result' => true, - ]))) - ; + call_user_func_array([$filesystem, $method], $arguments); - $filesystem->symlink('foo.bar', 'bar.foo'); + $calls = SingletonPoolStub::getCalls(); + self::assertCount(1, $calls); + /** @var array $call */ + $call = $calls[0][1][0]->jsonSerialize(); + self::assertSame($mockArguments[0], $call['target']); + self::assertSame($mockArguments[1], $call['payload']->getPayload()); } public function testLs() @@ -283,43 +220,36 @@ public function testLs() $loop = \React\EventLoop\Factory::create(); $adapter = new Adapter($loop, [ 'pool' => [ - 'class' => 'WyriHaximus\React\ChildProcess\Pool\Pool\Dummy', + 'class' => 'React\Tests\Filesystem\ChildProcess\SingletonPoolStub', ], ]); - $invoker = $this->getMock('React\Filesystem\CallInvokerInterface', [ - '__construct', - 'invokeCall', - 'isEmpty', - ]); - $adapter->setInvoker($invoker); - $fs = Filesystem::createFromAdapter($adapter); $deferred = new Deferred(); + SingletonPoolStub::setRpcResponse($deferred->promise()); - $invoker - ->expects($this->once()) - ->method('invokeCall') - ->with( - 'readdir', - [ - 'path' => 'foo.bar', - 'flags' => 2, - ] - )->will($this->returnValue($deferred->promise())) - ; + $fs = Filesystem::createFromAdapter($adapter); $promise = $adapter->ls('foo.bar'); $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); - - $deferred->resolve([ + $deferred->resolve(new Payload([ [ 'type' => 'file', 'name' => 'bar.foo', ], - ]); + ])); $nodes = $this->await($promise, $loop); + $calls = SingletonPoolStub::getCalls(); + self::assertCount(1, $calls); + /** @var array $call */ + $call = $calls[0][1][0]->jsonSerialize(); + self::assertSame('readdir', $call['target']); + self::assertSame([ + 'path' => 'foo.bar', + 'flags' => 2, + ], $call['payload']->getPayload()); + $this->assertEquals(new File('foo.bar/bar.foo', $fs), reset($nodes)); } @@ -328,30 +258,14 @@ public function testLsStream() $loop = $this->getMock('React\EventLoop\LoopInterface'); $adapter = new Adapter($loop, [ 'pool' => [ - 'class' => 'WyriHaximus\React\ChildProcess\Pool\Pool\Dummy', + 'class' => 'React\Tests\Filesystem\ChildProcess\SingletonPoolStub', ], ]); - $invoker = $this->getMock('React\Filesystem\CallInvokerInterface', [ - '__construct', - 'invokeCall', - 'isEmpty', - ]); - $adapter->setInvoker($invoker); - Filesystem::createFromAdapter($adapter); $deferred = new Deferred(); + SingletonPoolStub::setRpcResponse($deferred->promise()); - $invoker - ->expects($this->once()) - ->method('invokeCall') - ->with( - 'readdir', - [ - 'path' => 'foo.bar', - 'flags' => 2, - ] - )->will($this->returnValue($deferred->promise())) - ; + Filesystem::createFromAdapter($adapter); $stream = $adapter->lsStream('foo.bar'); $this->assertInstanceOf('React\Filesystem\ObjectStream', $stream); @@ -363,12 +277,23 @@ public function testLsStream() $calledOnData = true; }); - $deferred->resolve([ + $deferred->resolve(new Payload([ [ 'type' => 'file', 'name' => 'bar.foo', ], - ]); + ])); + + $calls = SingletonPoolStub::getCalls(); + self::assertCount(1, $calls); + /** @var array $call */ + $call = $calls[0][1][0]->jsonSerialize(); + self::assertSame('readdir', $call['target']); + self::assertSame([ + 'path' => 'foo.bar', + 'flags' => 2, + ], $call['payload']->getPayload()); + $this->assertTrue($calledOnData); } diff --git a/tests/ChildProcess/SingletonPoolStub.php b/tests/ChildProcess/SingletonPoolStub.php new file mode 100644 index 00000000..1cf7c022 --- /dev/null +++ b/tests/ChildProcess/SingletonPoolStub.php @@ -0,0 +1,80 @@ +getContents('foo.bar') ); } - - public function testSetFilesystemAndInvoker() - { - $adapter = $this->mockAdapter(); - $invoker = new InstantInvoker($adapter); - $adapter - ->expects($this->at(0)) - ->method('setFilesystem') - ->with($this->isInstanceOf('React\Filesystem\Filesystem')) - ; - $adapter - ->expects($this->at(1)) - ->method('setInvoker') - ->with($invoker) - ; - Filesystem::createFromAdapter($adapter)->setInvoker($invoker); - } } diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 5cefe6bb..f1abbd3d 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -6,27 +6,6 @@ class FunctionsTest extends TestCase { - public function testGetInvoker() - { - $adapter = $this->getMock('React\Filesystem\AdapterInterface'); - $callInvoker = $this->getMock('React\Filesystem\CallInvokerInterface'); - $key = 'k'; - $options = [ - $key => $callInvoker, - ]; - $fallback = ''; - $this->assertSame($callInvoker, \React\Filesystem\getInvoker($adapter, $options, $key, $fallback)); - } - - public function testGetInvokerFallback() - { - $adapter = $this->getMock('React\Filesystem\AdapterInterface'); - $key = 'k'; - $options = []; - $fallback = '\stdClass'; - $this->assertInstanceOf($fallback, \React\Filesystem\getInvoker($adapter, $options, $key, $fallback)); - } - public function testGetOpenFileLimit() { $limit = 123; diff --git a/tests/InstantInvokerTest.php b/tests/InstantInvokerTest.php deleted file mode 100644 index 07d82d13..00000000 --- a/tests/InstantInvokerTest.php +++ /dev/null @@ -1,33 +0,0 @@ -mockAdapter(); - - - $filesystem - ->expects($this->once()) - ->method('callFilesystem') - ->with($function, $args, $errorResultCode) - ; - - - $invoker = new InstantInvoker($filesystem); - $this->assertTrue($invoker->isEmpty()); - $invoker->invokeCall($function, $args, $errorResultCode); - $this->assertTrue($invoker->isEmpty()); - } -} diff --git a/tests/PooledInvokerTest.php b/tests/PooledInvokerTest.php deleted file mode 100644 index 48490ab0..00000000 --- a/tests/PooledInvokerTest.php +++ /dev/null @@ -1,62 +0,0 @@ -mockAdapter($loop); - - foreach ($function as $key => $value) { - $filesystem - ->expects($this->at($key + 1)) - ->method('callFilesystem') - ->with($function[$key], $args[$key], $errorResultCode[$key]) - ->will($this->returnValue(new FulfilledPromise())) - ; - } - - $invoker = new PooledInvoker($filesystem); - $this->assertTrue($invoker->isEmpty()); - foreach ($function as $key => $value) { - $invoker->invokeCall($function[$key], $args[$key], $errorResultCode[$key]); - } - $this->assertFalse($invoker->isEmpty()); - - $loop->run(); - $this->assertTrue($invoker->isEmpty()); - } -} diff --git a/tests/QueuedInvokerTest.php b/tests/QueuedInvokerTest.php deleted file mode 100644 index d6cd158b..00000000 --- a/tests/QueuedInvokerTest.php +++ /dev/null @@ -1,62 +0,0 @@ -mockAdapter($loop); - - foreach ($function as $key => $value) { - $filesystem - ->expects($this->at($key + 1)) - ->method('callFilesystem') - ->with($function[$key], $args[$key], $errorResultCode[$key]) - ->will($this->returnValue(new FulfilledPromise())) - ; - } - - - $invoker = new QueuedInvoker($filesystem); - $this->assertTrue($invoker->isEmpty()); - foreach ($function as $key => $value) { - $invoker->invokeCall($function[$key], $args[$key], $errorResultCode[$key]); - } - $this->assertFalse($invoker->isEmpty()); - - $loop->run(); - $this->assertTrue($invoker->isEmpty()); - } -} diff --git a/tests/ThrottledQueuedInvokerTest.php b/tests/ThrottledQueuedInvokerTest.php deleted file mode 100644 index 3cdd1eef..00000000 --- a/tests/ThrottledQueuedInvokerTest.php +++ /dev/null @@ -1,71 +0,0 @@ -mockAdapter($loop); - - foreach ($function as $key => $value) { - $filesystem - ->expects($this->at($key + 1)) - ->method('callFilesystem') - ->with($function[$key], $args[$key], $errorResultCode[$key]) - ->will($this->returnValue(new FulfilledPromise())) - ; - } - - $invoker = new ThrottledQueuedInvoker($filesystem); - $this->assertTrue($invoker->isEmpty()); - foreach ($function as $key => $value) { - $invoker->invokeCall($function[$key], $args[$key], $errorResultCode[$key]); - } - $this->assertFalse($invoker->isEmpty()); - - $loop->run(); - $this->assertTrue($invoker->isEmpty()); - } - - public function testInterval() - { - $invoker = new ThrottledQueuedInvoker($this->mockAdapter(Factory::create())); - $this->assertSame(ThrottledQueuedInvoker::DEFAULT_INTERVAL, $invoker->getInterval()); - $invoker = new ThrottledQueuedInvoker($this->mockAdapter(Factory::create()), 1.3); - $this->assertSame(1.3, $invoker->getInterval()); - $invoker->setInterval(3.2); - $this->assertSame(3.2, $invoker->getInterval()); - } -}