Skip to content

Commit

Permalink
correcting KVClient::acquire and release methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed May 15, 2018
1 parent 3ec6a2f commit 0690ab9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Consul.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions src/KV/KVClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
51 changes: 51 additions & 0 deletions tests/Usage/KV/KVClientLockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php namespace DCarbone\PHPConsulAPITests\Usage\KV;

use DCarbone\PHPConsulAPI\Config;
use DCarbone\PHPConsulAPI\Consul;
use DCarbone\PHPConsulAPI\KV\KVClient;
use DCarbone\PHPConsulAPI\KV\KVPair;
use DCarbone\PHPConsulAPI\Session\SessionClient;
use DCarbone\PHPConsulAPI\Session\SessionEntry;
use DCarbone\PHPConsulAPI\WriteMeta;
use DCarbone\PHPConsulAPITests\ConsulManager;
use PHPUnit\Framework\TestCase;

/**
* Class KVClientLockTest
* @package DCarbone\PHPConsulAPITests\Usage\KV
*/
class KVClientLockTest extends TestCase {

protected function setUp() {
ConsulManager::startSingle();
}

protected function tearDown() {
ConsulManager::stopSingle();
}

public function testAcquireAndRelease() {
static $name = 'lockme';
static $key = 'lockable';

$conf = new Config();
$kvClient = new KVClient($conf);
$sessionClient = new SessionClient($conf);

list($id, $_, $err) = $sessionClient->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);
}
}

0 comments on commit 0690ab9

Please sign in to comment.