@@ -6,6 +6,8 @@ use crate::backup::DiskBackupPolicy;
66use crate :: {
77 ChannelValue , Flow , IngestionConfigForm , RecoveryStrategy , RunForm , SiftStreamBuilder ,
88} ;
9+ use sift_rs:: common:: r#type:: v1:: ChannelDataType ;
10+ use sift_rs:: ingestion_configs:: v2:: { ChannelConfig , FlowConfig } ;
911use tempdir:: TempDir ;
1012use tracing_test:: traced_test;
1113
@@ -44,6 +46,7 @@ async fn test_sift_stream_builder_backup_manager_directory_naming_with_run() {
4446 retry_policy,
4547 disk_backup_policy,
4648 } )
49+ . metrics_streaming_interval ( None )
4750 . build ( )
4851 . await
4952 . expect ( "failed to build sift stream" ) ;
@@ -72,7 +75,7 @@ async fn test_sift_stream_builder_backup_manager_directory_naming_with_run() {
7275 let test_dir = fs:: read_dir ( tmp_dir_path)
7376 . expect ( "failed to read backups directory" )
7477 . collect :: < Vec < _ > > ( ) ;
75- assert_eq ! ( test_dir. len( ) , 1 ) ;
78+ assert_eq ! ( test_dir. len( ) , 1 , "{:?}" , test_dir ) ;
7679
7780 // The first subdirectory should be the asset name.
7881 let asset_dir = test_dir[ 0 ] . as_ref ( ) . expect ( "failed to get file" ) ;
@@ -122,6 +125,7 @@ async fn test_sift_stream_builder_backup_manager_directory_naming_no_run() {
122125 retry_policy,
123126 disk_backup_policy,
124127 } )
128+ . metrics_streaming_interval ( None )
125129 . build ( )
126130 . await
127131 . expect ( "failed to build sift stream" ) ;
@@ -230,3 +234,168 @@ async fn test_sift_stream_drop_without_finish() {
230234 . await
231235 . expect ( "timeout waiting for tasks to shutdown" ) ;
232236}
237+
238+ #[ tokio:: test]
239+ async fn test_sift_stream_builder_load_ingestion_config_with_no_flows ( ) {
240+ let backups_dir = uuid:: Uuid :: new_v4 ( ) . to_string ( ) ;
241+
242+ let tmp_dir = TempDir :: new ( & backups_dir) . expect ( "failed to creat tempdir" ) ;
243+ let tmp_dir_path = tmp_dir. path ( ) ;
244+
245+ let ingestion_config = IngestionConfigForm {
246+ asset_name : "already_exists_asset" . to_string ( ) ,
247+ client_key : "already_exists_client_key" . to_string ( ) ,
248+ flows : vec ! [ ] ,
249+ } ;
250+ let disk_backup_policy = DiskBackupPolicy {
251+ backups_dir : Some ( tmp_dir_path. to_path_buf ( ) ) ,
252+ retain_backups : true ,
253+ ..Default :: default ( )
254+ } ;
255+ let retry_policy = crate :: RetryPolicy :: default ( ) ;
256+ let ( grpc_channel, _mock_service) = crate :: test:: create_mock_grpc_channel_with_service ( ) . await ;
257+
258+ let mut sift_stream = SiftStreamBuilder :: from_channel ( grpc_channel)
259+ . ingestion_config ( ingestion_config)
260+ . recovery_strategy ( RecoveryStrategy :: RetryWithBackups {
261+ retry_policy,
262+ disk_backup_policy,
263+ } )
264+ . build ( )
265+ . await
266+ . expect ( "failed to build sift stream" ) ;
267+
268+ // The mock sift server should have returned 1 flow.
269+ let flows = sift_stream. get_flows ( ) ;
270+ assert_eq ! ( flows. len( ) , 1 ) ;
271+
272+ let existing_flow = FlowConfig {
273+ name : "already_exists_flow" . to_string ( ) ,
274+ channels : vec ! [ ChannelConfig {
275+ name: "channel1" . to_string( ) ,
276+ data_type: ChannelDataType :: Double . into( ) ,
277+ ..Default :: default ( )
278+ } ] ,
279+ } ;
280+
281+ // Add the existing flow again to ensure it is not added again.
282+ assert ! ( sift_stream. add_new_flows( & [ existing_flow] ) . await . is_ok( ) ) ;
283+ let flows = sift_stream. get_flows ( ) ;
284+ assert_eq ! ( flows. len( ) , 1 ) ;
285+
286+ sift_stream
287+ . finish ( )
288+ . await
289+ . expect ( "failed to finish sift stream" ) ;
290+ }
291+
292+ #[ tokio:: test]
293+ async fn test_sift_stream_builder_load_ingestion_config_with_flows ( ) {
294+ let backups_dir = uuid:: Uuid :: new_v4 ( ) . to_string ( ) ;
295+
296+ let tmp_dir = TempDir :: new ( & backups_dir) . expect ( "failed to creat tempdir" ) ;
297+ let tmp_dir_path = tmp_dir. path ( ) ;
298+
299+ let existing_flow = FlowConfig {
300+ name : "already_exists_flow" . to_string ( ) ,
301+ channels : vec ! [ ChannelConfig {
302+ name: "channel1" . to_string( ) ,
303+ data_type: ChannelDataType :: Double . into( ) ,
304+ ..Default :: default ( )
305+ } ] ,
306+ } ;
307+
308+ let ingestion_config = IngestionConfigForm {
309+ asset_name : "test_asset" . to_string ( ) ,
310+ client_key : "test_client_key" . to_string ( ) ,
311+ flows : vec ! [ existing_flow. clone( ) ] ,
312+ } ;
313+ let disk_backup_policy = DiskBackupPolicy {
314+ backups_dir : Some ( tmp_dir_path. to_path_buf ( ) ) ,
315+ retain_backups : true ,
316+ ..Default :: default ( )
317+ } ;
318+ let retry_policy = crate :: RetryPolicy :: default ( ) ;
319+ let ( grpc_channel, _mock_service) = crate :: test:: create_mock_grpc_channel_with_service ( ) . await ;
320+
321+ let mut sift_stream = SiftStreamBuilder :: from_channel ( grpc_channel)
322+ . ingestion_config ( ingestion_config)
323+ . recovery_strategy ( RecoveryStrategy :: RetryWithBackups {
324+ retry_policy,
325+ disk_backup_policy,
326+ } )
327+ . build ( )
328+ . await
329+ . expect ( "failed to build sift stream" ) ;
330+
331+ // The mock sift server should have returned 1 flow.
332+ let flows = sift_stream. get_flows ( ) ;
333+ assert_eq ! ( flows. len( ) , 1 ) ;
334+
335+ // Add the existing flow again to ensure it is not added again.
336+ assert ! ( sift_stream. add_new_flows( & [ existing_flow] ) . await . is_ok( ) ) ;
337+ let flows = sift_stream. get_flows ( ) ;
338+ assert_eq ! ( flows. len( ) , 1 ) ;
339+ }
340+
341+ #[ tokio:: test]
342+ async fn test_sift_stream_builder_load_ingestion_config_with_new_flows ( ) {
343+ let backups_dir = uuid:: Uuid :: new_v4 ( ) . to_string ( ) ;
344+
345+ let tmp_dir = TempDir :: new ( & backups_dir) . expect ( "failed to creat tempdir" ) ;
346+ let tmp_dir_path = tmp_dir. path ( ) ;
347+
348+ let new_flow = FlowConfig {
349+ name : "new_flow" . to_string ( ) ,
350+ channels : vec ! [ ChannelConfig {
351+ name: "channel-new" . to_string( ) ,
352+ data_type: ChannelDataType :: Uint32 . into( ) ,
353+ ..Default :: default ( )
354+ } ] ,
355+ } ;
356+
357+ let ingestion_config = IngestionConfigForm {
358+ asset_name : "test_asset" . to_string ( ) ,
359+ client_key : "test_client_key" . to_string ( ) ,
360+ flows : vec ! [ new_flow. clone( ) ] ,
361+ } ;
362+ let disk_backup_policy = DiskBackupPolicy {
363+ backups_dir : Some ( tmp_dir_path. to_path_buf ( ) ) ,
364+ retain_backups : true ,
365+ ..Default :: default ( )
366+ } ;
367+ let retry_policy = crate :: RetryPolicy :: default ( ) ;
368+ let ( grpc_channel, _mock_service) = crate :: test:: create_mock_grpc_channel_with_service ( ) . await ;
369+
370+ let mut sift_stream = SiftStreamBuilder :: from_channel ( grpc_channel)
371+ . ingestion_config ( ingestion_config)
372+ . recovery_strategy ( RecoveryStrategy :: RetryWithBackups {
373+ retry_policy,
374+ disk_backup_policy,
375+ } )
376+ . build ( )
377+ . await
378+ . expect ( "failed to build sift stream" ) ;
379+
380+ // The mock sift server should have returned 1 flow.
381+ let flows = sift_stream. get_flows ( ) ;
382+ assert_eq ! ( flows. len( ) , 1 ) ;
383+
384+ // Add the existing flow again to ensure it is not added again.
385+ assert ! ( sift_stream. add_new_flows( & [ new_flow] ) . await . is_ok( ) ) ;
386+ let flows = sift_stream. get_flows ( ) ;
387+ assert_eq ! ( flows. len( ) , 1 ) ;
388+
389+ // Add another new flow to ensure it is added.
390+ let new_flow2 = FlowConfig {
391+ name : "new_flow2" . to_string ( ) ,
392+ channels : vec ! [ ChannelConfig {
393+ name: "channel-new2" . to_string( ) ,
394+ data_type: ChannelDataType :: Uint32 . into( ) ,
395+ ..Default :: default ( )
396+ } ] ,
397+ } ;
398+ assert ! ( sift_stream. add_new_flows( & [ new_flow2] ) . await . is_ok( ) ) ;
399+ let flows = sift_stream. get_flows ( ) ;
400+ assert_eq ! ( flows. len( ) , 2 ) ;
401+ }
0 commit comments