PredisDistributorTestCase.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Cluster\Distributor;
  11. use PredisTestCase;
  12. /**
  13. *
  14. */
  15. abstract class PredisDistributorTestCase extends PredisTestCase
  16. {
  17. /**
  18. * Returns a new instance of the tested distributor.
  19. *
  20. * @return \Predis\Cluster\Distributor\DistributorInterface
  21. */
  22. abstract protected function getDistributorInstance();
  23. /**
  24. * Returns a list of nodes from the hashring.
  25. *
  26. * @param DistributorInterface $distributor Distributor instance.
  27. * @param int $iterations Number of nodes to fetch.
  28. *
  29. * @return array Nodes from the hashring.
  30. */
  31. protected function getNodes(DistributorInterface $distributor, $iterations = 10)
  32. {
  33. $nodes = array();
  34. for ($i = 0; $i < $iterations; ++$i) {
  35. $hash = $distributor->hash($i * $i);
  36. $nodes[] = $distributor->getByHash($hash);
  37. }
  38. return $nodes;
  39. }
  40. /**
  41. * Returns a distributor instance with the specified nodes added.
  42. *
  43. * @param array $nodes Nodes to add to the distributor.
  44. *
  45. * @return DistributorInterface
  46. */
  47. protected function getSampleDistribution(array $nodes)
  48. {
  49. $distributor = $this->getDistributorInstance();
  50. foreach ($nodes as $node) {
  51. $distributor->add($node);
  52. }
  53. return $distributor;
  54. }
  55. /**
  56. * @group disconnected
  57. */
  58. public function testEmptyRingThrowsException()
  59. {
  60. $this->setExpectedException('Predis\Cluster\Distributor\EmptyRingException');
  61. $distributor = $this->getDistributorInstance();
  62. $distributor->getByHash('nodehash');
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testRemoveOnEmptyRingDoesNotThrowException()
  68. {
  69. $distributor = $this->getDistributorInstance();
  70. $this->assertNull($distributor->remove('node'));
  71. }
  72. }