diff options
author | Nicolas Favre-Felix <n.favrefelix@gmail.com> | 2011-08-19 18:33:08 +0400 |
---|---|---|
committer | Nicolas Favre-Felix <n.favrefelix@gmail.com> | 2011-08-19 18:33:08 +0400 |
commit | 5b0ffe2c07ab72ea55b969db2d2adb404c239e9e (patch) | |
tree | 5b80a75421630826af68838404bc03103e9afce6 | |
parent | cc72eef80b683adc210613d7216d4bb0a046e192 (diff) |
Added rehashing test.
-rw-r--r-- | tests/array-rehash.php | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/array-rehash.php b/tests/array-rehash.php new file mode 100644 index 00000000..b7ea7a84 --- /dev/null +++ b/tests/array-rehash.php @@ -0,0 +1,74 @@ +<?php + +$firstRing = array('localhost:6379', 'localhost:6380', 'localhost:6381'); +$secondRing = array('localhost:6379', 'localhost:6380', 'localhost:6381', 'localhost:6382'); + +// prepare by flushing db +foreach($secondRing as $s) { + list($host, $port) = explode(':', $s); + + $r = new Redis; + $r->connect($host, (int)$port); + $r->flushdb(); +} + +function report_info($ra) { + $infos = $ra->info(); + foreach($infos as $host => $info) { + echo "Host $host: ".(isset($info['db0'])?$info['db0']:0)."\n"; + } +} + +$n = 10000; +$data = array(); +for($i = 0; $i < $n; $i++) { + $tmp = $i; //rand(); + $data['key-'.$tmp] = 'val-'.$tmp; +} + +echo "Distributing $n keys around the ring.\n"; +// first, distribute the keys to the original ring +$ra = new RedisArray($firstRing, NULL, array(), TRUE); +foreach($data as $k => $v) { + $ra->set($k, $v); +} +report_info($ra); + +echo "Reading back all the values.\n"; +foreach($data as $k => $v) { + $ret = $ra->get($k); + if($ret !== $v) { + echo "Could not match expected value $v for key $k, instead got:"; var_dump($ret); + die; + } +} +echo "All key/value pairs read successfuly.\n"; + +echo "Creating a new, larger ring and reading back all the values.\n"; +$ra = new RedisArray($secondRing, NULL, $firstRing, TRUE); +foreach($data as $k => $v) { + $ret = $ra->get($k); + if($ret !== $v) { + echo "Could not match expected value $v for key $k, instead got:"; var_dump($ret); + die; + } +} +echo "All key/value pairs read successfuly using the new setup with a fallback\n"; +report_info($ra); + +echo "Rehash array.\n"; +$ra->_rehash(); + +echo "Creating a new ring without fallback and reading back all the values.\n"; +$ra = new RedisArray($secondRing); +foreach($data as $k => $v) { + $ret = $ra->get($k); + if($ret !== $v) { + echo "Could not match expected value $v for key $k, instead got:"; var_dump($ret); + die; + } +} +echo "All key/value pairs read successfuly using the new setup without a fallback\n"; +report_info($ra); + +?> |