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