PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/colinmollenhour/credis/tests/CredisClusterTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 224 lines | 210 code | 13 blank | 1 comment | 10 complexity | b19e4f85cebeb4fa138aa943a916f433 MD5 | raw file
  1. <?php
  2. require_once dirname(__FILE__).'/../Client.php';
  3. require_once dirname(__FILE__).'/../Cluster.php';
  4. class CredisClusterTest extends PHPUnit_Framework_TestCase
  5. {
  6. /** @var Credis_Cluster */
  7. protected $cluster;
  8. protected $config;
  9. protected $useStandalone = FALSE;
  10. protected function setUp()
  11. {
  12. if($this->config === NULL) {
  13. $configFile = dirname(__FILE__).'/redis_config.json';
  14. if( ! file_exists($configFile) || ! ($config = file_get_contents($configFile))) {
  15. $this->markTestSkipped('Could not load '.$configFile);
  16. return;
  17. }
  18. $this->config = json_decode($config);
  19. $arrayConfig = array();
  20. foreach($this->config as $config) {
  21. $arrayConfig[] = (array)$config;
  22. }
  23. $this->config = $arrayConfig;
  24. if(count($this->config) < 6) {
  25. $this->markTestSkipped('Config file '.$configFile.' should contain at least 6 entries');
  26. return;
  27. }
  28. }
  29. if($this->useStandalone && !extension_loaded('redis')) {
  30. $this->fail('The Redis extension is not loaded.');
  31. }
  32. $clients = array_slice($this->config,0,4);
  33. $this->cluster = new Credis_Cluster($clients,2,$this->useStandalone);
  34. }
  35. protected function tearDown()
  36. {
  37. if($this->cluster) {
  38. foreach($this->cluster->clients() as $client){
  39. if($client->isConnected()) {
  40. $client->flushAll();
  41. $client->close();
  42. }
  43. }
  44. $this->cluster = NULL;
  45. }
  46. }
  47. public function testKeyHashing()
  48. {
  49. $this->tearDown();
  50. $this->cluster = new Credis_Cluster(array_slice($this->config,0,3),2,$this->useStandalone);
  51. $keys = array();
  52. $lines = explode("\n", file_get_contents("keys.test"));
  53. foreach ($lines as $line) {
  54. $pair = explode(':', trim($line));
  55. if (count($pair) >= 2) {
  56. $keys[$pair[0]] = $pair[1];
  57. }
  58. }
  59. foreach ($keys as $key => $value) {
  60. $this->assertTrue($this->cluster->set($key, $value));
  61. }
  62. $this->cluster = new Credis_Cluster(array_slice($this->config,0,4),2,true,$this->useStandalone);
  63. $hits = 0;
  64. foreach ($keys as $key => $value) {
  65. if ($this->cluster->all('get',$key)) {
  66. $hits++;
  67. }
  68. }
  69. $this->assertEquals(count($keys),$hits);
  70. }
  71. public function testAlias()
  72. {
  73. $slicedConfig = array_slice($this->config,0,4);
  74. foreach($slicedConfig as $config) {
  75. $this->assertEquals($config['port'],$this->cluster->client($config['alias'])->getPort());
  76. }
  77. foreach($slicedConfig as $offset => $config) {
  78. $this->assertEquals($config['port'],$this->cluster->client($offset)->getPort());
  79. }
  80. $alias = "non-existent-alias";
  81. $this->setExpectedException('CredisException',"Client $alias does not exist.");
  82. $this->cluster->client($alias);
  83. }
  84. public function testMasterSlave()
  85. {
  86. $this->tearDown();
  87. $this->cluster = new Credis_Cluster(array($this->config[0],$this->config[6]),2,$this->useStandalone);
  88. $this->assertTrue($this->cluster->client('master')->set('key','value'));
  89. $this->assertEquals('value',$this->cluster->client('slave')->get('key'));
  90. $this->assertEquals('value',$this->cluster->get('key'));
  91. $this->assertFalse($this->cluster->client('slave')->set('key2','value'));
  92. $this->tearDown();
  93. $writeOnlyConfig = $this->config[0];
  94. $writeOnlyConfig['write_only'] = true;
  95. $this->cluster = new Credis_Cluster(array($writeOnlyConfig,$this->config[6]),2,$this->useStandalone);
  96. $this->assertTrue($this->cluster->client('master')->set('key','value'));
  97. $this->assertEquals('value',$this->cluster->client('slave')->get('key'));
  98. $this->assertFalse($this->cluster->client('slave')->set('key2','value'));
  99. $this->assertEquals('value',$this->cluster->get('key'));
  100. }
  101. public function testMasterWithoutSlavesAndWriteOnlyFlag()
  102. {
  103. $this->tearDown();
  104. $writeOnlyConfig = $this->config[0];
  105. $writeOnlyConfig['write_only'] = true;
  106. $this->cluster = new Credis_Cluster(array($writeOnlyConfig),2,$this->useStandalone);
  107. $this->assertTrue($this->cluster->set('key','value'));
  108. $this->assertEquals('value',$this->cluster->get('key'));
  109. }
  110. public function testDontHashForCodeCoverage()
  111. {
  112. $this->assertInternalType('array',$this->cluster->info());
  113. }
  114. public function testByHash()
  115. {
  116. $this->cluster->set('key','value');
  117. $this->assertEquals(6379,$this->cluster->byHash('key')->getPort());
  118. }
  119. public function testRwsplit()
  120. {
  121. $readOnlyCommands = array(
  122. 'EXISTS',
  123. 'TYPE',
  124. 'KEYS',
  125. 'SCAN',
  126. 'RANDOMKEY',
  127. 'TTL',
  128. 'GET',
  129. 'MGET',
  130. 'SUBSTR',
  131. 'STRLEN',
  132. 'GETRANGE',
  133. 'GETBIT',
  134. 'LLEN',
  135. 'LRANGE',
  136. 'LINDEX',
  137. 'SCARD',
  138. 'SISMEMBER',
  139. 'SINTER',
  140. 'SUNION',
  141. 'SDIFF',
  142. 'SMEMBERS',
  143. 'SSCAN',
  144. 'SRANDMEMBER',
  145. 'ZRANGE',
  146. 'ZREVRANGE',
  147. 'ZRANGEBYSCORE',
  148. 'ZREVRANGEBYSCORE',
  149. 'ZCARD',
  150. 'ZSCORE',
  151. 'ZCOUNT',
  152. 'ZRANK',
  153. 'ZREVRANK',
  154. 'ZSCAN',
  155. 'HGET',
  156. 'HMGET',
  157. 'HEXISTS',
  158. 'HLEN',
  159. 'HKEYS',
  160. 'HVALS',
  161. 'HGETALL',
  162. 'HSCAN',
  163. 'PING',
  164. 'AUTH',
  165. 'SELECT',
  166. 'ECHO',
  167. 'QUIT',
  168. 'OBJECT',
  169. 'BITCOUNT',
  170. 'TIME',
  171. 'SORT'
  172. );
  173. foreach($readOnlyCommands as $command){
  174. $this->assertTrue($this->cluster->isReadOnlyCommand($command));
  175. }
  176. $this->assertFalse($this->cluster->isReadOnlyCommand("SET"));
  177. $this->assertFalse($this->cluster->isReadOnlyCommand("HDEL"));
  178. $this->assertFalse($this->cluster->isReadOnlyCommand("RPUSH"));
  179. $this->assertFalse($this->cluster->isReadOnlyCommand("SMOVE"));
  180. $this->assertFalse($this->cluster->isReadOnlyCommand("ZADD"));
  181. }
  182. public function testCredisClientInstancesInConstructor()
  183. {
  184. $this->tearDown();
  185. $two = new Credis_Client($this->config[1]['host'],$this->config[1]['port']);
  186. $three = new Credis_Client($this->config[2]['host'],$this->config[2]['port']);
  187. $four = new Credis_Client($this->config[3]['host'],$this->config[3]['port']);
  188. $this->cluster = new Credis_Cluster(array($two,$three,$four),2,$this->useStandalone);
  189. $this->assertTrue($this->cluster->set('key','value'));
  190. $this->assertEquals('value',$this->cluster->get('key'));
  191. $this->setExpectedException('CredisException','Server should either be an array or an instance of Credis_Client');
  192. new Credis_Cluster(array(new stdClass()),2,$this->useStandalone);
  193. }
  194. public function testSetMasterClient()
  195. {
  196. $this->tearDown();
  197. $master = new Credis_Client($this->config[0]['host'],$this->config[0]['port']);
  198. $slave = new Credis_Client($this->config[6]['host'],$this->config[6]['port']);
  199. $this->cluster = new Credis_Cluster(array($slave),2,$this->useStandalone);
  200. $this->assertInstanceOf('Credis_Cluster',$this->cluster->setMasterClient($master));
  201. $this->assertCount(2,$this->cluster->clients());
  202. $this->assertEquals($this->config[6]['port'],$this->cluster->client(0)->getPort());
  203. $this->assertEquals($this->config[0]['port'],$this->cluster->client('master')->getPort());
  204. $this->cluster = new Credis_Cluster(array($this->config[0]),2,$this->useStandalone);
  205. $this->assertInstanceOf('Credis_Cluster',$this->cluster->setMasterClient(new Credis_Client($this->config[1]['host'],$this->config[1]['port'])));
  206. $this->assertEquals($this->config[0]['port'],$this->cluster->client('master')->getPort());
  207. $this->cluster = new Credis_Cluster(array($slave),2,$this->useStandalone);
  208. $this->assertInstanceOf('Credis_Cluster',$this->cluster->setMasterClient($master,true));
  209. $this->assertCount(1,$this->cluster->clients());
  210. }
  211. }