@@ -273,3 +273,68 @@ pub async fn metrics_loop(config: &'static IngestorConfig) {
273273 handle. abort ( ) ; // abort each task
274274 }
275275}
276+
277+ #[ cfg( test) ]
278+ mod tests {
279+ use std:: collections:: HashMap ;
280+
281+ use tokio:: spawn;
282+
283+ use crate :: {
284+ config:: { BasicAuth , MetricInterval , MetricsConfig , UrlPort } ,
285+ metrics:: consumer_loop,
286+ metrics_core:: prometheus:: { Label , Sample , TimeSeries } ,
287+ } ;
288+
289+ fn test_config ( url : String ) -> MetricsConfig {
290+ MetricsConfig {
291+ user_config_path : None ,
292+ auth : BasicAuth {
293+ username : "user" . into ( ) ,
294+ password : "pass" . into ( ) ,
295+ } ,
296+ url : url,
297+ intervals : HashMap :: new ( ) ,
298+ dynamic : HashMap :: new ( ) ,
299+ watchdog_interval : MetricInterval :: Daily ( 1 ) ,
300+ publish_interval : MetricInterval :: Millis ( 1 ) ,
301+ }
302+ }
303+
304+ #[ tokio:: test( flavor = "multi_thread" ) ]
305+ async fn test_consumer_loop ( ) {
306+ // Request a new server from the pool
307+ let mut server = mockito:: Server :: new_async ( ) . await ;
308+
309+ // Use one of these addresses to configure your client
310+ let host = server. host_with_port ( ) ;
311+ let url = server. url ( ) ;
312+ let config = test_config ( url) ;
313+ // Create a mock
314+ let mock = server
315+ . mock ( "POST" , "/" )
316+ . with_status ( 201 )
317+ . with_header ( "Content-Type" , "application/x-protobuf" )
318+ . with_header ( "Content-Encoding" , "snappy" )
319+ . match_request ( |req| req. body ( ) . unwrap ( ) . len ( ) == 33 )
320+ . create ( ) ;
321+
322+ let ( tx, rx) = tokio:: sync:: mpsc:: unbounded_channel :: < TimeSeries > ( ) ;
323+ let handle = spawn ( consumer_loop ( Box :: leak ( Box :: new ( rx) ) , config) ) ;
324+
325+ let _ = tx. send ( TimeSeries {
326+ labels : Vec :: from ( [ Label {
327+ name : "__name__" . into ( ) ,
328+ value : "test" . into ( ) ,
329+ } ] ) ,
330+ samples : Vec :: from ( [ Sample {
331+ value : 1.0 ,
332+ timestamp : 0 ,
333+ } ] ) ,
334+ } ) ;
335+ drop ( tx) ;
336+ let _ = handle. await ;
337+
338+ mock. assert ( ) ;
339+ }
340+ }
0 commit comments