我正在开发一个处理 CTRL-C 的应用程序。我正在生成一个信号处理程序来正常关闭线程和其他资源。
我想在我的应用程序可能存在的不同场景中测试 CTRL-C 。我知道如何为被测进程设置这些,但我需要一种方法(在正在运行的测试套件的代码中)来检查是否达到该条件以准确调用 CTRL-C。
我在 Linux 中工作,我想在 CPPUNIT
的帮助下自动运行我的测试。在每个 CTRL-C 测试中,我启动该过程,然后使用 CTRL-C >kill
函数具有进程的 PID。
我正在使用共享内存;一旦测试的应用程序达到我感兴趣的条件或我想要发送 CTRL-C 的点,我就会将标签或状态写入共享内存。同时,在不同进程中运行的测试套件代码不断轮询共享内存,一旦读取到所需状态,就会发送 CTRL-C/kill。
您认为这是一个好方法还是通常以更好/有效的方式完成?
亲切的问候
AFG
I am developing an application handling CTRL-C. I am producing a signal handler to shut-down gracefully threads and other resources.
I want to test CTRL-C in different scenarios where my application might be. I know how to setup those for the process under test, but I need a way (in the code of the running test suite) to check whether that condition is reached or not to call exactly CTRL-C.
I work in Linux and I want to run my tests automatically with the help of CPPUNIT
. In each of my CTRL-C tests I start the process and then I send CTRL-C using kill
function having the PID of the process.
I am using shared memory; once the tested application reaches a condition of my interest or a point when I would like to send CTRL-C, I write a tag or a state into the shared memory. Aat the same time the test suite code running in a different process is continuosly polling the shared memory and once it reads the desired state it send CTRL-C/kill.
Do you think is a good approach or it is usually done in better/effective ways?
Kind Regards
AFG
发布评论
评论(2)
首先测试接收到某些外部信号时的行为看起来不像单元测试,而是像功能测试。
此外,您执行此操作的方式听起来也太复杂,并且可能会强制执行某种同步并隐藏某些行为。
另一方面,对于此类测试,我确实没有更好的建议,这通常是由外部工具以一种不太受控制的方式完成的。
First testing the behavior when some external signal is received does not look like unit testing but like functional testing.
Also, the way you do it also sound too complicated and is likely force some kind of synchronization and hide some behaviors.
On the other hand I do not really have something better to suggest for this kind of tests, this is usually done by external tools in a much less controled way.
引入一定程度的间接性。
Program
的类)。shutdown()
方法进行单元测试。static
Program
对象的shutdown()
方法,然后调用 std::退出()。这是唯一不能进行单元测试的部分。
Introduce a level of indirection.
Program
).shutdown()
method, which performs all of the shutdown operation except callingstd::exit()
.shutdown()
method as you would any other method.shutdown()
method for thestatic
Program
object that represents your entire program thencall std::exit()
. This is the only part you can not unit test.