/** * {@inheritdoc} */ public function attach(MessageHandler $handler) { $this->server->on('request', function (Request $request, Response $response) use($handler) { $data = (object) array('message' => ''); $request->on('data', function ($chunk) use($data) { $data->message .= $chunk; }); $request->on('end', function () use($data, $handler, $response) { try { $handler->receive($data->message); $ack = new JobAcknowledgement(); $ack->setCreatedOn(new \DateTime()); } catch (RuntimeException $e) { $ack = new JobNotAcknowledgement(); $ack->setCreatedOn(new \DateTime()); $ack->setReason($e->getMessage()); } $json = $ack->toJson(); $response->writeHead(200, array('Content-type' => 'application/json; charset=utf-8', 'Content-length' => strlen($json))); $response->write($json); $response->end(); }); $request->on('error', function ($error) use($handler) { $handler->error($error); }); }); }
public function getData() { $ack = new JobAcknowledgement(); $nack = new JobNotAcknowledgement(); $nack->setReason('Man machine'); return array(array($ack, $ack->toJson()), array($nack, $nack->toJson())); }
/** @test */ public function itMustBeSerializable() { $ack = new JobAcknowledgement(); $date = new \DateTime('+5days'); $ack->setCreatedOn($date); $data = json_decode($ack->toJson(), true); $this->assertEquals($date->format(DATE_ATOM), $data['createdOn']); }
/** @test */ public function itShouldSendMessageAndReceiveAcknowledgement() { $transport = 'ipc'; $host = 'local.gloubster'; $port = '1234'; $context = $this->getContext(); $socket = $this->getSocket(); $context->expects($this->once())->method('getSocket')->with($this->equalTo(\ZMQ::SOCKET_REQ), $this->anything())->will($this->returnValue($socket)); $job = $this->getMockBuilder('Gloubster\\Message\\Job\\ImageJob')->disableOriginalConstructor()->getMock(); $ack = new JobAcknowledgement(); $json = '{"hello": "world !"}'; $socket->expects($this->once())->method('send')->with($json); $socket->expects($this->once())->method('recv')->will($this->returnValue($ack->toJson())); $job->expects($this->any())->method('toJson')->will($this->returnValue($json)); $client = new ZMQClient($context, $transport, $host, $port); $this->assertEquals($ack, $client->send($job)); }
/** * {@inheritdoc} */ public function attach(MessageHandler $handler) { $pull = $this->pull; $this->pull->on('error', function ($error) use($handler) { $handler->error($error); }); $this->pull->on('message', function ($message) use($pull, $handler) { try { $handler->receive($message); $ack = new JobAcknowledgement(); $ack->setCreatedOn(new \DateTime()); } catch (RuntimeException $e) { $ack = new JobNotAcknowledgement(); $ack->setCreatedOn(new \DateTime()); $ack->setReason($e->getMessage()); } $pull->send($ack->toJson()); }); }