interrupt_signals_ut.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "interrupt_signals.h"
  2. #include <util/datetime/base.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <atomic>
  5. #ifdef _win_
  6. #include <windows.h>
  7. #endif
  8. Y_UNIT_TEST_SUITE(TTestInterruptSignals) {
  9. static std::atomic<size_t> HandledSigNum = 0;
  10. static void Handler(int signum) {
  11. HandledSigNum.store(signum);
  12. }
  13. Y_UNIT_TEST(Test1) {
  14. SetInterruptSignalsHandler(Handler);
  15. #ifdef _win_
  16. // TODO: unfortunately GenerateConsoleCtrlEvent fails under Wine
  17. /*
  18. for (auto [winEvent, posixSigNum] : {
  19. std::make_pair(CTRL_C_EVENT, SIGINT),
  20. std::make_pair(CTRL_BREAK_EVENT, SIGTERM)
  21. })
  22. {
  23. if (!GenerateConsoleCtrlEvent(winEvent, 0)) {
  24. UNIT_FAIL("GenerateConsoleCtrlEvent failed: " << LastSystemErrorText());
  25. }
  26. Sleep(TDuration::MilliSeconds(100));
  27. UNIT_ASSERT_VALUES_EQUAL(HandledSigNum.load(), posixSigNum);
  28. }
  29. */
  30. for (int signum : {SIGINT, SIGTERM}) {
  31. #else
  32. for (int signum : {SIGINT, SIGTERM, SIGHUP}) {
  33. #endif
  34. std::raise(signum);
  35. Sleep(TDuration::MilliSeconds(100)); // give it time to handle an async signal
  36. UNIT_ASSERT_VALUES_EQUAL(HandledSigNum.load(), signum);
  37. }
  38. }
  39. } // Y_UNIT_TEST_SUITE(TTestInterruptSignals)