Skip to content

Commit

Permalink
fix ack payload rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
chazzbg authored and nekufa committed Jul 4, 2024
1 parent 7aeaf92 commit 206d522
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 13 deletions.
7 changes: 2 additions & 5 deletions src/Message/Ack.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
class Ack extends Prototype
{
public string $subject;
public string $command = '+ACK';

public ?Payload $payload = null;

public function render(): string
{
$payload = ($this->payload ?: Payload::parse(''))->render();
return "PUB $this->subject $this->command $payload";
$payload = Payload::parse('')->render();
return "PUB $this->subject $payload";
}
}
10 changes: 3 additions & 7 deletions src/Message/Msg.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ public function getClient(): ?Client

public function nack(float $delay = 0): void
{
$this->reply(new Ack([
'command' => '-NAK',
$this->reply(new Nak([
'subject' => $this->replyTo,
'payload' => Payload::parse([
'delay' => $delay,
]),
'delay' => $delay,
]));
}

Expand Down Expand Up @@ -125,8 +122,7 @@ public function parse($payload): self

public function progress(): void
{
$this->reply(new Ack([
'command' => '+WPI',
$this->reply(new Progress([
'subject' => $this->replyTo,
]));
}
Expand Down
21 changes: 21 additions & 0 deletions src/Message/Nak.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Basis\Nats\Message;

class Nak extends Prototype
{
public string $subject;
public float $delay;

public function render(): string
{
$data = ['-NAK'];
if (isset($this->delay) && $this->delay > 0) {
$data[] = json_encode(['delay' => $this->delay * 10 ** 9]);
}
$payload = Payload::parse(implode(' ', $data))->render();
return "PUB $this->subject $payload";
}
}
16 changes: 16 additions & 0 deletions src/Message/Progress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Basis\Nats\Message;

class Progress extends Prototype
{
public string $subject;

public function render(): string
{
$payload = Payload::parse('+WPI')->render();
return "PUB $this->subject $payload";
}
}
2 changes: 1 addition & 1 deletion tests/Functional/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testNack()
$message = $queue->fetch();
$this->assertNotNull($message);
$this->assertSame((string) $message->payload, 'first');
$message->nack(1);
$message->nack(30);

$this->assertSame(1, $consumer->info()->num_ack_pending);
$this->assertSame(1, $consumer->info()->num_pending);
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Message/AckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Unit\Message;

use Basis\Nats\Message\Ack;
use Tests\TestCase;

class AckTest extends TestCase
{
public function testAck()
{
$ack = new Ack([
'subject' => '$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0'
]);

$this->assertEquals("PUB \$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0 0\r\n", $ack->render());
}

}
47 changes: 47 additions & 0 deletions tests/Unit/Message/NakTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Tests\Unit\Message;

use Basis\Nats\Message\Nak;
use Tests\TestCase;

class NakTest extends TestCase
{
public function testNak()
{
$nak = new Nak([
'subject' => '$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0'
]);

$this->assertEquals("PUB \$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0 4\r\n-NAK", $nak->render());
}

public function testNakDelay()
{
$nak = new Nak([
'subject' => '$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0',
'delay' => 10
]);

$this->assertEquals("PUB \$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0 26\r\n-NAK {\"delay\":10000000000}", $nak->render());
}

public function testNakFloatDelay()
{
$nak = new Nak([
'subject' => '$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0',
'delay' => 1.1
]);

$this->assertEquals("PUB \$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0 25\r\n-NAK {\"delay\":1100000000}", $nak->render());
}
public function testNakZeroDelay()
{
$nak = new Nak([
'subject' => '$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0',
'delay' => 0
]);

$this->assertEquals("PUB \$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0 4\r\n-NAK", $nak->render());
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Message/ProgressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Unit\Message;

use Basis\Nats\Message\Progress;
use Tests\TestCase;

class ProgressTest extends TestCase
{
public function testProgress()
{
$progress = new Progress([
'subject' => '$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0'
]);

$this->assertEquals("PUB \$JS.ACK.stream.consumer.1.3.18.1719992702186105579.0 4\r\n+WPI", $progress->render());
}

}

0 comments on commit 206d522

Please sign in to comment.