PredisClusterTest.php 13 KB

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