HashRingTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Distribution;
  11. /**
  12. * @todo To be improved.
  13. */
  14. class HashRingTest extends DistributionStrategyTestCase
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function getDistributorInstance()
  20. {
  21. return new HashRing();
  22. }
  23. /**
  24. * @group disconnected
  25. */
  26. public function testGenerateKey()
  27. {
  28. $ring = $this->getDistributorInstance();
  29. $this->assertEquals(crc32('foobar'), $ring->generateKey('foobar'));
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testSingleNodeInRing()
  35. {
  36. $node = '127.0.0.1:7000';
  37. $ring = $this->getDistributorInstance();
  38. $ring->add($node);
  39. $expected = array_fill(0, 20, $node);
  40. $actual = $this->getNodes($ring, 20);
  41. $this->assertSame($expected, $actual);
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testMultipleNodesInRing()
  47. {
  48. $nodes = array(
  49. '127.0.0.1:7000',
  50. '127.0.0.1:7001',
  51. '127.0.0.1:7002',
  52. );
  53. $ring = $this->getDistributorInstance();
  54. foreach ($nodes as $node) {
  55. $ring->add($node);
  56. }
  57. $expected = array(
  58. '127.0.0.1:7001',
  59. '127.0.0.1:7001',
  60. '127.0.0.1:7001',
  61. '127.0.0.1:7002',
  62. '127.0.0.1:7002',
  63. '127.0.0.1:7001',
  64. '127.0.0.1:7001',
  65. '127.0.0.1:7000',
  66. '127.0.0.1:7001',
  67. '127.0.0.1:7002',
  68. '127.0.0.1:7002',
  69. '127.0.0.1:7002',
  70. '127.0.0.1:7002',
  71. '127.0.0.1:7000',
  72. '127.0.0.1:7002',
  73. '127.0.0.1:7002',
  74. '127.0.0.1:7002',
  75. '127.0.0.1:7000',
  76. '127.0.0.1:7001',
  77. '127.0.0.1:7002',
  78. );
  79. $actual = $this->getNodes($ring, 20);
  80. $this->assertSame($expected, $actual);
  81. }
  82. /**
  83. * @group disconnected
  84. */
  85. public function testSubsequendAddAndRemoveFromRing()
  86. {
  87. $ring = $this->getDistributorInstance();
  88. $expected1 = array_fill(0, 10, '127.0.0.1:7000');
  89. $expected3 = array_fill(0, 10, '127.0.0.1:7001');
  90. $expected2 = array(
  91. '127.0.0.1:7001',
  92. '127.0.0.1:7001',
  93. '127.0.0.1:7001',
  94. '127.0.0.1:7001',
  95. '127.0.0.1:7001',
  96. '127.0.0.1:7001',
  97. '127.0.0.1:7001',
  98. '127.0.0.1:7000',
  99. '127.0.0.1:7001',
  100. '127.0.0.1:7000',
  101. );
  102. $ring->add('127.0.0.1:7000');
  103. $actual1 = $this->getNodes($ring, 10);
  104. $ring->add('127.0.0.1:7001');
  105. $actual2 = $this->getNodes($ring, 10);
  106. $ring->remove('127.0.0.1:7000');
  107. $actual3 = $this->getNodes($ring, 10);
  108. $this->assertSame($expected1, $actual1);
  109. $this->assertSame($expected2, $actual2);
  110. $this->assertSame($expected3, $actual3);
  111. }
  112. }