test_service.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "service.h"
  2. // A test service.
  3. //
  4. // This service can be induced to successfully start or fail (once it is STARTING) by calling either the
  5. // started() or failed_to_start() functions.
  6. //
  7. class test_service : public service_record
  8. {
  9. public:
  10. bool bring_up_reqd = false;
  11. bool start_interruptible = false;
  12. test_service(service_set *set, std::string name, service_type_t type_p,
  13. const std::list<prelim_dep> &deplist_p)
  14. : service_record(set, name, type_p, deplist_p)
  15. {
  16. }
  17. bool auto_stop = true; // whether to call stopped() immediately from bring_down()
  18. // Do any post-dependency startup; return false on failure
  19. virtual bool bring_up() noexcept override
  20. {
  21. // return service_record::bring_up();
  22. bring_up_reqd = true;
  23. return true;
  24. }
  25. // All dependents have stopped.
  26. virtual void bring_down() noexcept override
  27. {
  28. waiting_for_deps = false;
  29. if (auto_stop) {
  30. stopped();
  31. }
  32. }
  33. void stopped() noexcept
  34. {
  35. assert(get_state() != service_state_t::STOPPED);
  36. service_record::stopped();
  37. }
  38. // Whether a STARTING service can immediately transition to STOPPED (as opposed to
  39. // having to wait for it reach STARTED and then go through STOPPING).
  40. virtual bool can_interrupt_start() noexcept override
  41. {
  42. return waiting_for_deps || start_interruptible;
  43. }
  44. virtual bool interrupt_start() noexcept override
  45. {
  46. return true;
  47. }
  48. void started() noexcept
  49. {
  50. assert(bring_up_reqd);
  51. service_record::started();
  52. }
  53. void failed_to_start() noexcept
  54. {
  55. service_record::failed_to_start();
  56. }
  57. };