unit testing for CTRL-C sent to an application

删除回忆录丶 提交于 2019-12-01 14:48:18

问题


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


回答1:


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.




回答2:


Introduce a level of indirection.

  1. Place your high-level program code behind a Facade (I use a class named Program).
  2. Have that Facade provide a shutdown() method, which performs all of the shutdown operation except calling std::exit().
  3. Unit test that shutdown() method as you would any other method.
  4. Have the signal handler delegate to that shutdown() method for the static Program object that represents your entire program then call std::exit(). This is the only part you can not unit test.


来源:https://stackoverflow.com/questions/4500794/unit-testing-for-ctrl-c-sent-to-an-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!