PredisClusterTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Connection\Cluster;
  11. use PredisTestCase;
  12. /**
  13. *
  14. */
  15. class PredisClusterTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testExposesCommandHashStrategy()
  21. {
  22. $cluster = new PredisCluster();
  23. $this->assertInstanceOf('Predis\Cluster\PredisStrategy', $cluster->getClusterStrategy());
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testAddingConnectionsToCluster()
  29. {
  30. $connection1 = $this->getMockConnection();
  31. $connection2 = $this->getMockConnection();
  32. $cluster = new PredisCluster();
  33. $cluster->add($connection1);
  34. $cluster->add($connection2);
  35. $this->assertSame(2, count($cluster));
  36. $this->assertSame($connection1, $cluster->getConnectionById(0));
  37. $this->assertSame($connection2, $cluster->getConnectionById(1));
  38. }
  39. /**
  40. * @group disconnected
  41. */
  42. public function testAddingConnectionsToClusterUsesConnectionAlias()
  43. {
  44. $connection1 = $this->getMockConnection('tcp://host1:7001?alias=node1');
  45. $connection2 = $this->getMockConnection('tcp://host1:7002?alias=node2');
  46. $cluster = new PredisCluster();
  47. $cluster->add($connection1);
  48. $cluster->add($connection2);
  49. $this->assertSame(2, count($cluster));
  50. $this->assertSame($connection1, $cluster->getConnectionById('node1'));
  51. $this->assertSame($connection2, $cluster->getConnectionById('node2'));
  52. }
  53. /**
  54. * @group disconnected
  55. */
  56. public function testRemovingConnectionsFromCluster()
  57. {
  58. $connection1 = $this->getMockConnection();
  59. $connection2 = $this->getMockConnection();
  60. $connection3 = $this->getMockConnection();
  61. $cluster = new PredisCluster();
  62. $cluster->add($connection1);
  63. $cluster->add($connection2);
  64. $this->assertTrue($cluster->remove($connection1));
  65. $this->assertFalse($cluster->remove($connection3));
  66. $this->assertSame(1, count($cluster));
  67. }
  68. /**
  69. * @group disconnected
  70. */
  71. public function testRemovingConnectionsFromClusterByAlias()
  72. {
  73. $connection1 = $this->getMockConnection();
  74. $connection2 = $this->getMockConnection('tcp://host1:7001?alias=node2');
  75. $connection3 = $this->getMockConnection('tcp://host1:7002?alias=node3');
  76. $cluster = new PredisCluster();
  77. $cluster->add($connection1);
  78. $cluster->add($connection2);
  79. $cluster->add($connection3);
  80. $this->assertTrue($cluster->removeById(0));
  81. $this->assertTrue($cluster->removeById('node2'));
  82. $this->assertFalse($cluster->removeById('node4'));
  83. $this->assertSame(1, count($cluster));
  84. }
  85. /**
  86. * @group disconnected
  87. */
  88. public function testConnectForcesAllConnectionsToConnect()
  89. {
  90. $connection1 = $this->getMockConnection();
  91. $connection1->expects($this->once())->method('connect');
  92. $connection2 = $this->getMockConnection();
  93. $connection2->expects($this->once())->method('connect');
  94. $cluster = new PredisCluster();
  95. $cluster->add($connection1);
  96. $cluster->add($connection2);
  97. $cluster->connect();
  98. }
  99. /**
  100. * @group disconnected
  101. */
  102. public function testDisconnectForcesAllConnectionsToDisconnect()
  103. {
  104. $connection1 = $this->getMockConnection();
  105. $connection1->expects($this->once())->method('disconnect');
  106. $connection2 = $this->getMockConnection();
  107. $connection2->expects($this->once())->method('disconnect');
  108. $cluster = new PredisCluster();
  109. $cluster->add($connection1);
  110. $cluster->add($connection2);
  111. $cluster->disconnect();
  112. }
  113. /**
  114. * @group disconnected
  115. */
  116. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  117. {
  118. $connection1 = $this->getMockConnection();
  119. $connection1->expects($this->once())
  120. ->method('isConnected')
  121. ->will($this->returnValue(false));
  122. $connection2 = $this->getMockConnection();
  123. $connection2->expects($this->once())
  124. ->method('isConnected')
  125. ->will($this->returnValue(true));
  126. $cluster = new PredisCluster();
  127. $cluster->add($connection1);
  128. $cluster->add($connection2);
  129. $this->assertTrue($cluster->isConnected());
  130. }
  131. /**
  132. * @group disconnected
  133. */
  134. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  135. {
  136. $connection1 = $this->getMockConnection();
  137. $connection1->expects($this->once())
  138. ->method('isConnected')
  139. ->will($this->returnValue(false));
  140. $connection2 = $this->getMockConnection();
  141. $connection2->expects($this->once())
  142. ->method('isConnected')
  143. ->will($this->returnValue(false));
  144. $cluster = new PredisCluster();
  145. $cluster->add($connection1);
  146. $cluster->add($connection2);
  147. $this->assertFalse($cluster->isConnected());
  148. }
  149. /**
  150. * @group disconnected
  151. */
  152. public function testCanReturnAnIteratorForConnections()
  153. {
  154. $connection1 = $this->getMockConnection();
  155. $connection2 = $this->getMockConnection();
  156. $cluster = new PredisCluster();
  157. $cluster->add($connection1);
  158. $cluster->add($connection2);
  159. $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
  160. $connections = iterator_to_array($iterator);
  161. $this->assertSame($connection1, $connections[0]);
  162. $this->assertSame($connection2, $connections[1]);
  163. }
  164. /**
  165. * @group disconnected
  166. */
  167. public function testReturnsCorrectConnectionUsingKey()
  168. {
  169. $connection1 = $this->getMockConnection('tcp://host1:7001');
  170. $connection2 = $this->getMockConnection('tcp://host1:7002');
  171. $cluster = new PredisCluster();
  172. $cluster->add($connection1);
  173. $cluster->add($connection2);
  174. $this->assertSame($connection1, $cluster->getConnectionByKey('node01:5431'));
  175. $this->assertSame($connection2, $cluster->getConnectionByKey('node02:3212'));
  176. $this->assertSame($connection1, $cluster->getConnectionByKey('prefix:{node01:5431}'));
  177. $this->assertSame($connection2, $cluster->getConnectionByKey('prefix:{node02:3212}'));
  178. }
  179. /**
  180. * @group disconnected
  181. */
  182. public function testReturnsCorrectConnectionUsingCommandInstance()
  183. {
  184. $commands = $this->getCommandFactory();
  185. $connection1 = $this->getMockConnection('tcp://host1:7001');
  186. $connection2 = $this->getMockConnection('tcp://host1:7002');
  187. $cluster = new PredisCluster();
  188. $cluster->add($connection1);
  189. $cluster->add($connection2);
  190. $set = $commands->createCommand('set', array('node01:5431', 'foobar'));
  191. $get = $commands->createCommand('get', array('node01:5431'));
  192. $this->assertSame($connection1, $cluster->getConnection($set));
  193. $this->assertSame($connection1, $cluster->getConnection($get));
  194. $set = $commands->createCommand('set', array('prefix:{node01:5431}', 'foobar'));
  195. $get = $commands->createCommand('get', array('prefix:{node01:5431}'));
  196. $this->assertSame($connection1, $cluster->getConnection($set));
  197. $this->assertSame($connection1, $cluster->getConnection($get));
  198. $set = $commands->createCommand('set', array('node02:3212', 'foobar'));
  199. $get = $commands->createCommand('get', array('node02:3212'));
  200. $this->assertSame($connection2, $cluster->getConnection($set));
  201. $this->assertSame($connection2, $cluster->getConnection($get));
  202. $set = $commands->createCommand('set', array('prefix:{node02:3212}', 'foobar'));
  203. $get = $commands->createCommand('get', array('prefix:{node02:3212}'));
  204. $this->assertSame($connection2, $cluster->getConnection($set));
  205. $this->assertSame($connection2, $cluster->getConnection($get));
  206. }
  207. /**
  208. * @group disconnected
  209. * @expectedException \Predis\NotSupportedException
  210. * @expectedExceptionMessage Cannot use 'PING' over clusters of connections.
  211. */
  212. public function testThrowsExceptionOnNonShardableCommand()
  213. {
  214. $ping = $this->getCommandFactory()->createCommand('ping');
  215. $cluster = new PredisCluster();
  216. $cluster->add($this->getMockConnection());
  217. $cluster->getConnection($ping);
  218. }
  219. /**
  220. * @group disconnected
  221. */
  222. public function testSupportsKeyHashTags()
  223. {
  224. $commands = $this->getCommandFactory();
  225. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  226. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  227. $cluster = new PredisCluster();
  228. $cluster->add($connection1);
  229. $cluster->add($connection2);
  230. $set = $commands->createCommand('set', array('{node:1001}:foo', 'foobar'));
  231. $get = $commands->createCommand('get', array('{node:1001}:foo'));
  232. $this->assertSame($connection1, $cluster->getConnection($set));
  233. $this->assertSame($connection1, $cluster->getConnection($get));
  234. $set = $commands->createCommand('set', array('{node:1001}:bar', 'foobar'));
  235. $get = $commands->createCommand('get', array('{node:1001}:bar'));
  236. $this->assertSame($connection1, $cluster->getConnection($set));
  237. $this->assertSame($connection1, $cluster->getConnection($get));
  238. }
  239. /**
  240. * @group disconnected
  241. */
  242. public function testWritesCommandToCorrectConnection()
  243. {
  244. $command = $this->getCommandFactory()->createCommand('get', array('node01:5431'));
  245. $connection1 = $this->getMockConnection('tcp://host1:7001');
  246. $connection1->expects($this->once())->method('writeRequest')->with($command);
  247. $connection2 = $this->getMockConnection('tcp://host1:7002');
  248. $connection2->expects($this->never())->method('writeRequest');
  249. $cluster = new PredisCluster();
  250. $cluster->add($connection1);
  251. $cluster->add($connection2);
  252. $cluster->writeRequest($command);
  253. }
  254. /**
  255. * @group disconnected
  256. */
  257. public function testReadsCommandFromCorrectConnection()
  258. {
  259. $command = $this->getCommandFactory()->createCommand('get', array('node02:3212'));
  260. $connection1 = $this->getMockConnection('tcp://host1:7001');
  261. $connection1->expects($this->never())->method('readResponse');
  262. $connection2 = $this->getMockConnection('tcp://host1:7002');
  263. $connection2->expects($this->once())->method('readResponse')->with($command);
  264. $cluster = new PredisCluster();
  265. $cluster->add($connection1);
  266. $cluster->add($connection2);
  267. $cluster->readResponse($command);
  268. }
  269. /**
  270. * @group disconnected
  271. */
  272. public function testExecutesCommandOnCorrectConnection()
  273. {
  274. $command = $this->getCommandFactory()->createCommand('get', array('node01:5431'));
  275. $connection1 = $this->getMockConnection('tcp://host1:7001');
  276. $connection1->expects($this->once())->method('executeCommand')->with($command);
  277. $connection2 = $this->getMockConnection('tcp://host1:7002');
  278. $connection2->expects($this->never())->method('executeCommand');
  279. $cluster = new PredisCluster();
  280. $cluster->add($connection1);
  281. $cluster->add($connection2);
  282. $cluster->executeCommand($command);
  283. }
  284. /**
  285. * @group disconnected
  286. */
  287. public function testExecuteCommandOnEachNode()
  288. {
  289. $ping = $this->getCommandFactory()->createCommand('ping', array());
  290. $connection1 = $this->getMock('Predis\Connection\NodeConnectionInterface');
  291. $connection1->expects($this->once())
  292. ->method('executeCommand')
  293. ->with($ping)
  294. ->will($this->returnValue(true));
  295. $connection2 = $this->getMock('Predis\Connection\NodeConnectionInterface');
  296. $connection2->expects($this->once())
  297. ->method('executeCommand')
  298. ->with($ping)
  299. ->will($this->returnValue(false));
  300. $cluster = new PredisCluster();
  301. $cluster->add($connection1);
  302. $cluster->add($connection2);
  303. $this->assertSame(array(true, false), $cluster->executeCommandOnNodes($ping));
  304. }
  305. /**
  306. * @group disconnected
  307. */
  308. public function testCanBeSerialized()
  309. {
  310. $connection1 = $this->getMockConnection('tcp://host1?alias=first');
  311. $connection2 = $this->getMockConnection('tcp://host2?alias=second');
  312. $cluster = new PredisCluster();
  313. $cluster->add($connection1);
  314. $cluster->add($connection2);
  315. // We use the following line to initialize the underlying hashring.
  316. $cluster->getConnectionByKey('foo');
  317. $unserialized = unserialize(serialize($cluster));
  318. $this->assertEquals($cluster, $unserialized);
  319. }
  320. }