Example #1
0
 public function testHopeRemovingTargetDoesNotChangeMuchWithCrc32Hasher()
 {
     $hashSpace = new Flexihash(new Flexihash_Crc32Hasher());
     foreach (range(1, $this->_targets) as $i) {
         $hashSpace->addTarget("target{$i}");
     }
     $results1 = array();
     foreach (range(1, $this->_lookups) as $i) {
         $results1[$i] = $hashSpace->lookup("t{$i}");
     }
     $hashSpace->removeTarget("target1");
     $results2 = array();
     foreach (range(1, $this->_lookups) as $i) {
         $results2[$i] = $hashSpace->lookup("t{$i}");
     }
     $differences = 0;
     foreach (range(1, $this->_lookups) as $i) {
         if ($results1[$i] !== $results2[$i]) {
             $differences++;
         }
     }
     $percent = round($differences / $this->_lookups * 100);
     $this->dump("ConsistentHash: {$percent}% of lookups changed " . "after removing 1 of {$this->_targets} targets");
 }
Example #2
0
 public function testFallbackPrecedenceWhenServerRemoved()
 {
     $mockHasher = new MockHasher();
     $hashSpace = new Flexihash($mockHasher, 1);
     $mockHasher->setHashValue(10);
     $hashSpace->addTarget("t1");
     $mockHasher->setHashValue(20);
     $hashSpace->addTarget("t2");
     $mockHasher->setHashValue(30);
     $hashSpace->addTarget("t3");
     $mockHasher->setHashValue(15);
     $this->assertEqual($hashSpace->lookup('resource'), 't2');
     $this->assertEqual($hashSpace->lookupList('resource', 3), array('t2', 't3', 't1'));
     $hashSpace->removeTarget('t2');
     $this->assertEqual($hashSpace->lookup('resource'), 't3');
     $this->assertEqual($hashSpace->lookupList('resource', 3), array('t3', 't1'));
     $hashSpace->removeTarget('t3');
     $this->assertEqual($hashSpace->lookup('resource'), 't1');
     $this->assertEqual($hashSpace->lookupList('resource', 3), array('t1'));
 }