Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Allow passing already configured redis client to the storage adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
bdudelsack committed Sep 15, 2016
1 parent 43fc3bd commit 71ecf64
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Prometheus/Storage/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public function __construct(array $options = array())
}

$this->options = array_merge(self::$defaultOptions, $options);
$this->redis = new \Redis();

if(isset($this->options['redis']) && $this->options['redis'] instanceof \Redis) {
$this->redis = $this->options['redis'];
}
}

/**
Expand Down Expand Up @@ -80,7 +83,13 @@ function (array $metric) {
*/
private function openConnection()
{
if($this->redis != null) {
return;
}

try {
$this->redis = new \Redis();

if ($this->options['persistent_connections']) {
@$this->redis->pconnect($this->options['host'], $this->options['port'], $this->options['timeout']);
} else {
Expand Down
26 changes: 26 additions & 0 deletions tests/Test/Prometheus/Storage/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,30 @@ public function itShouldThrowAnExceptionOnConnectionFailure()
$redis->flushRedis();
}

public function testReuseRedisClient()
{
$redisClient = $this->getMockBuilder(\Redis::class)->getMock();
$redisStorage = new Redis(['redis' => $redisClient]);

$this->assertAttributeEquals($redisClient, 'redis', $redisStorage);

$redisClient->expects($this->atLeastOnce())
->method('flushAll');

$redisClient->expects($this->never())
->method('connect');

$redisStorage->flushRedis();
}

public function testReuseRedisClientWithDefaultOptions()
{
$redisClient = $this->getMockBuilder(\Redis::class)->getMock();

Redis::setDefaultOptions(['redis' => $redisClient]);

$redisStorage = new Redis();

$this->assertAttributeEquals($redisClient, 'redis', $redisStorage);
}
}

0 comments on commit 71ecf64

Please sign in to comment.