1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Esi\ConsistentHash. |
7
|
|
|
* |
8
|
|
|
* (c) Eric Sizemore <[email protected]> |
9
|
|
|
* (c) Paul Annesley <[email protected]> |
10
|
|
|
* |
11
|
|
|
* This source file is subject to the MIT license. For the full copyright and |
12
|
|
|
* license information, please view the LICENSE file that was distributed with |
13
|
|
|
* this source code. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Esi\ConsistentHash\Tests\Hasher; |
17
|
|
|
|
18
|
|
|
use Esi\ConsistentHash\ConsistentHash; |
19
|
|
|
use Esi\ConsistentHash\Hasher\Crc32Hasher; |
20
|
|
|
use Esi\ConsistentHash\Hasher\Md5Hasher; |
21
|
|
|
use Esi\ConsistentHash\Hasher\Xxh32Hasher; |
22
|
|
|
use PHPUnit\Framework\Attributes\CoversClass; |
23
|
|
|
use PHPUnit\Framework\Attributes\UsesClass; |
24
|
|
|
use PHPUnit\Framework\TestCase; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @internal |
28
|
|
|
*/ |
29
|
|
|
#[CoversClass(Crc32Hasher::class)] |
30
|
|
|
#[CoversClass(Md5Hasher::class)] |
31
|
|
|
#[CoversClass(Xxh32Hasher::class)] |
32
|
|
|
#[UsesClass(ConsistentHash::class)] |
33
|
|
|
class HasherTest extends TestCase |
34
|
|
|
{ |
35
|
|
|
public function testCrc32Hash(): void |
36
|
|
|
{ |
37
|
|
|
$hasher = new Crc32Hasher(); |
38
|
|
|
$result1 = $hasher->hash('test'); |
39
|
|
|
$result2 = $hasher->hash('test'); |
40
|
|
|
$result3 = $hasher->hash('different'); |
41
|
|
|
|
42
|
|
|
self::assertEquals($result1, $result2); |
43
|
|
|
self::assertNotEquals($result1, $result3); // fragile but worthwhile |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testCrc32HashSpaceLookupsAreValidTargets(): void |
47
|
|
|
{ |
48
|
|
|
$targets = []; |
49
|
|
|
foreach (range(1, 10) as $i) { |
50
|
|
|
$targets[] = \sprintf('target%s', $i); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$hashSpace = new ConsistentHash(new Crc32Hasher()); |
54
|
|
|
$hashSpace->addTargets($targets); |
55
|
|
|
|
56
|
|
|
foreach (range(1, 10) as $i) { |
57
|
|
|
self::assertTrue( |
58
|
|
|
\in_array($hashSpace->lookup(\sprintf('r%s', $i)), $targets, true), |
59
|
|
|
'target must be in list of targets', |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testMd5Hash(): void |
65
|
|
|
{ |
66
|
|
|
$hasher = new Md5Hasher(); |
67
|
|
|
$result1 = $hasher->hash('test'); |
68
|
|
|
$result2 = $hasher->hash('test'); |
69
|
|
|
$result3 = $hasher->hash('different'); |
70
|
|
|
|
71
|
|
|
self::assertEquals($result1, $result2); |
72
|
|
|
self::assertNotEquals($result1, $result3); // fragile but worthwhile |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testMd5HashSpaceLookupsAreValidTargets(): void |
76
|
|
|
{ |
77
|
|
|
$targets = []; |
78
|
|
|
foreach (range(1, 10) as $i) { |
79
|
|
|
$targets[] = \sprintf('target%s', $i); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$hashSpace = new ConsistentHash(new Md5Hasher()); |
83
|
|
|
$hashSpace->addTargets($targets); |
84
|
|
|
|
85
|
|
|
foreach (range(1, 10) as $i) { |
86
|
|
|
self::assertTrue( |
87
|
|
|
\in_array($hashSpace->lookup(\sprintf('r%s', $i)), $targets, true), |
88
|
|
|
'target must be in list of targets', |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testXxh32Hash(): void |
94
|
|
|
{ |
95
|
|
|
$hasher = new Xxh32Hasher(); |
96
|
|
|
$result1 = $hasher->hash('test'); |
97
|
|
|
$result2 = $hasher->hash('test'); |
98
|
|
|
$result3 = $hasher->hash('different'); |
99
|
|
|
|
100
|
|
|
self::assertEquals($result1, $result2); |
101
|
|
|
self::assertNotEquals($result1, $result3); // fragile but worthwhile |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testXxh32HashSpaceLookupsAreValidTargets(): void |
105
|
|
|
{ |
106
|
|
|
$targets = []; |
107
|
|
|
foreach (range(1, 10) as $i) { |
108
|
|
|
$targets[] = \sprintf('target%s', $i); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$hashSpace = new ConsistentHash(new Xxh32Hasher()); |
112
|
|
|
$hashSpace->addTargets($targets); |
113
|
|
|
|
114
|
|
|
foreach (range(1, 10) as $i) { |
115
|
|
|
self::assertTrue( |
116
|
|
|
\in_array($hashSpace->lookup(\sprintf('r%s', $i)), $targets, true), |
117
|
|
|
'target must be in list of targets', |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|