-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathHashTest.php
More file actions
143 lines (122 loc) · 5.1 KB
/
HashTest.php
File metadata and controls
143 lines (122 loc) · 5.1 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace SplitIO\Test\Suite\Engine;
use SplitIO\Engine\Hash\HashFactory;
use SplitIO\Engine\Hash\LegacyHash;
use SplitIO\Engine\Hash\Murmur3Hash;
use SplitIO\Component\Cache\SplitCache;
use SplitIO\Engine\Hash\HashAlgorithmEnum;
use SplitIO\Grammar\Split;
use SplitIO\Split as SplitApp;
class HashTest extends \PHPUnit_Framework_TestCase
{
public function testLegacyHashFunction()
{
$handle = fopen(__DIR__."/../../files/sample-data.csv", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$_line = explode(',', $line);
if ($_line[0] == '#seed') {
continue;
}
$hashfn = new LegacyHash();
$hash = $hashfn->getHash($_line[1], $_line[0]);
$bucket = abs($hash % 100) + 1;
$this->assertEquals((int)$_line[2], (int)$hash, "Hash, Expected: ".$_line[2]." Calculated: ".$hash);
$this->assertEquals(
(int)$_line[3],
(int)$bucket,
"Bucket, Expected: ".$_line[3]." Calculated: ".$bucket
);
}
fclose($handle);
} else {
$this->assertTrue(false, "Sample Data not found");
}
}
public function testMurmur3HashFunction()
{
$handles = array(
fopen(__DIR__."/../../files/murmur3-sample-double-treatment-users.csv", "r"),
fopen(__DIR__."/../../files/murmur3-sample-data-v2.csv", "r"),
fopen(__DIR__."/../../files/murmur3-sample-data-non-alpha-numeric-v2.csv", "r"),
);
foreach ($handles as $handle) {
if ($handle) {
while (($line = fgets($handle)) !== false) {
$_line = explode(',', $line);
if ($_line[0] == 'seed') {
continue;
}
$hashfn = new Murmur3Hash();
$hash = $hashfn->getHash($_line[1], $_line[0]);
$bucket = abs($hash % 100) + 1;
$this->assertEquals((int)$_line[2], (int)$hash, "Hash, Expected: ".$_line[2]." Calculated: ".$hash);
$this->assertEquals(
(int)$_line[3],
(int)$bucket,
"Bucket, Expected: ".$_line[3]." Calculated: ".$bucket
);
}
fclose($handle);
} else {
$this->assertTrue(false, "Sample Data not found");
}
}
}
private function addSplitsInCache()
{
$splitChanges = file_get_contents(__DIR__."/../../files/algoSplits.json");
$this->assertJson($splitChanges);
$splitCache = new SplitCache();
$splitChanges = json_decode($splitChanges, true);
$splits = $splitChanges['splits'];
foreach ($splits as $split) {
$splitName = $split['name'];
$this->assertTrue($splitCache->addSplit($splitName, json_encode($split)));
}
}
public function testAlgoField()
{
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
$options = array();
$sdkConfig = array(
'log' => array('adapter' => 'stdout'),
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options)
);
//Initializing the SDK instance.
$splitFactory = \SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);
$splitFactory->client();
//Populating the cache.
$this->addSplitsInCache();
$cases = array(
array( // Split with algo = 1. Should use legacy function
'key' => 'some_feature_1',
'algo' => HashAlgorithmEnum::LEGACY,
'class' => '\SplitIO\Engine\Hash\LegacyHash'
),
array( // Split with algo = 2. Should use murmur function
'key' => 'some_feature_2',
'algo' => HashAlgorithmEnum::MURMUR,
'class' => '\SplitIO\Engine\Hash\Murmur3Hash'
),
array( // Split with algo = null. Should use legacy function
'key' => 'some_feature_3',
'algo' => HashAlgorithmEnum::LEGACY,
'class' => '\SplitIO\Engine\Hash\LegacyHash'
),
array( // Split with no algo field. SHould use legacy function
'key' => 'some_feature_4',
'algo' => HashAlgorithmEnum::LEGACY,
'class' => '\SplitIO\Engine\Hash\LegacyHash'
),
);
foreach ($cases as $case) {
$splitCacheKey = SplitCache::getCacheKeyForSplit($case['key']);
$splitCachedItem = SplitApp::cache()->getItem($splitCacheKey);
$split = new Split(json_decode($splitCachedItem->get(), true));
$this->assertEquals($split->getAlgo(), $case['algo']);
$hasher = HashFactory::getHashAlgorithm($split->getAlgo());
$this->assertInstanceof($case['class'], $hasher);
}
}
}