PredisDistributorTestCase.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. * @return array Nodes from the hashring.
  29. */
  30. protected function getNodes(DistributorInterface $distributor, $iterations = 10)
  31. {
  32. $nodes = array();
  33. for ($i = 0; $i < $iterations; $i++) {
  34. $hash = $distributor->hash($i * $i);
  35. $nodes[] = $distributor->getByHash($hash);
  36. }
  37. return $nodes;
  38. }
  39. /**
  40. * Returns a distributor instance with the specified nodes added.
  41. *
  42. * @param array $nodes Nodes to add to the distributor.
  43. * @return DistributorInterface
  44. */
  45. protected function getSampleDistribution(array $nodes)
  46. {
  47. $distributor = $this->getDistributorInstance();
  48. foreach ($nodes as $node) {
  49. $distributor->add($node);
  50. }
  51. return $distributor;
  52. }
  53. /**
  54. * @group disconnected
  55. */
  56. public function testEmptyRingThrowsException()
  57. {
  58. $this->setExpectedException('Predis\Cluster\Distributor\EmptyRingException');
  59. $distributor = $this->getDistributorInstance();
  60. $distributor->getByHash('nodehash');
  61. }
  62. /**
  63. * @group disconnected
  64. */
  65. public function testRemoveOnEmptyRingDoesNotThrowException()
  66. {
  67. $distributor = $this->getDistributorInstance();
  68. $this->assertNull($distributor->remove('node'));
  69. }
  70. }