@@ -50,16 +50,16 @@ impl Client {
5050 }
5151
5252 pub fn ping ( & self , idx : usize ) -> Result < bool , ClientError > {
53- if idx >= self . endpoints . len ( ) as usize {
53+ if idx >= self . endpoints . len ( ) {
5454 return Err ( ClientError :: ValueError ( "Index out of range" . to_string ( ) ) ) ;
5555 }
5656 let url: String = self . endpoints [ idx] . url . clone ( ) + URL_PING ;
5757 match reqwest:: blocking:: get ( url) {
5858 Ok ( response) => {
5959 if response. status ( ) . is_success ( ) {
60- return Ok ( true ) ;
60+ Ok ( true )
6161 } else {
62- return Ok ( false ) ;
62+ Ok ( false )
6363 }
6464 }
6565 Err ( _) => Err ( ClientError :: ConnectionError ) ,
@@ -82,10 +82,11 @@ impl Client {
8282 todo ! ( ) ;
8383 }
8484
85- fn get_server_url ( & self ) -> Option < String > {
85+ pub fn get_server_url ( & self ) -> Option < String > {
8686 let current_index = self . prev_idx . load ( Ordering :: SeqCst ) ;
8787 let endpoint_count = self . endpoints . len ( ) as i32 ;
88- let url = if endpoint_count > 0 {
88+
89+ if endpoint_count > 0 {
8990 let url = self . endpoints [ current_index as usize % endpoint_count as usize ]
9091 . url
9192 . clone ( ) ;
@@ -94,8 +95,7 @@ impl Client {
9495 Some ( url)
9596 } else {
9697 None
97- } ;
98- url
98+ }
9999 }
100100}
101101
@@ -111,79 +111,3 @@ pub fn build_endpoints(addresses: Vec<Address>) -> Vec<Endpoint> {
111111 } )
112112 . collect ( )
113113}
114-
115- #[ cfg( test) ]
116- mod tests {
117- use std:: time:: Duration ;
118-
119- use crate :: config:: { AuthConfig , BatchConfig } ;
120-
121- use super :: * ;
122-
123- fn create_test_config ( ) -> Config {
124- Config {
125- address : vec ! [ Address {
126- host: "127.0.0.1" . to_string( ) ,
127- port: 8086 ,
128- } ] ,
129- batch_config : BatchConfig {
130- batch_interval : Duration :: from_secs ( 30 ) ,
131- batch_size : 100 ,
132- } ,
133- timeout : Duration :: from_secs ( 30 ) ,
134- connect_timeout : Duration :: from_secs ( 10 ) ,
135- gzip_enabled : true ,
136- auth_config : AuthConfig {
137- username : "user" . to_string ( ) ,
138- password : "password" . to_string ( ) ,
139- token : None ,
140- auth_type : 1 ,
141- } ,
142- }
143- }
144-
145- #[ test]
146- fn test_get_server_url ( ) {
147- let addresses = vec ! [
148- Address {
149- host: "127.0.0.1" . to_string( ) ,
150- port: 8086 ,
151- } ,
152- Address {
153- host: "127.0.0.2" . to_string( ) ,
154- port: 8087 ,
155- } ,
156- ] ;
157- let mut config = create_test_config ( ) ;
158-
159- config. address = addresses;
160-
161- let client = Client :: new ( & config) ;
162-
163- let url1 = client. get_server_url ( ) ;
164- let url2 = client. get_server_url ( ) ;
165-
166- assert ! ( url1. is_some( ) ) ;
167- assert ! ( url2. is_some( ) ) ;
168- assert_ne ! ( url1, url2) ;
169- }
170-
171- /// Tests the `ping` method of the `Client` struct.
172- ///
173- /// This test sets up a `Client` with a single address and checks if the `ping` method
174- /// returns `Ok(true)` when the server is reachable.
175- ///
176- /// Before running this test, make sure to start the server using the following Docker command:
177- /// ```sh
178- /// docker run -p 8086:8086 --name opengemini --rm opengeminidb/opengemini-server
179- /// ```
180- #[ test]
181- fn test_ping_success ( ) {
182- let config = create_test_config ( ) ;
183- let client = Client :: new ( & config) ;
184-
185- let result = client. ping ( 0 ) ;
186- assert ! ( result. is_ok( ) ) ;
187- assert_eq ! ( result. unwrap( ) , true ) ;
188- }
189- }
0 commit comments