PredisShared.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. // -------------------------------------------------------------------------- //
  3. define('I_AM_AWARE_OF_THE_DESTRUCTIVE_POWER_OF_THIS_TEST_SUITE', false);
  4. // -------------------------------------------------------------------------- //
  5. Predis_RedisServerProfile::registerProfile('Predis_RedisServer_v1_2', '1.2');
  6. Predis_RedisServerProfile::registerProfile('Predis_RedisServer_v2_0', '2.0');
  7. Predis_RedisServerProfile::registerProfile('Predis_RedisServer_v2_2', '2.2');
  8. if (I_AM_AWARE_OF_THE_DESTRUCTIVE_POWER_OF_THIS_TEST_SUITE !== true) {
  9. exit(
  10. "Please set the I_AM_AWARE_OF_THE_DESTRUCTIVE_POWER_OF_THIS_TEST_SUITE " .
  11. "constant to TRUE in PredisShared.php if you want to proceed.\n"
  12. );
  13. }
  14. if (!function_exists('array_union')) {
  15. function array_union(Array $a, Array $b) {
  16. return array_merge($a, array_diff($b, $a));
  17. }
  18. }
  19. function p_anon($param, $function) {
  20. return create_function($param, $function);
  21. }
  22. class RC {
  23. const SERVER_HOST = '127.0.0.1';
  24. const SERVER_PORT = 6379;
  25. const DEFAULT_DATABASE = 15;
  26. const WIPE_OUT = 1;
  27. const EXCEPTION_WRONG_TYPE = 'Operation against a key holding the wrong kind of value';
  28. const EXCEPTION_NO_SUCH_KEY = 'no such key';
  29. const EXCEPTION_OUT_OF_RANGE = 'index out of range';
  30. const EXCEPTION_OFFSET_RANGE = 'offset is out of range';
  31. const EXCEPTION_INVALID_DB_IDX = 'invalid DB index';
  32. const EXCEPTION_VALUE_NOT_INT = 'value is not an integer';
  33. const EXCEPTION_EXEC_NO_MULTI = 'EXEC without MULTI';
  34. const EXCEPTION_SETEX_TTL = 'invalid expire time in SETEX';
  35. const EXCEPTION_HASH_VALNOTINT = 'hash value is not an integer';
  36. const EXCEPTION_BIT_VALUE = 'bit is not an integer or out of range';
  37. const EXCEPTION_BIT_OFFSET = 'bit offset is not an integer or out of range';
  38. private static $_connection;
  39. public static function getConnectionArguments() {
  40. return array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT);
  41. }
  42. public static function getConnectionParameters() {
  43. return new Predis_ConnectionParameters(array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT));
  44. }
  45. private static function createConnection() {
  46. $serverProfile = Predis_RedisServerProfile::get('2.2');
  47. $connection = new Predis_Client(RC::getConnectionArguments(), $serverProfile);
  48. $connection->connect();
  49. $connection->select(RC::DEFAULT_DATABASE);
  50. return $connection;
  51. }
  52. public static function getConnection($new = false) {
  53. if ($new == true) {
  54. return self::createConnection();
  55. }
  56. if (self::$_connection === null || !self::$_connection->isConnected()) {
  57. self::$_connection = self::createConnection();
  58. }
  59. return self::$_connection;
  60. }
  61. public static function resetConnection() {
  62. if (self::$_connection !== null && self::$_connection->isConnected()) {
  63. self::$_connection->disconnect();
  64. self::$_connection = self::createConnection();
  65. }
  66. }
  67. public static function helperForBlockingPops($op) {
  68. // TODO: I admit that this helper is kinda lame and it does not run
  69. // in a separate process to properly test BLPOP/BRPOP
  70. $redisUri = sprintf('redis://%s:%d/?database=%d', RC::SERVER_HOST, RC::SERVER_PORT, RC::DEFAULT_DATABASE);
  71. $handle = popen('php', 'w');
  72. $dir = __DIR__;
  73. fwrite($handle, "<?php
  74. require '{$dir}/../lib/Predis.php';
  75. \$redis = Predis_Client::create('$redisUri');
  76. \$redis->rpush('{$op}1', 'a');
  77. \$redis->rpush('{$op}2', 'b');
  78. \$redis->rpush('{$op}3', 'c');
  79. \$redis->rpush('{$op}1', 'd');
  80. ?>");
  81. pclose($handle);
  82. }
  83. public static function getArrayOfNumbers() {
  84. return array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  85. }
  86. public static function getKeyValueArray() {
  87. return array(
  88. 'foo' => 'bar',
  89. 'hoge' => 'piyo',
  90. 'foofoo' => 'barbar',
  91. );
  92. }
  93. public static function getNamespacedKeyValueArray() {
  94. return array(
  95. 'metavar:foo' => 'bar',
  96. 'metavar:hoge' => 'piyo',
  97. 'metavar:foofoo' => 'barbar',
  98. );
  99. }
  100. public static function getZSetArray() {
  101. return array(
  102. 'a' => -10, 'b' => 0, 'c' => 10, 'd' => 20, 'e' => 20, 'f' => 30
  103. );
  104. }
  105. public static function sameValuesInArrays($arrayA, $arrayB) {
  106. if (count($arrayA) != count($arrayB)) {
  107. return false;
  108. }
  109. return count(array_diff($arrayA, $arrayB)) == 0;
  110. }
  111. public static function testForServerException($testcaseInstance, $expectedMessage, $wrapFunction) {
  112. $thrownException = null;
  113. try {
  114. $wrapFunction($testcaseInstance);
  115. }
  116. catch (Predis_ServerException $exception) {
  117. $thrownException = $exception;
  118. }
  119. $testcaseInstance->assertInstanceOf('Predis_ServerException', $thrownException);
  120. if (isset($expectedMessage)) {
  121. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  122. }
  123. }
  124. public static function testForClientException($testcaseInstance, $expectedMessage, $wrapFunction) {
  125. $thrownException = null;
  126. try {
  127. $wrapFunction($testcaseInstance);
  128. }
  129. catch (Predis_ClientException $exception) {
  130. $thrownException = $exception;
  131. }
  132. $testcaseInstance->assertInstanceOf('Predis_ClientException', $thrownException);
  133. if (isset($expectedMessage)) {
  134. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  135. }
  136. }
  137. public static function testForCommunicationException($testcaseInstance, $expectedMessage, $wrapFunction) {
  138. $thrownException = null;
  139. try {
  140. $wrapFunction($testcaseInstance);
  141. }
  142. catch (Predis_CommunicationException $exception) {
  143. $thrownException = $exception;
  144. }
  145. $testcaseInstance->assertInstanceOf('Predis_CommunicationException', $thrownException);
  146. if (isset($expectedMessage)) {
  147. $testcaseInstance->assertEquals($expectedMessage, $thrownException->getMessage());
  148. }
  149. }
  150. public static function pushTailAndReturn(Predis_Client $client, $keyName, Array $values, $wipeOut = 0) {
  151. if ($wipeOut == true) {
  152. $client->del($keyName);
  153. }
  154. foreach ($values as $value) {
  155. $client->rpush($keyName, $value);
  156. }
  157. return $values;
  158. }
  159. public static function setAddAndReturn(Predis_Client $client, $keyName, Array $values, $wipeOut = 0) {
  160. if ($wipeOut == true) {
  161. $client->del($keyName);
  162. }
  163. foreach ($values as $value) {
  164. $client->sadd($keyName, $value);
  165. }
  166. return $values;
  167. }
  168. public static function zsetAddAndReturn(Predis_Client $client, $keyName, Array $values, $wipeOut = 0) {
  169. // $values: array(SCORE => VALUE, ...);
  170. if ($wipeOut == true) {
  171. $client->del($keyName);
  172. }
  173. foreach ($values as $value => $score) {
  174. $client->zadd($keyName, $score, $value);
  175. }
  176. return $values;
  177. }
  178. public static function getConnectionParametersArgumentsArray() {
  179. return array(
  180. 'host' => '10.0.0.1', 'port' => 6380, 'connection_timeout' => 10, 'read_write_timeout' => 30,
  181. 'database' => 5, 'password' => 'dbpassword', 'alias' => 'connection_alias'
  182. );
  183. }
  184. public static function getConnectionParametersArgumentsString($arguments = null) {
  185. // TODO: must be improved
  186. $args = $arguments !== null ? $arguments : RC::getConnectionParametersArgumentsArray();
  187. $paramsString = "redis://{$args['host']}:{$args['port']}/";
  188. $paramsString .= "?connection_timeout={$args['connection_timeout']}&read_write_timeout={$args['read_write_timeout']}";
  189. $paramsString .= "&database={$args['database']}&password={$args['password']}&alias={$args['alias']}";
  190. return $paramsString;
  191. }
  192. }
  193. ?>