/**
  * Adapter constructor.
  * @param LoopInterface $loop
  * @param array $options
  */
 public function __construct(LoopInterface $loop, array $options = [], FlysystemInterface $flysystem = null)
 {
     $this->loop = $loop;
     $this->invoker = \React\Filesystem\getInvoker($this, $options, 'invoker', 'React\\Filesystem\\InstantInvoker');
     $this->openFileLimiter = new OpenFileLimiter(\React\Filesystem\getOpenFileLimit($options));
     Flexible::createFromClass(Worker::class, $loop, ['min_size' => 0, 'max_size' => 50])->then(function (PoolInterface $pool) {
         $this->pool = $pool;
     });
     Util::forwardEvents($this->pool, $this, ['error']);
     $this->pool->on('worker', function (WorkerInterface $worker) {
         $worker->rpc(Factory::rpc('setFlysystem', ['flysystem' => serialize($this->flysystem)]));
     });
     $this->options = array_merge_recursive($this->options, $options);
     $this->flysystem = $flysystem;
 }
    echo 'Peak memory usage: ', memory_get_peak_usage() / MB, 'MB', PHP_EOL;
    echo 'Current real memory usage: ', memory_get_usage(true) / MB, 'MB', PHP_EOL;
    echo 'Peak real memory usage: ', memory_get_peak_usage(true) / MB, 'MB', PHP_EOL;
}
show_memory('Bare init');
require dirname(__DIR__) . '/vendor/autoload.php';
use React\EventLoop\Factory as EventLoopFactory;
use React\EventLoop\Timer\Timer;
use WyriHaximus\React\ChildProcess\Messenger\Factory as MessengerFactory;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory as MessagesFactory;
use WyriHaximus\React\ChildProcess\Messenger\Messenger;
use WyriHaximus\React\ChildProcess\Pool\Factory\Flexible;
use WyriHaximus\React\ChildProcess\Pool\PoolInfoInterface;
use WyriHaximus\React\ChildProcess\Pool\PoolInterface;
show_memory('Begin');
$loop = EventLoopFactory::create();
Flexible::createFromClass('WyriHaximus\\React\\ChildProcess\\Messenger\\ReturnChild', $loop)->then(function (PoolInterface $messenger) use($loop) {
    $messenger->on('error', function ($e) {
        echo 'Error: ', var_export($e, true), PHP_EOL;
    });
    for ($i = 0; $i <= I; $i++) {
        $messenger->rpc(MessagesFactory::rpc('return', ['i' => $i, 'time' => time()]));
    }
});
$loop->run();
show_memory('Done');
unset($loop);
$loop = null;
show_memory('Removed loop');
gc_collect_cycles();
show_memory('gc_collect_cycles');
 /**
  * @expectedException \Exception
  * @expectedExceptionMessage Given class doesn't implement ChildInterface
  */
 public function testCreateFromClass()
 {
     $loop = Phake::mock('React\\EventLoop\\LoopInterface');
     Flexible::createFromClass('stdClass', $loop, [Options::MIN_SIZE => 1, Options::MAX_SIZE => 1]);
 }
 public function testManagerReadyQueueEmptyIsBusy()
 {
     $message = Factory::rpc('beer', ['foo' => 'bar']);
     $worker = Phake::mock('WyriHaximus\\React\\ChildProcess\\Pool\\WorkerInterface');
     $queue = Phake::mock('WyriHaximus\\React\\ChildProcess\\Pool\\QueueInterface');
     $manager = Phake::mock('WyriHaximus\\React\\ChildProcess\\Pool\\ManagerInterface');
     $timer = Phake::mock('React\\EventLoop\\Timer\\TimerInterface');
     Phake::when($worker)->isBusy()->thenReturn(true);
     Phake::when($queue)->count()->thenReturn(0);
     Phake::when($queue)->dequeue()->thenReturn($message);
     Phake::when($manager)->on($this->isType('string'), $this->isType('callable'))->thenReturnCallback(function ($event, $function) use($worker) {
         $function($worker);
     });
     $poolInstance = null;
     $process = Phake::mock('React\\ChildProcess\\Process');
     $loop = Phake::mock('React\\EventLoop\\LoopInterface');
     Phake::when($loop)->addPeriodicTimer($this->isType('float'), $this->isType('callable'))->thenReturnCallback(function ($interval, $function) use($timer) {
         $function($timer);
     });
     Flexible::create($process, $loop, [Options::MANAGER => $manager, Options::QUEUE => $queue, Options::TTL => 0])->then(function ($pool) use($message) {
         $pool->rpc($message);
     });
     Phake::verify($timer)->cancel();
 }