Skip to content

Commit

Permalink
Added tests, improved server stats_file, fix core-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Oct 8, 2021
1 parent bf393fb commit 73372ad
Show file tree
Hide file tree
Showing 7 changed files with 1,194 additions and 51 deletions.
1,094 changes: 1,050 additions & 44 deletions ext-src/php_swoole_library.h

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/memory/fixed_pool.cc
Expand Up @@ -82,13 +82,13 @@ FixedPool::FixedPool(uint32_t slice_size, void *memory, size_t size, bool shared
impl = (FixedPoolImpl*) memory;
memory = (char*) memory + sizeof(*impl);
sw_memset_zero(impl, sizeof(*impl));
impl->shared = shared;
impl->slice_size = slice_size;
impl->size = size - sizeof(*impl);
uint32_t slice_num = impl->size / (slice_size + sizeof(FixedPoolSlice));
if (slice_num < 2) {
throw Exception(SW_ERROR_INVALID_PARAMS);
}
impl->shared = shared;
impl->slice_size = slice_size;
impl->size = size - sizeof(*impl);
impl->slice_num = slice_num;
impl->memory = memory;
impl->allocated = false;
Expand Down
7 changes: 6 additions & 1 deletion tests/include/functions.php
Expand Up @@ -798,4 +798,9 @@ function swoole_get_variance($avg, $array, $is_swatch = false)
function swoole_get_average($array)
{
return array_sum($array) / count($array);
}
}

function assert_server_stats($stats) {
Assert::keyExists($stats, 'connection_num');
Assert::keyExists($stats, 'request_count');
}
10 changes: 7 additions & 3 deletions tests/swoole_server/stats_file.phpt
Expand Up @@ -9,9 +9,12 @@ require __DIR__ . '/../include/bootstrap.php';
use function Swoole\Coroutine\run;

const STATS_FILE = __DIR__ . '/stats.log';
if (is_file(STATS_FILE)) {
unlink(STATS_FILE);
}
$rm_fn = function () {
if (is_file(STATS_FILE)) {
unlink(STATS_FILE);
}
};
$rm_fn();

$pm = new ProcessManager;
$pm->initFreePorts(1);
Expand Down Expand Up @@ -59,5 +62,6 @@ $pm->childFunc = function () use ($pm) {

$pm->childFirst();
$pm->run();
$rm_fn();
?>
--EXPECT--
62 changes: 62 additions & 0 deletions tests/swoole_server/stats_file_json.phpt
@@ -0,0 +1,62 @@
--TEST--
swoole_server: stats_file [json]
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use function Swoole\Coroutine\run;

const STATS_FILE = __DIR__ . '/stats.json';
$rm_fn = function () {
if (is_file(STATS_FILE)) {
unlink(STATS_FILE);
}
};
$rm_fn();

$pm = new ProcessManager;
$pm->initFreePorts(1);

$pm->parentFunc = function ($pid) use ($pm) {
run(function () use ($pm, $pid) {
httpRequest('http://127.0.0.1:' . $pm->getFreePort(0));
for ($i = 0; $i < 4; ++$i) {
Co::sleep(0.5);
$content = @file_get_contents(STATS_FILE);
if ('' != $content) {
$stats = json_decode($content, true);
assert_server_stats($stats);
break;
}
}
});
echo "\n";
$pm->kill();
};

$pm->childFunc = function () use ($pm) {
$mode = SERVER_MODE_RANDOM;
$worker_num = rand(1, 4);
phpt_var_dump("mode: $mode\nworker_num: $worker_num\n");
$server = new Swoole\Http\Server('127.0.0.1', $pm->getFreePort(0), $mode);
$server->set([
'stats_file' => STATS_FILE,
'log_file' => DEV_NULL,
'worker_num' => $worker_num,
]);
$server->on('ManagerStart', function ($serv) use ($pm) {
$pm->wakeup();
});
$server->on('request', function ($request, $response) {
$response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>");
});
$server->start();
};

$pm->childFirst();
$pm->run();
$rm_fn();
?>
--EXPECT--
65 changes: 65 additions & 0 deletions tests/swoole_server/stats_file_php.phpt
@@ -0,0 +1,65 @@
--TEST--
swoole_server: stats_file [php]
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use function Swoole\Coroutine\run;

const STATS_FILE = __DIR__ . '/stats.php';
$rm_fn = function () {
if (is_file(STATS_FILE)) {
unlink(STATS_FILE);
}
};
$rm_fn();

$pm = new ProcessManager;
$pm->initFreePorts(1);

$pm->parentFunc = function ($pid) use ($pm) {
run(function () use ($pm, $pid) {
httpRequest('http://127.0.0.1:' . $pm->getFreePort(0));
for ($i = 0; $i < 4; ++$i) {
Co::sleep(0.5);
if (!is_file(STATS_FILE)) {
continue;
}
$stats = include STATS_FILE;
if (empty($stats)) {
assert_server_stats($stats);
break;
}
}
});
echo "\n";
$pm->kill();
};

$pm->childFunc = function () use ($pm) {
$mode = SERVER_MODE_RANDOM;
$worker_num = rand(1, 4);
phpt_var_dump("mode: $mode\nworker_num: $worker_num\n");
$server = new Swoole\Http\Server('127.0.0.1', $pm->getFreePort(0), $mode);
$server->set([
'stats_file' => STATS_FILE,
'log_file' => DEV_NULL,
'worker_num' => $worker_num,
]);
$server->on('ManagerStart', function ($serv) use ($pm) {
$pm->wakeup();
});
$server->on('request', function ($request, $response) {
$response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>");
});
$server->start();
};

$pm->childFirst();
$pm->run();

$rm_fn();
?>
--EXPECT--
1 change: 1 addition & 0 deletions tools/build-library.php
Expand Up @@ -85,6 +85,7 @@
# <core for Process> #
'core/Process/Manager.php',
# <core for Server> #
'core/Server/Admin.php',
'core/Server/Helper.php',
# <core for functions> #
'core/Coroutine/functions.php',
Expand Down

0 comments on commit 73372ad

Please sign in to comment.