summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/libs/HashRingTest.php
blob: b51eb3f4b700cfc9c2d7799507ab15785d0e25cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

/**
 * @group HashRing
 */
class HashRingTest extends PHPUnit_Framework_TestCase {
	/**
	 * @covers HashRing
	 */
	public function testHashRing() {
		$ring = new HashRing( array( 's1' => 1, 's2' => 1, 's3' => 2, 's4' => 2, 's5' => 2, 's6' => 3 ) );

		$locations = array();
		for ( $i = 0; $i < 20; $i++ ) {
			$locations[ "hello$i"] = $ring->getLocation( "hello$i" );
		}
		$expectedLocations = array(
			"hello0" => "s5",
			"hello1" => "s6",
			"hello2" => "s2",
			"hello3" => "s5",
			"hello4" => "s6",
			"hello5" => "s4",
			"hello6" => "s5",
			"hello7" => "s4",
			"hello8" => "s5",
			"hello9" => "s5",
			"hello10" => "s3",
			"hello11" => "s6",
			"hello12" => "s1",
			"hello13" => "s3",
			"hello14" => "s3",
			"hello15" => "s5",
			"hello16" => "s4",
			"hello17" => "s6",
			"hello18" => "s6",
			"hello19" => "s3"
		);

		$this->assertEquals( $expectedLocations, $locations, 'Items placed at proper locations' );

		$locations = array();
		for ( $i = 0; $i < 5; $i++ ) {
			$locations[ "hello$i"] = $ring->getLocations( "hello$i", 2 );
		}

		$expectedLocations = array(
			"hello0" => array( "s5", "s6" ),
			"hello1" => array( "s6", "s4" ),
			"hello2" => array( "s2", "s1" ),
			"hello3" => array( "s5", "s6" ),
			"hello4" => array( "s6", "s4" ),
		);
		$this->assertEquals( $expectedLocations, $locations, 'Items placed at proper locations' );
	}
}