PredisShared.php 7.5 KB

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