对发送到应用程序的CTRL-C进行单元测试

时间:2021-10-20 22:42:51

I am developing an application handling CTRL-C. I am producing a signal handler to shut-down gracefully threads and other resources.

我正在开发一个处理CTRL-C的应用程序。我正在生成一个信号处理程序来优雅地关闭线程和其他资源。

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.

我想在应用程序可能位于的不同场景中测试CTRL-C。我知道如何为被测试的进程设置这些条件,但是我需要一种方法(在运行的测试套件的代码中)来检查是否达到了该条件,以准确地调用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.

我在Linux工作,我想在CPPUNIT的帮助下自动运行我的测试。在我的每一个CTRL-C测试中,我启动了这个过程,然后我使用具有进程的PID的kill函数来发送CTRL-C。

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.

我在使用共享内存;一旦被测试的应用程序达到我感兴趣的条件或我想发送CTRL-C时的一个点,我将一个标记或一个状态写到共享内存中。与此同时,在不同进程中运行的测试套件代码将继续轮询共享内存,一旦它读取所需的状态,就会发送CTRL-C/kill。

Do you think is a good approach or it is usually done in better/effective ways?

你认为这是一种好方法还是用更好的/有效的方法来做?

Kind Regards

亲切的问候

AFG

二*度陀螺仪

2 个解决方案

#1


5  

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


2  

Introduce a level of indirection.

引入一个间接层次。

  1. Place your high-level program code behind a Facade (I use a class named Program).
  2. 将高级程序代码放在Facade的后面(我使用一个名为program的类)。
  3. Have that Facade provide a shutdown() method, which performs all of the shutdown operation except calling std::exit().
  4. 让Facade提供一个shutdown()方法,该方法执行除调用std::exit()之外的所有关机操作。
  5. Unit test that shutdown() method as you would any other method.
  6. 像其他方法一样,单元测试关闭()方法。
  7. 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.
  8. 让代表整个程序的静态程序对象的shutdown()方法的信号处理程序委托,然后调用std::exit()。这是唯一不能进行单元测试的部分。

#1


5  

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


2  

Introduce a level of indirection.

引入一个间接层次。

  1. Place your high-level program code behind a Facade (I use a class named Program).
  2. 将高级程序代码放在Facade的后面(我使用一个名为program的类)。
  3. Have that Facade provide a shutdown() method, which performs all of the shutdown operation except calling std::exit().
  4. 让Facade提供一个shutdown()方法,该方法执行除调用std::exit()之外的所有关机操作。
  5. Unit test that shutdown() method as you would any other method.
  6. 像其他方法一样,单元测试关闭()方法。
  7. 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.
  8. 让代表整个程序的静态程序对象的shutdown()方法的信号处理程序委托,然后调用std::exit()。这是唯一不能进行单元测试的部分。