/** * Message producers need to be invokable. * * A producer MUST be able to handle a message async without returning a response. * A producer MAY also support future response by resolving the passed $deferred. * * Note: A $deferred is only passed by a QueryBus but in this case the $deferred * MUST either be resolved/rejected OR the message producer * MUST throw a Prooph\ServiceBus\Exception\RuntimeException if it cannot * handle the $deferred * * @param Message $message * @param null|Deferred $deferred * @throws RuntimeException If a $deferred is passed but producer can not handle it */ public function __invoke(Message $message, Deferred $deferred = null) { $this->assertDeferred(null !== $deferred); $data = $this->arrayFromMessage($message); $this->zmqClient->send(json_encode($data), ZMQ::MODE_DONTWAIT); if ($deferred) { $response = $this->zmqClient->receive(); $deferred->resolve($response); } }
/** * @test */ public function it_can_handle_rpc() { $this->zmqClient->handlesDeferred()->willReturn(true)->shouldBeCalled(); $this->zmqClient->receive()->willReturn($response = 'Hello World')->shouldBeCalled(); $deferred = $this->prophesize(Deferred::class); $deferred->resolve($response)->shouldBeCalled(); $zmqMessageProducer = $this->zmqMessageProducer; $doSomething = new DoSomething(['data' => 'test command']); $this->zmqClient->send($this->validate_message_body($doSomething), \ZMQ::MODE_NOBLOCK)->willReturn(null)->shouldBeCalled(); $zmqMessageProducer($doSomething, $deferred->reveal()); }
/** * @test */ public function it_sends_message_and_gets_response() { $socket = new \ZMQSocket(new \ZMQContext(), \ZMQ::SOCKET_REQ); $zmqClient = new ZeroMQSocket($socket, 'tcp://localhost:5556'); $zmqClient->send($message = 'testing-123'); $this->assertEquals($message, $zmqClient->receive()); $socket->disconnect('tcp://localhost:5556'); }