Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wazsmwazsm committed Jul 24, 2018
1 parent dc7fc4e commit 78c9bb9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
12 changes: 5 additions & 7 deletions src/WorkerF/Http/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,28 +234,26 @@ protected static function _variablePathParse($path, $method)
*/
protected static function _variableRouteCacheControl($value)
{
// get route cache index count
$count = count(self::$_variable_route_cache_index);
// is value in cache index list?
if (FALSE !== ($index = array_search($value, self::$_variable_route_cache_index))) {
// if value is tail, do nothing
if ($index == ($count - 1)) {
if ($index == (count(self::$_variable_route_cache_index) - 1)) {
return;
}
// unset old value
unset(self::$_variable_route_cache_index[$index]);
// reset array index
self::$_variable_route_cache_index = array_values(self::$_variable_route_cache_index);
}
// push value to tail
array_push(self::$_variable_route_cache_index, $value);
// cache index list out of range?
if ($count >= self::$_variable_route_cache_limit) {
if (count(self::$_variable_route_cache_index) > self::$_variable_route_cache_limit) {
// remove head value
$remove_value = array_shift(self::$_variable_route_cache_index);
// unset route cache, free memory
unset(self::$_variable_route_cache[$remove_value]);
}
// push value to tail
array_push(self::$_variable_route_cache_index, $value);
}
}

/**
Expand Down
79 changes: 52 additions & 27 deletions tests/Http/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public static function getVariableRouteCacheIndex()
return self::$_variable_route_cache_index;
}

public static function setVariableRouteCacheIndex($arr)
{
self::$_variable_route_cache_index = $arr;
}

public static function getVariableRouteCacheLimit()
{
return self::$_variable_route_cache_limit;
Expand Down Expand Up @@ -644,33 +649,53 @@ public function testVariableCache()

public function testVariableRouteCacheControl()
{
// // data not out of range
// $route_cache = [];

// for ($i=0; $i < 65535; $i++) {
// $route_cache[$i] = 1;
// }

// RouteFake::setVariableRouteCache($route_cache);
// RouteFake::clearVariableRouteCache();

// $route_cache = RouteFake::getVariableRouteCache();

// $this->assertEquals(65535, count($route_cache));

// // data out of range
// $route_cache = [];

// for ($i=0; $i < 65536; $i++) {
// $route_cache[$i] = 1;
// }

// RouteFake::setVariableRouteCache($route_cache);
// RouteFake::clearVariableRouteCache();

// $route_cache = RouteFake::getVariableRouteCache();

// $this->assertEquals(0, count($route_cache));
// set cache limit
RouteFake::setVariableRouteCacheLimit(10);

/* data not out of range */

$route_cache_index = [9, 1, 5, 6, 7, 2];
RouteFake::setVariableRouteCacheIndex($route_cache_index);
RouteFake::setVariableRouteCache([9 => 'a', 7 => 'c', 1 => 'b']);

// value add
RouteFake::variableRouteCacheControl(15);
$route_cache_index = RouteFake::getVariableRouteCacheIndex();
$this->assertEquals([9, 1, 5, 6, 7, 2, 15], $route_cache_index);

// value hit
RouteFake::variableRouteCacheControl(1);
$route_cache_index = RouteFake::getVariableRouteCacheIndex();
$this->assertEquals([9, 5, 6, 7, 2, 15, 1], $route_cache_index);
RouteFake::variableRouteCacheControl(7);
$route_cache_index = RouteFake::getVariableRouteCacheIndex();
$this->assertEquals([9, 5, 6, 2, 15, 1, 7], $route_cache_index);
$route_cache = RouteFake::getVariableRouteCache();
$this->assertEquals([9 => 'a', 7 => 'c', 1 => 'b'], $route_cache);

/* data out of range */

$route_cache_index = [9, 1, 5, 6, 7, 2, 11, 3, 15, 4];
RouteFake::setVariableRouteCacheIndex($route_cache_index);
RouteFake::setVariableRouteCache([9 => 'a', 7 => 'c', 1 => 'b']);

// value add
RouteFake::variableRouteCacheControl(18);
$route_cache_index = RouteFake::getVariableRouteCacheIndex();
$route_cache = RouteFake::getVariableRouteCache();
$this->assertEquals([1, 5, 6, 7, 2, 11, 3, 15, 4, 18], $route_cache_index);
$this->assertEquals([7 => 'c', 1 => 'b'], $route_cache);
RouteFake::variableRouteCacheControl(16);
$route_cache_index = RouteFake::getVariableRouteCacheIndex();
$route_cache = RouteFake::getVariableRouteCache();
$this->assertEquals([5, 6, 7, 2, 11, 3, 15, 4, 18, 16], $route_cache_index);
$this->assertEquals([7 => 'c'], $route_cache);

// value hit
RouteFake::variableRouteCacheControl(2);
$route_cache_index = RouteFake::getVariableRouteCacheIndex();
$this->assertEquals([5, 6, 7, 11, 3, 15, 4, 18, 16, 2], $route_cache_index);
$this->assertEquals([7 => 'c'], $route_cache);
}

public function testSetVariableRouteCacheLimit()
Expand Down

0 comments on commit 78c9bb9

Please sign in to comment.