public function testWaterfallWithTasks() { $loop = new \React\EventLoop\StreamSelectLoop(); $tasks = array(function ($callback, $errback) use($loop) { $loop->addTimer(0.05, function () use($callback) { $callback('foo'); }); }, function ($foo, $callback, $errback) use($loop) { $loop->addTimer(0.05, function () use($callback, $foo) { $callback($foo . 'bar'); }); }, function ($bar, $callback, $errback) use($loop) { $loop->addTimer(0.05, function () use($callback, $bar) { $callback($bar . 'baz'); }); }); $callback = $this->createCallableMock($this->once(), 'foobarbaz'); $errback = $this->createCallableMock($this->never()); Util::waterfall($tasks, $callback, $errback); $timer = new Timer($this); $timer->start(); $loop->run(); $timer->stop(); $timer->assertInRange(0.15, 0.3); }
public function testParallelWithDelayedError() { $called = 0; $loop = new \React\EventLoop\StreamSelectLoop(); $tasks = array(function ($callback, $errback) use(&$called) { $callback('foo'); $called++; }, function ($callback, $errback) use($loop) { $loop->addTimer(0.001, function () use($errback) { $e = new \RuntimeException('whoops'); $errback($e); }); }, function ($callback, $errback) use(&$called) { $callback('bar'); $called++; }); $callback = $this->createCallableMock($this->never()); $errback = $this->createCallableMock($this->once()); Util::parallel($tasks, $callback, $errback); $loop->run(); $this->assertSame(2, $called); }
$t = isset($args['t']) ? $args['t'] : 1; // passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465) $if = str_replace('/dev/fd/', 'php://fd/', $if); $of = str_replace('/dev/fd/', 'php://fd/', $of); $loop = new React\EventLoop\StreamSelectLoop(); // setup information stream $info = new React\Stream\Stream(STDERR, $loop); $info->pause(); if (extension_loaded('xdebug')) { $info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL); } $info->write('piping from ' . $if . ' to ' . $of . ' (for max ' . $t . ' second(s)) ...' . PHP_EOL); // setup input and output streams and pipe inbetween $in = new React\Stream\Stream(fopen($if, 'r'), $loop); $out = new React\Stream\Stream(fopen($of, 'w'), $loop); $out->pause(); $in->pipe($out); // stop input stream in $t seconds $start = microtime(true); $timeout = $loop->addTimer($t, function () use($in, &$bytes) { $in->close(); }); // print stream position once stream closes $in->on('close', function () use($in, $start, $timeout, $info) { $t = microtime(true) - $start; $timeout->cancel(); $bytes = ftell($in->stream); $info->write('read ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL); $info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL); }); $loop->run();