From 0690ab94b1e14b2b2cb189d5f4270fe6c433337a Mon Sep 17 00:00:00 2001 From: Daniel Paul Carbone Date: Tue, 15 May 2018 14:41:40 -0500 Subject: [PATCH] correcting KVClient::acquire and release methods --- src/Consul.php | 3 ++ src/KV/KVClient.php | 4 +-- tests/Usage/KV/KVClientLockTest.php | 51 +++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 tests/Usage/KV/KVClientLockTest.php diff --git a/src/Consul.php b/src/Consul.php index 4a3e470..c086b1e 100644 --- a/src/Consul.php +++ b/src/Consul.php @@ -48,6 +48,9 @@ class Consul { const HealthCritical = 'critical'; const HealthMaint = 'maintenance'; + const SessionBehaviorRelease = 'release'; + const SessionBehaviorDelete = 'delete'; + /** @var \DCarbone\PHPConsulAPI\ACL\ACLClient */ public $ACL; /** @var \DCarbone\PHPConsulAPI\Agent\AgentClient */ diff --git a/src/KV/KVClient.php b/src/KV/KVClient.php index efbafcb..edab86b 100644 --- a/src/KV/KVClient.php +++ b/src/KV/KVClient.php @@ -243,7 +243,7 @@ public function cas(KVPair $p, WriteOptions $options = null) { * ) */ public function acquire(KVPair $p, WriteOptions $options = null) { - $r = new Request('PUT', sprintf('v1/kv/%s', $p->Key), $this->config); + $r = new Request('PUT', sprintf('v1/kv/%s', $p->Key), $this->config, $p->Value); $r->setWriteOptions($options); $r->Params->set('acquire', $p->Session); if (0 !== $p->Flags) { @@ -288,7 +288,7 @@ public function deleteCAS(KVPair $p, WriteOptions $options = null) { * ) */ public function release(KVPair $p, WriteOptions $options = null) { - $r = new Request('PUT', sprintf('v1/kv/%s', $p->Key), $this->config); + $r = new Request('PUT', sprintf('v1/kv/%s', $p->Key), $this->config, $p->Value); $r->setWriteOptions($options); $r->Params->set('release', $p->Session); if (0 !== $p->Flags) { diff --git a/tests/Usage/KV/KVClientLockTest.php b/tests/Usage/KV/KVClientLockTest.php new file mode 100644 index 0000000..08c688a --- /dev/null +++ b/tests/Usage/KV/KVClientLockTest.php @@ -0,0 +1,51 @@ +CreateNoChecks(new SessionEntry(['Name' => $name, + 'TTL' => '10s', + 'Behavior' => Consul::SessionBehaviorDelete])); + $this->assertNull($err, sprintf('Error creating session: %s', $err)); + + $kv = new KVPair(['Key' => $key, 'Value' => 'whatever', 'Session' => $id]); + list($wm, $err) = $kvClient->Acquire($kv); + $this->assertNull($err, sprintf('Error acquiring lock: %s', $err)); + $this->assertInstanceOf(WriteMeta::class, $wm); + + list($kv, $_, $err) = $kvClient->Get($key); + $this->assertNull($err, sprintf('Error retrieving key: %s', $err)); + $this->assertInstanceOf(KVPair::class, $kv); + $this->assertEquals($id, $kv->Session); + $this->assertEquals('whatever', $kv->Value); + } +} \ No newline at end of file