unit-testing - 如何对基本功能进行单元测试?
问题描述
我有一个非常有趣的测试场景..
使用线程的系统的核心是一个非类函数。为了向这个函数添加单元测试,我需要这个函数来工作并模拟它包含的类。
函数如下所示:
include_once(__DIR__."/../core/debug.php");
...(A lot of include_once)
/*
* Run Task
* This is a PHP script that is ran from the command line.
* It runs the entire time
*/
ini_set("serialize_precision", -1);
$settings = settings::getInstance();
$settings->load("main");
new debug();
$db = db::getInstance();
$db->connect();
$mongo = mongo::getInstance();
$mongo->connect();
putenv("RUNNING_AS_CLI=1");
\publish\Log::notice("RunTask","Starting command line publish tasks");
putenv("GRACEFULL_EXIT=0");
function gracefullExit($signo, $signinfo){
echo "Executing gracefull exit... \n";
putenv("GRACEFULL_EXIT=1");
}
pcntl_async_signals(true);
pcntl_signal(SIGTERM, "gracefullExit");
pcntl_signal(SIGINT, "gracefullExit");
pcntl_signal(SIGHUP, "gracefullExit");
pcntl_signal(SIGQUIT, "gracefullExit");
pcntl_signal(SIGABRT, "gracefullExit");
$stackCounter = 0;
while(true) {
try {
// Run task
\publish\JobEngine::runTask();
} catch (Exception $e) {
error_log($e);
}
if (getenv("GRACEFULL_EXIT") == "1"){
echo "Exiting now...\n";
exit(0);
}
// Sleep 300ms
usleep( 300 * 1000 );
$stackCounter++;
if ($stackCounter > 1000){
// If we runned this more than 1000 times (5 minutes if the thread is idle) restart this thread
exit(0);
}
}
这是一个有点复杂的系统。知道如何测试这样的功能吗?
解决方案
推荐阅读
- java - 错误:发生 JNI 错误,请检查您的安装
- c# - 位图中的步幅是什么意思?带有注释的小颜色选择器代码,指定有问题的行
- mysql - 如何更新 SQLITE 中两列匹配条件的列数据?
- python - 如何将经过身份验证的用户设置为序列化程序?
- r - R - 将两个具有特殊条件的电子表格合并在一起
- vba - 下一行继续字符串
- react-native - 使用 React-Native App 通过蓝牙发送文本数据
- ffmpeg - 有没有办法告诉 ffmpeg 加速视频,使其持续时间与音频的持续时间相匹配?
- parallel-processing - 如何使用 pararrayfun 以八度音阶并行化 for 循环(或任何其他函数也可以)?
- terminal - 为了在 Windows 10 命令提示符下启用时间命令,我必须做什么?