KetamaPureRingTest.php 3.2 KB

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