15
15
mod protocols;
16
16
mod utils;
17
17
18
+ use log:: LevelFilter ;
18
19
use protocols:: sync:: { agent, agent_ttrpc, health, health_ttrpc} ;
19
20
use std:: thread;
20
21
use ttrpc:: context:: { self , Context } ;
22
+ use ttrpc:: error:: Error ;
23
+ use ttrpc:: proto:: Code ;
21
24
use ttrpc:: Client ;
22
25
23
26
fn main ( ) {
27
+ simple_logging:: log_to_stderr ( LevelFilter :: Trace ) ;
28
+
24
29
let c = Client :: connect ( utils:: SOCK_ADDR ) . unwrap ( ) ;
25
30
let hc = health_ttrpc:: HealthClient :: new ( c. clone ( ) ) ;
26
31
let ac = agent_ttrpc:: AgentServiceClient :: new ( c) ;
@@ -33,69 +38,97 @@ fn main() {
33
38
let t = thread:: spawn ( move || {
34
39
let req = health:: CheckRequest :: new ( ) ;
35
40
println ! (
36
- "OS Thread {:?} - {} started: {:?}" ,
41
+ "OS Thread {:?} - health.check() started: {:?}" ,
37
42
std:: thread:: current( ) . id( ) ,
38
- "health.check()" ,
39
43
now. elapsed( ) ,
40
44
) ;
45
+
46
+ let rsp = thc. check ( default_ctx ( ) , & req) ;
47
+ match rsp. as_ref ( ) {
48
+ Err ( Error :: RpcStatus ( s) ) => {
49
+ assert_eq ! ( Code :: NOT_FOUND , s. code( ) ) ;
50
+ assert_eq ! ( "Just for fun" . to_string( ) , s. message( ) )
51
+ }
52
+ Err ( e) => {
53
+ panic ! ( "not expecting an error from the example server: {:?}" , e)
54
+ }
55
+ Ok ( x) => {
56
+ panic ! ( "not expecting a OK response from the example server: {:?}" , x)
57
+ }
58
+ }
41
59
println ! (
42
- "OS Thread {:?} - {} -> {:?} ended: {:?}" ,
60
+ "OS Thread {:?} - health.check() -> {:?} ended: {:?}" ,
43
61
std:: thread:: current( ) . id( ) ,
44
- "health.check()" ,
45
- thc. check( default_ctx( ) , & req) ,
62
+ rsp,
46
63
now. elapsed( ) ,
47
64
) ;
48
65
} ) ;
49
66
50
67
let t2 = thread:: spawn ( move || {
51
68
println ! (
52
- "OS Thread {:?} - {} started: {:?}" ,
69
+ "OS Thread {:?} - agent.list_interfaces() started: {:?}" ,
53
70
std:: thread:: current( ) . id( ) ,
54
- "agent.list_interfaces()" ,
55
71
now. elapsed( ) ,
56
72
) ;
57
73
58
74
let show = match tac. list_interfaces ( default_ctx ( ) , & agent:: ListInterfacesRequest :: new ( ) ) {
59
- Err ( e) => format ! ( "{:?}" , e) ,
60
- Ok ( s) => format ! ( "{:?}" , s) ,
75
+ Err ( e) => {
76
+ panic ! ( "not expecting an error from the example server: {:?}" , e)
77
+ }
78
+ Ok ( s) => {
79
+ assert_eq ! ( "first" . to_string( ) , s. Interfaces [ 0 ] . name) ;
80
+ assert_eq ! ( "second" . to_string( ) , s. Interfaces [ 1 ] . name) ;
81
+ format ! ( "{s:?}" )
82
+ }
61
83
} ;
62
84
63
85
println ! (
64
- "OS Thread {:?} - {} -> {} ended: {:?}" ,
86
+ "OS Thread {:?} - agent.list_interfaces() -> {} ended: {:?}" ,
65
87
std:: thread:: current( ) . id( ) ,
66
- "agent.list_interfaces()" ,
67
88
show,
68
89
now. elapsed( ) ,
69
90
) ;
70
91
} ) ;
71
92
72
93
println ! (
73
- "Main OS Thread - {} started: {:?}" ,
74
- "agent.online_cpu_mem()" ,
94
+ "Main OS Thread - agent.online_cpu_mem() started: {:?}" ,
75
95
now. elapsed( )
76
96
) ;
77
97
let show = match ac. online_cpu_mem ( default_ctx ( ) , & agent:: OnlineCPUMemRequest :: new ( ) ) {
78
- Err ( e) => format ! ( "{:?}" , e) ,
79
- Ok ( s) => format ! ( "{:?}" , s) ,
98
+ Err ( Error :: RpcStatus ( s) ) => {
99
+ assert_eq ! ( Code :: NOT_FOUND , s. code( ) ) ;
100
+ assert_eq ! (
101
+ "/grpc.AgentService/OnlineCPUMem is not supported" . to_string( ) ,
102
+ s. message( )
103
+ ) ;
104
+ format ! ( "{s:?}" )
105
+ }
106
+ Err ( e) => {
107
+ panic ! ( "not expecting an error from the example server: {:?}" , e)
108
+ }
109
+ Ok ( s) => {
110
+ panic ! ( "not expecting a OK response from the example server: {:?}" , s)
111
+ }
80
112
} ;
81
113
println ! (
82
- "Main OS Thread - {} -> {} ended: {:?}" ,
83
- "agent.online_cpu_mem()" ,
114
+ "Main OS Thread - agent.online_cpu_mem() -> {} ended: {:?}" ,
84
115
show,
85
116
now. elapsed( )
86
117
) ;
87
118
88
119
println ! ( "\n sleep 2 seconds ...\n " ) ;
89
120
thread:: sleep ( std:: time:: Duration :: from_secs ( 2 ) ) ;
121
+
122
+ let version = hc. version ( default_ctx ( ) , & health:: CheckRequest :: new ( ) ) ;
123
+ assert_eq ! ( "mock.0.1" , version. as_ref( ) . unwrap( ) . agent_version. as_str( ) ) ;
124
+ assert_eq ! ( "0.0.1" , version. as_ref( ) . unwrap( ) . grpc_version. as_str( ) ) ;
90
125
println ! (
91
- "Main OS Thread - {} started: {:?}" ,
92
- "health.version()" ,
126
+ "Main OS Thread - health.version() started: {:?}" ,
93
127
now. elapsed( )
94
128
) ;
95
129
println ! (
96
- "Main OS Thread - {} -> {:?} ended: {:?}" ,
97
- "health.version()" ,
98
- hc. version( default_ctx( ) , & health:: CheckRequest :: new( ) ) ,
130
+ "Main OS Thread - health.version() -> {:?} ended: {:?}" ,
131
+ version,
99
132
now. elapsed( )
100
133
) ;
101
134
0 commit comments