public function testOverflow()
 {
     $memory = new Memory();
     $queue = Phake::mock('WyriHaximus\\React\\ChildProcess\\Pool\\QueueInterface');
     Phake::when($queue)->count()->thenReturnCallback(function () use($memory) {
         return $memory->count();
     });
     Phake::when($queue)->dequeue()->thenReturnCallback(function () use($memory) {
         return new FulfilledPromise($memory->dequeue());
     });
     Phake::when($queue)->enqueue($this->isInstanceOf('WyriHaximus\\React\\ChildProcess\\Messenger\\Messages\\Rpc'))->thenReturnCallback(function (Rpc $rpc) use($memory) {
         return new FulfilledPromise($memory->enqueue($rpc));
     });
     $overflow = new Overflow($queue, 1);
     $rpc0 = Factory::rpc('a', ['b']);
     $rpc1 = Factory::rpc('c', ['d']);
     $this->assertSame(0, $memory->count());
     $this->assertSame(0, $overflow->count());
     $overflow->enqueue($rpc0);
     $this->assertSame(0, $memory->count());
     $this->assertSame(1, $overflow->count());
     $overflow->enqueue($rpc1);
     $this->assertSame(1, $memory->count());
     $this->assertSame(2, $overflow->count());
     $this->assertSame(json_encode($rpc0), json_encode($this->dequeue($overflow->dequeue())));
     $this->assertSame(0, $memory->count());
     $this->assertSame(1, $overflow->count());
     $this->assertSame(json_encode($rpc1), json_encode($this->dequeue($overflow->dequeue())));
     $this->assertSame(0, $memory->count());
     $this->assertSame(0, $overflow->count());
 }
 public function testMessage()
 {
     $message = Factory::message(['t']);
     $messenger = Phake::mock('WyriHaximus\\React\\ChildProcess\\Messenger\\Messenger');
     $worker = new Worker($messenger);
     $worker->message($message);
     Phake::verify($messenger)->message($message);
 }
 /**
  * @param LoopInterface $loop
  * @param Messenger $messenger
  * @return Process
  */
 public static function create(LoopInterface $loop, Messenger $messenger)
 {
     try {
         return new Process($loop, $messenger);
     } catch (\Exception $exeption) {
         $messenger->error(MessagesFactory::error(['message' => $exeption->getMessage(), 'code' => $exeption->getCode(), 'line' => $exeption->getLine(), 'file' => $exeption->getFile()]));
         $loop->addTimer(1, function () use($loop) {
             $loop->stop();
         });
     }
 }
 public function testMethods()
 {
     $loop = Phake::mock('React\\EventLoop\\LoopInterface');
     $poolPromise = Dummy::createFromClass('stdClass', $loop);
     $promiseHasResolved = false;
     $poolPromise->then(function ($pool) use(&$promiseHasResolved) {
         $pool->message(Factory::message([]));
         $pool->terminate(Factory::message([]));
         $this->assertInstanceOf('React\\Promise\\FulfilledPromise', $pool->rpc(Factory::rpc('abc', ['def'])));
         $this->assertSame([], $pool->info());
         $promiseHasResolved = true;
     });
     $this->assertTrue($promiseHasResolved);
 }
 /**
  * @param string $className
  * @param LoopInterface $loop
  * @param array $options
  * @param float $interval
  * @return \React\Promise\PromiseInterface
  * @throws \Exception
  */
 public static function parentFromClass($className, LoopInterface $loop, array $options = [], $interval = self::INTERVAL)
 {
     if (!is_subclass_of($className, 'WyriHaximus\\React\\ChildProcess\\Messenger\\ChildInterface')) {
         throw new \Exception('Given class doesn\'t implement ChildInterface');
     }
     $template = '%s';
     if (isset($options['cmdTemplate'])) {
         $template = $options['cmdTemplate'];
         unset($options['cmdTemplate']);
     }
     $process = new Process(sprintf($template, self::getProcessForCurrentOS() . ' ' . ArgvEncoder::encode($options)));
     return static::parent($process, $loop, $options, $interval)->then(function (Messenger $messenger) use($className) {
         return $messenger->rpc(MessengesFactory::rpc(Factory::PROCESS_REGISTER, ['className' => $className]))->then(function () use($messenger) {
             return \React\Promise\resolve($messenger);
         });
     });
 }
 /**
  * @param $bindTo
  * @param $source
  */
 public function handle($bindTo, $source)
 {
     $cb = function ($target, $payload, $uniqid) {
         if (!$this->hasRpc($target)) {
             $this->getStderr()->write($this->createLine(Factory::rpcError($uniqid, ['message' => 'Target doesn\'t exist'])));
             return;
         }
         $this->callRpc($target, $payload)->then(function (array $payload) use($uniqid) {
             $this->getStdout()->write($this->createLine(Factory::rpcSuccess($uniqid, $payload)));
         }, function ($error) use($uniqid) {
             $this->getStderr()->write($this->createLine(Factory::rpcError($uniqid, ['error' => $error])));
         }, function (array $payload) use($uniqid) {
             $this->getStdout()->write($this->createLine(Factory::rpcNotify($uniqid, $payload)));
         });
     };
     $cb = $cb->bindTo($bindTo);
     $cb($this->target, $this->payload, $this->uniqid);
 }
 /**
  * @dataProvider queueProvider
  */
 public function testOperations(QueueInterface $queue)
 {
     $rpc0 = Factory::rpc('a', ['b']);
     $rpc1 = Factory::rpc('c', ['d']);
     $rpc2 = Factory::rpc('e', ['f']);
     $this->assertSame(0, $queue->count());
     $queue->enqueue($rpc0);
     $this->assertSame(1, $queue->count());
     $queue->enqueue($rpc1);
     $this->assertSame(2, $queue->count());
     $queue->enqueue($rpc2);
     $this->assertSame(3, $queue->count());
     $this->assertSame(json_encode($rpc0), json_encode($this->dequeue($queue->dequeue())));
     $this->assertSame(2, $queue->count());
     $this->assertSame(json_encode($rpc1), json_encode($this->dequeue($queue->dequeue())));
     $this->assertSame(1, $queue->count());
     $this->assertSame(json_encode($rpc2), json_encode($this->dequeue($queue->dequeue())));
     $this->assertSame(0, $queue->count());
 }
 public function testManagerReady()
 {
     $function = null;
     $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');
     Phake::when($queue)->count()->thenReturn(4);
     Phake::when($queue)->dequeue()->thenReturn($message);
     Phake::when($manager)->on($this->isType('string'), $this->isType('callable'))->thenReturnCallback(function ($event, $passedFunction) use(&$function) {
         $function = $passedFunction;
     });
     $poolInstance = null;
     $process = Phake::mock('React\\ChildProcess\\Process');
     $loop = Phake::mock('React\\EventLoop\\LoopInterface');
     Fixed::create($process, $loop, [Options::MANAGER => $manager, Options::QUEUE => $queue])->then(function ($pool) use($message) {
         $pool->rpc($message);
     });
     $function($worker);
     Phake::verify($worker)->rpc($message);
 }
 /**
  * @param string $function
  * @param array $args
  * @param int $errorResultCode
  * @return \React\Promise\Promise
  */
 public function callFilesystem($function, $args, $errorResultCode = -1)
 {
     return $this->pool->rpc(Factory::rpc($function, $args))->then(function (Payload $payload) {
         return \React\Promise\resolve($payload->getPayload());
     });
 }
 /**
  * @return \React\Promise\Promise
  */
 public function softTerminate()
 {
     return $this->rpc(MessageFactory::rpc(static::TERMINATE_RPC));
 }
 public function testMessage()
 {
     $message = Factory::message(['bar']);
     $workerDeferred = new Deferred();
     $worker = null;
     $messenger = Phake::mock('WyriHaximus\\React\\ChildProcess\\Messenger\\Messenger');
     Phake::when($this->processCollection)->current()->thenReturnCallback(function () use($workerDeferred) {
         return function () use($workerDeferred) {
             return $workerDeferred->promise();
         };
     });
     $this->createManager();
     $this->manager->once('ready', function (WorkerInterface $workerInstance) use(&$worker) {
         $worker = $workerInstance;
     });
     $workerDeferred->resolve($messenger);
     $this->manager->message($message);
     Phake::verify($messenger)->message($message);
 }
    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');
 /**
  * Modifies a term's karma.
  *
  * @param string $term   Term to modify
  * @param string $action Karma action (either ++ or --)
  * @param \Phergie\Irc\Event\UserEvent $event
  * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue
  * @return bool|\React\Promise\Promise
  */
 protected function modifyKarma($term, $action, UserEvent $event, Queue $queue)
 {
     $nick = $event->getNick();
     $canonicalTerm = $this->getCanonicalTerm($term, $nick);
     if ($canonicalTerm == strtolower($nick)) {
         $message = 'You can\'t give yourself karma.';
         $queue->ircPrivmsg($event->getSource(), $message);
         return false;
     }
     $karma = 0;
     return $this->messenger->rpc(MessageFactory::rpc('fetchKarma', ['term' => $canonicalTerm]))->then(function ($payload) use($event, $queue, $action, $term, $canonicalTerm, &$karma) {
         $this->logDebug('payload: ' . var_export($payload, true));
         $karma = $payload['karma'] + ($action == '++' ? 1 : -1);
         return MessageFactory::rpc('modifyKarma', ['term' => $canonicalTerm, 'karma' => $karma]);
     })->then([$this->messenger, 'rpc'])->then(function ($payload) use($event, $queue, $action, $term) {
         $this->logDebug('payload: ' . var_export($payload, true));
         $queue->ircPrivmsg($event->getSource(), $this->getUserMessage('karma' . $action, $term));
         return $payload['karma'];
     });
 }
 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();
 }