Skip to content

Commit c32bc4b

Browse files
committed
WIP
1 parent 0f6c071 commit c32bc4b

File tree

6 files changed

+108
-65
lines changed

6 files changed

+108
-65
lines changed

examples/directory_ls.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
$filesystem = \React\Filesystem\Filesystem::create($loop);
88
echo 'Using ', get_class($filesystem->getAdapter()), PHP_EOL;
9-
$filesystem->dir(__DIR__ . DIRECTORY_SEPARATOR . 'tmp')->ls()->then(function (array $list) {
9+
$filesystem->dir(__DIR__)->ls()->then(function (array $list) {
1010
foreach ($list as $node) {
1111
echo get_class($node), ': ', $node->getPath(), PHP_EOL;
1212
}
13-
}, function ($e) {
14-
echo $e->getMessage(), PHP_EOL;
13+
})->then(null, function ($error) {
14+
echo (string)$error;
1515
});
1616

1717
$loop->run();

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
convertErrorsToExceptions="true"
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
9-
processIsolation="false"
9+
processIsolation="true"
1010
stopOnFailure="false"
1111
syntaxCheck="false"
1212
bootstrap="tests/bootstrap.php"

src/Eio/Adapter.php

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use React\Filesystem\PermissionFlagResolver;
1515
use React\Filesystem\TypeDetectorInterface;
1616
use React\Promise\Deferred;
17+
use React\Promise\Promise;
1718
use React\Promise\PromiseInterface;
1819

1920
class Adapter implements AdapterInterface
@@ -181,6 +182,10 @@ public function lsStream($path)
181182

182183
$this->callFilesystem('eio_readdir', [$path, $this->options['lsFlags']], false)->then(function ($result) use ($path, $stream) {
183184
$this->processLsContents($path, $result, $stream);
185+
}, function ($error) use ($stream) {
186+
var_export($error);
187+
$stream->emit('error', [$error]);
188+
$stream->close();
184189
});
185190

186191
return $stream;
@@ -208,11 +213,18 @@ protected function processLsContents($basePath, $result, ObjectStream $stream)
208213
];
209214
$promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use ($stream) {
210215
$stream->write($node);
216+
217+
return \React\Promise\resolve(true);
218+
}, function ($error) {
219+
echo (string)$error;
220+
return \React\Promise\resolve(true);
211221
});
212222
}
213223

214224
\React\Promise\all($promises)->then(function () use ($stream) {
215225
$stream->close();
226+
}, function ($error) use ($stream) {
227+
$stream->close();
216228
});
217229
}
218230

@@ -355,46 +367,60 @@ public function detectType($path)
355367
*/
356368
public function callFilesystem($function, $args, $errorResultCode = -1)
357369
{
358-
$deferred = new Deferred();
370+
return new Promise(function ($resolve, $reject) use ($function, $args, $errorResultCode) {
371+
if ($this->loopRunning) {
372+
try {
373+
$resolve($this->executeDelayedCall($function, $args, $errorResultCode));
374+
} catch (\Exception $exception) {
375+
$reject($exception);
376+
} catch (\Throwable $exception) {
377+
$reject($exception);
378+
}
359379

360-
if ($this->loopRunning) {
361-
$this->executeDelayedCall($function, $args, $errorResultCode, $deferred);
362-
return $deferred->promise();
363-
}
380+
return;
381+
}
364382

365-
// Run this in a future tick to make sure all EIO calls are run within the loop
366-
$this->loop->futureTick(function () use ($function, $args, $errorResultCode, $deferred) {
367-
$this->loopRunning = true;
368-
$this->executeDelayedCall($function, $args, $errorResultCode, $deferred);
369-
});
370383

371-
return $deferred->promise();
384+
// Run this in a future tick to make sure all EIO calls are run within the loop
385+
$this->loop->futureTick(function () use ($function, $args, $errorResultCode, $resolve, $reject) {
386+
try {
387+
$resolve($this->executeDelayedCall($function, $args, $errorResultCode));
388+
} catch (\Exception $exception) {
389+
$reject($exception);
390+
} catch (\Throwable $exception) {
391+
$reject($exception);
392+
}
393+
});
394+
395+
});
372396
}
373397

374-
protected function executeDelayedCall($function, $args, $errorResultCode, Deferred $deferred)
398+
protected function executeDelayedCall($function, $args, $errorResultCode)
375399
{
376400
$this->register();
377-
$args[] = EIO_PRI_DEFAULT;
378-
$args[] = function ($data, $result, $req) use ($deferred, $errorResultCode, $function, $args) {
379-
if ($result == $errorResultCode) {
380-
$exception = new UnexpectedValueException(@eio_get_last_error($req));
401+
return new Promise(function ($resolve, $reject) use ($function, $args, $errorResultCode) {
402+
$args[] = EIO_PRI_DEFAULT;
403+
$args[] = function ($data, $result, $req) use ($resolve, $reject, $errorResultCode, $function, $args) {
404+
if ($result == $errorResultCode) {
405+
$exception = new UnexpectedValueException(@eio_get_last_error($req));
406+
$exception->setArgs($args);
407+
$reject($exception);
408+
return;
409+
}
410+
411+
$resolve($result);
412+
};
413+
414+
if (!call_user_func_array($function, $args)) {
415+
$name = $function;
416+
if (!is_string($function)) {
417+
$name = get_class($function);
418+
}
419+
$exception = new RuntimeException('Unknown error calling "' . $name . '"');
381420
$exception->setArgs($args);
382-
$deferred->reject($exception);
383-
return;
384-
}
385-
386-
$deferred->resolve($result);
387-
};
388-
389-
if (!call_user_func_array($function, $args)) {
390-
$name = $function;
391-
if (!is_string($function)) {
392-
$name = get_class($function);
393-
}
394-
$exception = new RuntimeException('Unknown error calling "' . $name . '"');
395-
$exception->setArgs($args);
396-
$deferred->reject($exception);
397-
};
421+
$reject($exception);
422+
};
423+
});
398424
}
399425

400426
protected function register()

tests/Adapters/AbstractAdaptersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public function adapterProvider()
5555

5656
protected function adapterFactory(&$adapters, $loopSlug, callable $loopFactory)
5757
{
58-
$adapters[$loopSlug . '-factory'] = $this->getFacoryProvider($loopFactory);
59-
$adapters[$loopSlug . '-child-process'] = $this->getChildProcessProvider($loopFactory);
58+
// $adapters[$loopSlug . '-factory'] = $this->getFacoryProvider($loopFactory);
59+
// $adapters[$loopSlug . '-child-process'] = $this->getChildProcessProvider($loopFactory);
6060

6161
if (extension_loaded('eio')) {
6262
$adapters[$loopSlug . '-eio'] = $this->getEioProvider($loopFactory);

tests/Eio/AdapterTest.php

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use React\Filesystem\Eio\Adapter;
88
use React\Promise\FulfilledPromise;
99
use React\Promise\RejectedPromise;
10-
use React\Tests\Filesystem\CallInvokerProvider;
1110
use React\Tests\Filesystem\TestCase;
1211

1312
/**
@@ -247,11 +246,8 @@ public function testHandleEventNothingToDo()
247246
$filesystem->handleEvent();
248247
}
249248

250-
public function callInvokerProvider()
249+
public function testTouchExists()
251250
{
252-
if (!extension_loaded('eio')) {
253-
return null;
254-
}
255251
$loop = Factory::create();
256252
$adapter = $this->getMock('React\Filesystem\Eio\Adapter', [
257253
'getLoop',
@@ -265,14 +261,6 @@ public function callInvokerProvider()
265261
->will($this->returnValue($loop))
266262
;
267263

268-
return (new CallInvokerProvider())->callInvokerProvider($loop, $adapter);
269-
}
270-
271-
/**
272-
* @dataProvider callInvokerProvider
273-
*/
274-
public function testTouchExists($loop, $adapter, $invoker)
275-
{
276264
$filename = 'foo.bar';
277265
$fd = '01010100100010011110101';
278266

@@ -316,11 +304,21 @@ public function testTouchExists($loop, $adapter, $invoker)
316304
$loop->run();
317305
}
318306

319-
/**
320-
* @dataProvider callInvokerProvider
321-
*/
322-
public function testTouchExistsNoTime($loop, $adapter, $invoker)
307+
public function testTouchExistsNoTime()
323308
{
309+
$loop = Factory::create();
310+
$adapter = $this->getMock('React\Filesystem\Eio\Adapter', [
311+
'getLoop',
312+
], [
313+
$loop,
314+
]);
315+
316+
$adapter
317+
->expects($this->any())
318+
->method('getLoop')
319+
->will($this->returnValue($loop))
320+
;
321+
324322
$filename = 'foo.bar';
325323
$fd = '01010100100010011110101';
326324

@@ -360,11 +358,21 @@ public function testTouchExistsNoTime($loop, $adapter, $invoker)
360358
$loop->run();
361359
}
362360

363-
/**
364-
* @dataProvider callInvokerProvider
365-
*/
366-
public function testTouchCreate($loop, $adapter, $invoker)
361+
public function testTouchCreate()
367362
{
363+
$loop = Factory::create();
364+
$adapter = $this->getMock('React\Filesystem\Eio\Adapter', [
365+
'getLoop',
366+
], [
367+
$loop,
368+
]);
369+
370+
$adapter
371+
->expects($this->any())
372+
->method('getLoop')
373+
->will($this->returnValue($loop))
374+
;
375+
368376
$filename = 'foo.bar';
369377
$fd = '01010100100010011110101';
370378

@@ -400,11 +408,21 @@ public function testTouchCreate($loop, $adapter, $invoker)
400408
$loop->run();
401409
}
402410

403-
/**
404-
* @dataProvider callInvokerProvider
405-
*/
406-
public function _testOpen($loop, $adapter, $invoker)
411+
public function _testOpen()
407412
{
413+
$loop = Factory::create();
414+
$adapter = $this->getMock('React\Filesystem\Eio\Adapter', [
415+
'getLoop',
416+
], [
417+
$loop,
418+
]);
419+
420+
$adapter
421+
->expects($this->any())
422+
->method('getLoop')
423+
->will($this->returnValue($loop))
424+
;
425+
408426
$filename = 'foo.bar';
409427
$fd = '01010100100010011110101';
410428

tests/TestCase.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class TestCase extends PHPUnitTestCase
1111
{
12-
const TIMEOUT = 30;
12+
const TIMEOUT = 5;
1313

1414
protected $tmpDir;
1515

@@ -25,7 +25,6 @@ protected function mockAdapter(LoopInterface $loop = null)
2525
'__construct',
2626
'getLoop',
2727
'setFilesystem',
28-
'setInvoker',
2928
'callFilesystem',
3029
'isSupported',
3130
'mkdir',

0 commit comments

Comments
 (0)