@@ -24,14 +24,11 @@ fn main() -> Result<()> {
2424create and start service
2525``` rust
2626use systemd_client :: {
27- build_blocking_client,
28- create_unit_configuration_file,
29- manager :: blocking :: OrgFreedesktopSystemd1Manager ,
30- Result ,
31- ServiceConfiguration ,
32- ServiceUnitConfiguration ,
33- SystemdObjectType ,
34- UnitConfiguration ,
27+ build_blocking_client, create_unit_configuration_file,
28+ manager :: blocking :: OrgFreedesktopSystemd1Manager , models :: IntoModel ,
29+ unit :: blocking :: UnitProperties , Result , ServiceConfiguration , ServiceUnitConfiguration ,
30+ SystemdObjectType , UnitActiveStateType , UnitConfiguration , UnitLoadStateType , UnitProps ,
31+ UnitSubStateType ,
3532};
3633
3734/*
@@ -43,7 +40,7 @@ use systemd_client::{
4340 */
4441fn main () -> Result <()> {
4542 let unit_builder = UnitConfiguration :: builder (). description (" test service" );
46- let svc_builder = ServiceConfiguration :: builder (). exec_start (vec! [" /bin/sleep" , " 10 " ]);
43+ let svc_builder = ServiceConfiguration :: builder (). exec_start (vec! [" /bin/sleep" , " 3 " ]);
4744 let svc_unit = ServiceUnitConfiguration :: builder ()
4845 . unit (unit_builder )
4946 . service (svc_builder )
@@ -56,6 +53,22 @@ fn main() -> Result<()> {
5653 println! (" {}" , job_path );
5754 let svc_unit_path = client . get_unit (" test.service" )? ;
5855 println! (" {}" , svc_unit_path );
56+ // verify unit state given unit path
57+ let client = build_blocking_client (SystemdObjectType :: Unit (svc_unit_path ))? ;
58+ let unit_props = client . get_unit_properties ()? ;
59+ let unit_props : UnitProps = unit_props . into_model ()? ;
60+ println! (" {:?}" , unit_props );
61+ assert_eq! (unit_props . load_state, UnitLoadStateType :: Loaded );
62+ assert_eq! (unit_props . active_state, UnitActiveStateType :: Active );
63+ assert_eq! (unit_props . sub_state, UnitSubStateType :: Running );
64+ std :: thread :: sleep (std :: time :: Duration :: from_secs (4 ));
65+ // service should exit after 3 sec
66+ let unit_props = client . get_unit_properties ()? ;
67+ let unit_props : UnitProps = unit_props . into_model ()? ;
68+ println! (" {:?}" , unit_props );
69+ assert_eq! (unit_props . load_state, UnitLoadStateType :: Loaded );
70+ assert_eq! (unit_props . active_state, UnitActiveStateType :: Inactive );
71+ assert_eq! (unit_props . sub_state, UnitSubStateType :: Dead );
5972 Ok (())
6073}
6174```
@@ -85,14 +98,11 @@ pub async fn main() -> Result<()> {
8598create and start service
8699``` rust
87100use systemd_client :: {
88- build_nonblock_client,
89- create_unit_configuration_file,
90- manager :: nonblock :: OrgFreedesktopSystemd1Manager ,
91- Result ,
92- ServiceConfiguration ,
93- ServiceUnitConfiguration ,
94- SystemdObjectType ,
95- UnitConfiguration ,
101+ build_nonblock_client, create_unit_configuration_file,
102+ manager :: nonblock :: OrgFreedesktopSystemd1Manager , models :: IntoModel ,
103+ unit :: nonblock :: UnitProperties , Result , ServiceConfiguration , ServiceUnitConfiguration ,
104+ SystemdObjectType , UnitActiveStateType , UnitConfiguration , UnitLoadStateType , UnitProps ,
105+ UnitSubStateType ,
96106};
97107
98108/*
@@ -105,7 +115,7 @@ use systemd_client::{
105115#[tokio:: main]
106116async fn main () -> Result <()> {
107117 let unit_builder = UnitConfiguration :: builder (). description (" test service" );
108- let svc_builder = ServiceConfiguration :: builder (). exec_start (vec! [" /bin/sleep" , " 10 " ]);
118+ let svc_builder = ServiceConfiguration :: builder (). exec_start (vec! [" /bin/sleep" , " 3 " ]);
109119 let svc_unit = ServiceUnitConfiguration :: builder ()
110120 . unit (unit_builder )
111121 . service (svc_builder )
@@ -120,6 +130,24 @@ async fn main() -> Result<()> {
120130 println! (" {}" , svc_unit_path );
121131 // close connection
122132 jh . abort ();
133+ // verify unit state given unit path
134+ let (client , jh ) = build_nonblock_client (SystemdObjectType :: Unit (svc_unit_path ))? ;
135+ let unit_props = client . get_unit_properties (). await ? ;
136+ let unit_props : UnitProps = unit_props . into_model ()? ;
137+ println! (" {:?}" , unit_props );
138+ assert_eq! (unit_props . load_state, UnitLoadStateType :: Loaded );
139+ assert_eq! (unit_props . active_state, UnitActiveStateType :: Active );
140+ assert_eq! (unit_props . sub_state, UnitSubStateType :: Running );
141+ std :: thread :: sleep (std :: time :: Duration :: from_secs (4 ));
142+ // service should exit after 3 sec
143+ let unit_props = client . get_unit_properties (). await ? ;
144+ let unit_props : UnitProps = unit_props . into_model ()? ;
145+ println! (" {:?}" , unit_props );
146+ assert_eq! (unit_props . load_state, UnitLoadStateType :: Loaded );
147+ assert_eq! (unit_props . active_state, UnitActiveStateType :: Inactive );
148+ assert_eq! (unit_props . sub_state, UnitSubStateType :: Dead );
149+ // close connection
150+ jh . abort ();
123151 Ok (())
124152}
125153```
0 commit comments