143-thread-shutdown_flag.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // Copyright (c) 2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  3. //
  4. // Distributed under the MIT Software License
  5. //
  6. #ifdef __unix__
  7. #include "rotor.hpp"
  8. #include "rotor/thread.hpp"
  9. #include "actor_test.h"
  10. #include "supervisor_test.h"
  11. #include "system_context_test.h"
  12. #include "access.h"
  13. #include <unistd.h>
  14. #include <signal.h>
  15. #include <atomic>
  16. namespace r = rotor;
  17. namespace rt = r::test;
  18. namespace rth = rotor::thread;
  19. namespace payload {
  20. struct sample_payload_t {};
  21. } // namespace payload
  22. namespace message {
  23. using sample_payload_t = r::message_t<payload::sample_payload_t>;
  24. }
  25. std::atomic_bool my_flag{false};
  26. TEST_CASE("throw in factory", "[spawner]") {
  27. auto system_context = rth::system_context_thread_t();
  28. auto timeout = r::pt::milliseconds{100};
  29. auto sup = system_context.create_supervisor<rth::supervisor_thread_t>()
  30. .timeout(timeout)
  31. .shutdown_flag(my_flag, r::pt::millisec{1})
  32. .finish();
  33. auto r = signal(SIGALRM, [](int) { my_flag = true; });
  34. REQUIRE(r != SIG_ERR);
  35. alarm(1);
  36. sup->start();
  37. system_context.run();
  38. CHECK(my_flag == true);
  39. REQUIRE(static_cast<r::actor_base_t *>(sup.get())->access<rt::to::state>() == r::state_t::SHUT_DOWN);
  40. }
  41. #endif