Skip to content

Commit

Permalink
add memcached support
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleferguson committed Mar 24, 2016
1 parent 205c2c8 commit dde2cc4
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"mockery/mockery": "^0.9.4"
"mockery/mockery": "^0.9.4",
"gecko-packages/gecko-memcache-mock": "^1.0"
}
}
143 changes: 143 additions & 0 deletions src/Bundles/General/MemcachedHealthCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
namespace GenTux\Healthz\Bundles\General;

use Memcached;
use GenTux\Healthz\HealthCheck;
use GenTux\Healthz\Exceptions\HealthFailureException;

/**
* Health check for memcached connection
*
* @package GenTux\Healthz
*/
class MemcachedHealthCheck extends HealthCheck
{

protected $title = 'Memcached';

protected $servers = [];
protected $options = [];
protected $username = null;
protected $password = null;

/** @var Memcached */
protected $memcached;

public function __construct($memcached = null)
{
$this->memcached = $memcached ?: new Memcached();
}

/**
* Check for connection to memcached servers
*
* @return mixed
*/
public function run()
{
if (count($this->servers())) {
$this->memcached->addServers($this->servers());
}

if (count($this->options())) {
$this->memcached->setOptions($this->options());
}

if (!is_null($this->username())) {
$this->memcached->setSaslAuthData($this->username(), $this->password());
}

$result = $this->memcached->set('test.connection', 'success', 1);
if (!$result) {
throw new HealthFailureException('Unable to set test value in memcache');
}

$this->setStatus('able to set test value in memcache');
}

/**
* Add server to check
*
* @param string $server
* @param int $port
* @param int $weight
*
* @return self
*/
public function addServer($server, $port = 11211, $weight = 0)
{
$this->servers[] = [$server, $port, $weight];

return $this;
}

/**
* Get servers
*
* @return array
*/
public function servers()
{
return $this->servers;
}

/**
* Set memcached options
*
* @param array $options
*
* @return self
*/
public function setOptions(array $options)
{
$this->options = $options;

return $this;
}

/**
* Get options
*
* @return array
*/
public function options()
{
return $this->options;
}

/**
* Set username and password for servers
*
* @param string $username
* @param string $password
*
* @return self
*/
public function setAuth($username, $password)
{
$this->username = $username;
$this->password = $password;

return $this;
}

/**
* Get username
*
* @return string|null
*/
public function username()
{
return $this->username;
}

/**
* Get password
*
* @return string|null
*/
public function password()
{
return $this->password;
}
}
95 changes: 95 additions & 0 deletions tests/Bundles/General/MemcachedHealthCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
namespace {
// if memcached isnt installed, create a stub for tests
if (!class_exists('Memcached')) {
class Memcached {
}
}
}

namespace GenTux\Healthz\Bundles\General {

use Mockery;
use GenTux\Healthz\HealthCheck;
use GeckoPackages\MemcacheMock\MemcachedMock;

class MemcachedHealthCheckTest extends \TestCase
{

/** @var \Memcached */
protected $memcached;

/** @var MemcachedHealthCheck */
protected $health;

public function setUp()
{
parent::setUp();
$this->memcached = Mockery::mock(MemcachedMock::class);
$this->health = new MemcachedHealthCheck($this->memcached);
}

/** @test */
public function instance_of_health_check()
{
$this->assertInstanceOf(HealthCheck::class, $this->health);
}

/** @test */
public function add_servers()
{
$this->health->addServer('123.com');
$this->health->addServer('456.com', 2222, 1);

$expect = [
['123.com', 11211, 0],
['456.com', 2222, 1],
];
$this->assertSame($expect, $this->health->servers());
}

/** @test */
public function set_options()
{
$this->health->setOptions([1 => 'foo']);
$this->assertSame([1 => 'foo'], $this->health->options());
}

/** @test */
public function username_and_password()
{
$this->health->setAuth('user', 'secret');

$this->assertSame('user', $this->health->username());
$this->assertSame('secret', $this->health->password());
}

/** @test */
public function run_builds_memcached_instance_and_tests_connection()
{
$this->health->addServer('123.com');
$this->health->addServer('456.com', 2222, 1);
$this->health->setAuth('user', 'secret');
$this->health->setOptions(['foo' => 'bar']);

# spy on memcached instance
$servers = [ ['123.com', 11211, 0], ['456.com', 2222, 1] ];
$this->memcached->shouldReceive('addServers')->with($servers)->once();
$this->memcached->shouldReceive('setOptions')->with(['foo' => 'bar'])->once();
$this->memcached->shouldReceive('setSaslAuthData')->with('user', 'secret')->once();
$this->memcached->shouldReceive('set')->with('test.connection', 'success', 1)->once()->andReturn(true);

$this->health->run();
}

/**
* @test
* @expectedException \GenTux\Healthz\Exceptions\HealthFailureException
*/
public function run_throws_failure_exception_if_memcached_cant_set_test_value()
{
$this->memcached->shouldReceive('set')->with('test.connection', 'success', 1)->once()->andReturn(false);
$this->health->run();
}
}
}

0 comments on commit dde2cc4

Please sign in to comment.