From 2eaf2fd4de1e0fcb865ee36f919509a9401d5e29 Mon Sep 17 00:00:00 2001 From: Christoffer Lindahl Date: Fri, 8 Dec 2023 14:33:04 +0100 Subject: [PATCH] Configure KV streams the same way as the Nats GO lib (history = 1 and DiscardPolicy = NEW) Fixes #37 --- src/KeyValue/Configuration.php | 4 +++- tests/Functional/KeyValue/BucketTest.php | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/KeyValue/Configuration.php b/src/KeyValue/Configuration.php index 444d842..cb99f3b 100644 --- a/src/KeyValue/Configuration.php +++ b/src/KeyValue/Configuration.php @@ -5,6 +5,7 @@ namespace Basis\Nats\KeyValue; use Basis\Nats\Stream\Configuration as StreamConfiguration; +use Basis\Nats\Stream\DiscardPolicy; class Configuration { @@ -22,11 +23,12 @@ public function configureStream(StreamConfiguration $configuration): self { $configuration ->setAllowRollupHeaders(true) + ->setDiscardPolicy(DiscardPolicy::NEW) ->setDenyDelete(false) ->setMaxAge($this->getTtl() ?? 0) ->setMaxBytes($this->getMaxBytes()) ->setMaxMessageSize($this->getMaxValueSize()) - ->setMaxMessagesPerSubject($this->getHistory()) + ->setMaxMessagesPerSubject($this->getHistory() ?? 1) ->setReplicas($this->getReplicas() ?? 1) ->setSubjects([strtoupper("\$kv.$this->name.*")]); diff --git a/tests/Functional/KeyValue/BucketTest.php b/tests/Functional/KeyValue/BucketTest.php index 1575f9e..bed783f 100644 --- a/tests/Functional/KeyValue/BucketTest.php +++ b/tests/Functional/KeyValue/BucketTest.php @@ -28,13 +28,13 @@ public function testBasics() $bucket->update('username', 'bazyaba', $bucket->getEntry('username')->revision); $this->assertSame($bucket->get('username'), 'bazyaba'); - $this->assertSame(2, $bucket->getStatus()->values); + $this->assertSame(1, $bucket->getStatus()->values); $bucket->delete('username'); // username null value in history $this->assertSame($bucket->get('username'), null); - $this->assertSame(3, $bucket->getStatus()->values); + $this->assertSame(1, $bucket->getStatus()->values); // purge key logs $bucket->purge('username');