unit testing for CTRL-C sent to an application
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.
Introduce a level of indirection.
- Place your high-level program code behind a Facade (I use a class named
Program
). - Have that Facade provide a
shutdown()
method, which performs all of the shutdown operation except callingstd::exit()
. - Unit test that
shutdown()
method as you would any other method. - Have the signal handler delegate to that
shutdown()
method for thestatic
Program
object that represents your entire program thencall std::exit()
. This is the only part you can not unit test.