unit testing for CTRL-C sent to an application

后端 未结 2 655
庸人自扰
庸人自扰 2021-01-17 21:10

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

2条回答
  •  再見小時候
    2021-01-17 21:56

    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.

提交回复
热议问题