Skip to content

Commit

Permalink
Added the codec parameter to the worker's respond method
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Mar 29, 2024
2 parents 8efc721 + ee6f54b commit 7c7411a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
"php": ">=8.1",
"ext-json": "*",
"ext-sockets": "*",
"psr/log": "^2.0|^3.0",
"psr/log": "^2.0 || ^3.0",
"spiral/goridge": "^4.1.0",
"spiral/roadrunner": "^2023.1",
"spiral/roadrunner": "^2023.1 || ^2024.1",
"composer-runtime-api": "^2.0"
},
"require-dev": {
Expand Down
15 changes: 12 additions & 3 deletions src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ public function withStreamMode(): static
return $clone;
}

public function respond(Payload $payload): void
/**
* @param int|null $codec The codec used for encoding the payload header.
* Can be {@see Frame::CODEC_PROTO} for Protocol Buffers or {@see Frame::CODEC_JSON} for JSON.
* This parameter will be removed in v4.0 and {@see Frame::CODEC_PROTO} will be used by default.
*/
public function respond(Payload $payload, ?int $codec = null): void
{
$this->streamMode and ++$this->framesSent;
$this->send($payload->body, $payload->header, $payload->eos);
$this->send($payload->body, $payload->header, $payload->eos, $codec);
}

public function error(string $error): void
Expand Down Expand Up @@ -198,7 +203,7 @@ private function pullPayload(): ?Payload
return $payload;
}

private function send(string $body = '', string $header = '', bool $eos = true): void
private function send(string $body = '', string $header = '', bool $eos = true, ?int $codec = null): void
{
$frame = new Frame($header . $body, [\strlen($header)]);

Expand All @@ -210,6 +215,10 @@ private function send(string $body = '', string $header = '', bool $eos = true):
$frame->byte10 |= Frame::BYTE10_PING;
}

if ($codec !== null) {
$frame->setFlag($codec);
}

$this->sendFrame($frame);
}

Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/WorkerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Tests\Worker\Unit;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Spiral\Goridge\Frame;
use Spiral\Goridge\RelayInterface;
use Spiral\RoadRunner\Payload;
use Spiral\RoadRunner\Worker;

final class WorkerTest extends TestCase
{
#[DataProvider('respondDataProvider')]
public function testRespond(int $expectedFlags, ?int $codec): void
{
$expected = new Frame('Hello World!', [0 => 0], $expectedFlags);

$relay = $this->createMock(RelayInterface::class);
$relay
->expects($this->once())
->method('send')
->with($this->equalTo($expected));

$worker = new Worker($relay, false);

$worker->respond(new Payload('Hello World!'), $codec);
}

public static function respondDataProvider(): \Traversable
{
yield [0, null];
yield [Frame::CODEC_PROTO, Frame::CODEC_PROTO];
yield [Frame::CODEC_JSON, Frame::CODEC_JSON];
}
}

0 comments on commit 7c7411a

Please sign in to comment.