Make it possible to serialize and unserialize connection instances.

This commit is contained in:
Daniele Alessandri
2012-01-14 13:57:10 +01:00
parent 626520b836
commit 93163bdfb3
9 changed files with 112 additions and 0 deletions
@@ -132,4 +132,12 @@ class ComposableStreamConnection extends StreamConnection implements IConnection
{
return $this->protocol->read($this);
}
/**
* {@inheritdoc}
*/
public function __sleep()
{
return array_merge(parent::__sleep(), array('protocol'));
}
}
+8
View File
@@ -232,4 +232,12 @@ abstract class ConnectionBase implements IConnectionSingle
return $this->cachedId;
}
/**
* {@inheritdoc}
*/
public function __sleep()
{
return array('parameters', 'initCmds');
}
}
@@ -384,4 +384,12 @@ class PhpiredisConnection extends ConnectionBase
array_unshift($cmdargs, $command->getId());
$this->write(phpiredis_format_command($cmdargs));
}
/**
* {@inheritdoc}
*/
public function __wakeup()
{
$this->initializeProtocol($this->getParameters());
}
}
+8
View File
@@ -284,4 +284,12 @@ class StreamConnection extends ConnectionBase
$this->writeBytes($buffer);
}
/**
* {@inheritdoc}
*/
public function __sleep()
{
return array_merge(parent::__sleep(), array('mbiterable', 'throwErrors'));
}
}
+20
View File
@@ -321,4 +321,24 @@ class WebdisConnection implements IConnectionSingle
{
return "{$this->parameters->host}:{$this->parameters->port}";
}
/**
* {@inheritdoc}
*/
public function __sleep()
{
return array('parameters');
}
/**
* {@inheritdoc}
*/
public function __wakeup()
{
$this->checkExtensions();
$parameters = $this->getParameters();
$this->resource = $this->initializeCurl($parameters);
$this->reader = $this->initializeReader($parameters);
}
}
@@ -53,6 +53,20 @@ class PhpiredisConnectionTest extends ConnectionTestCase
$connection = new PhpiredisConnection($parameters);
}
/**
* @group disconnected
*/
public function testCanBeSerialized()
{
$parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
$connection = new PhpiredisConnection($parameters);
$unserialized = unserialize(serialize($connection));
$this->assertInstanceOf('Predis\Network\PhpiredisConnection', $unserialized);
$this->assertEquals($parameters, $unserialized->getParameters());
}
// ******************************************************************** //
// ---- INTEGRATION TESTS --------------------------------------------- //
// ******************************************************************** //
@@ -321,6 +321,25 @@ class PredisClusterTest extends StandardTestCase
$cluster->executeCommand($command);
}
/**
* @group disconnected
*/
public function testCanBeSerialized()
{
$connection1 = $this->getMockConnection('tcp://host1?alias=first');
$connection2 = $this->getMockConnection('tcp://host2?alias=second');
$cluster = new PredisCluster();
$cluster->add($connection1);
$cluster->add($connection2);
// We use the following line to initialize the underlying hashring.
$cluster->getConnectionByKey('foo');
$unserialized = unserialize(serialize($cluster));
$this->assertEquals($cluster, $unserialized);
}
// ******************************************************************** //
// ---- HELPER METHODS ------------------------------------------------ //
// ******************************************************************** //
@@ -53,6 +53,19 @@ class StreamConnectionTest extends ConnectionTestCase
$connection = new StreamConnection($parameters);
}
/**
* @group disconnected
*/
public function testCanBeSerialized()
{
$parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
$connection = new StreamConnection($parameters);
$unserialized = unserialize(serialize($connection));
$this->assertEquals($connection, $unserialized);
}
// ******************************************************************** //
// ---- INTEGRATION TESTS --------------------------------------------- //
// ******************************************************************** //
@@ -100,6 +100,20 @@ class WebdisConnectionTest extends StandardTestCase
$connection->executeCommand($this->getProfile()->createCommand('auth', array('foobar')));
}
/**
* @group disconnected
*/
public function testCanBeSerialized()
{
$parameters = $this->getParameters(array('alias' => 'webdis'));
$connection = new WebdisConnection($parameters);
$unserialized = unserialize(serialize($connection));
$this->assertInstanceOf('Predis\Network\WebdisConnection', $unserialized);
$this->assertEquals($parameters, $unserialized->getParameters());
}
// ******************************************************************** //
// ---- INTEGRATION TESTS --------------------------------------------- //
// ******************************************************************** //