-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
205c2c8
commit dde2cc4
Showing
3 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |