11use  crate :: selector:: CompileTestCase ; 
22use  crate :: { 
3-     ArtifactCollection ,  ArtifactId ,  ArtifactIdNumber ,  BenchmarkJob ,  BenchmarkRequest , 
4-     BenchmarkRequestIndex ,  BenchmarkRequestStatus ,  BenchmarkSet ,  CodegenBackend ,   CollectorConfig , 
5-     CompileBenchmark ,  Target , 
3+     ArtifactCollection ,  ArtifactId ,  ArtifactIdNumber ,  BenchmarkJob ,  BenchmarkJobConclusion , 
4+     BenchmarkRequest ,   BenchmarkRequestIndex ,  BenchmarkRequestStatus ,  BenchmarkSet ,  CodegenBackend , 
5+     CollectorConfig ,   CompileBenchmark ,  Target , 
66} ; 
77use  crate :: { CollectionId ,  Index ,  Profile ,  QueuedCommit ,  Scenario ,  Step } ; 
88use  chrono:: { DateTime ,  Utc } ; 
@@ -246,13 +246,25 @@ pub trait Connection: Send + Sync {
246246    /// Get the confiuguration for a collector by the name of the collector 
247247     async  fn  get_collector_config ( & self ,  collector_name :  & str )  -> anyhow:: Result < CollectorConfig > ; 
248248
249-     /// Get the confiuguration for a collector by the name of the collector  
249+     /// Dequeue benchmark job  
250250     async  fn  dequeue_benchmark_job ( 
251251        & self , 
252252        collector_name :  & str , 
253253        target :  & Target , 
254254        benchmark_set :  & BenchmarkSet , 
255255    )  -> anyhow:: Result < Option < BenchmarkJob > > ; 
256+ 
257+     /// Try and mark the benchmark_request as completed. Will return `true` if 
258+      /// it has been marked as completed else `false` meaning there was no change 
259+      async  fn  mark_benchmark_request_as_completed ( & self ,  tag :  & str )  -> anyhow:: Result < bool > ; 
260+ 
261+     /// Mark the job as completed. Sets the status to 'failed' or 'success' 
262+      /// depending on the enum's completed state being a success 
263+      async  fn  mark_benchmark_job_as_completed ( 
264+         & self , 
265+         id :  u32 , 
266+         benchmark_job_conculsion :  & BenchmarkJobConclusion , 
267+     )  -> anyhow:: Result < ( ) > ; 
256268} 
257269
258270#[ async_trait:: async_trait]  
@@ -388,6 +400,45 @@ mod tests {
388400        } 
389401    } 
390402
403+     async  fn  complete_request ( 
404+         db :  & dyn  Connection , 
405+         request_tag :  & str , 
406+         collector_name :  & str , 
407+         benchmark_set :  u32 , 
408+         target :  & Target , 
409+     )  { 
410+         /* Create job for the request */ 
411+         db. enqueue_benchmark_job ( 
412+             request_tag, 
413+             & target, 
414+             & CodegenBackend :: Llvm , 
415+             & Profile :: Opt , 
416+             benchmark_set, 
417+         ) 
418+         . await 
419+         . unwrap ( ) ; 
420+ 
421+         let  job = db
422+             . dequeue_benchmark_job ( collector_name,  & target,  & BenchmarkSet ( benchmark_set) ) 
423+             . await 
424+             . unwrap ( ) 
425+             . unwrap ( ) ; 
426+ 
427+         assert_eq ! ( job. request_tag( ) ,  request_tag) ; 
428+ 
429+         /* Mark the job as complete */ 
430+         db. mark_benchmark_job_as_completed ( job. id ( ) ,  & BenchmarkJobConclusion :: Success ) 
431+             . await 
432+             . unwrap ( ) ; 
433+ 
434+         assert_eq ! ( 
435+             db. mark_benchmark_request_as_completed( request_tag) 
436+                 . await 
437+                 . unwrap( ) , 
438+             true 
439+         ) ; 
440+     } 
441+ 
391442    #[ tokio:: test]  
392443    async  fn  pstat_returns_empty_vector_when_empty ( )  { 
393444        run_db_test ( |ctx| async  { 
@@ -475,28 +526,31 @@ mod tests {
475526    #[ tokio:: test]  
476527    async  fn  multiple_non_completed_try_requests ( )  { 
477528        run_postgres_test ( |ctx| async  { 
478-             let  db = ctx. db_client ( ) ; 
479-             let  db = db. connection ( ) . await ; 
529+             let  db = ctx. db_client ( ) . connection ( ) . await ; 
530+             let  target = & Target :: X86_64UnknownLinuxGnu ; 
531+             let  collector_name = "collector-1" ; 
532+             let  benchmark_set = 1 ; 
480533
481-             // Completed 
534+             db. add_collector_config ( collector_name,  & target,  benchmark_set,  true ) 
535+                 . await 
536+                 . unwrap ( ) ; 
537+ 
538+             // Complete parent 
539+             let  parent = BenchmarkRequest :: create_release ( "sha-parent-1" ,  Utc :: now ( ) ) ; 
540+             // Complete 
482541            let  req_a = BenchmarkRequest :: create_try_without_artifacts ( 42 ,  Utc :: now ( ) ,  "" ,  "" ) ; 
483542            // WaitingForArtifacts 
484543            let  req_b = BenchmarkRequest :: create_try_without_artifacts ( 42 ,  Utc :: now ( ) ,  "" ,  "" ) ; 
485544            let  req_c = BenchmarkRequest :: create_try_without_artifacts ( 42 ,  Utc :: now ( ) ,  "" ,  "" ) ; 
486545
546+             db. insert_benchmark_request ( & parent) . await . unwrap ( ) ; 
487547            db. insert_benchmark_request ( & req_a) . await . unwrap ( ) ; 
488548            db. attach_shas_to_try_benchmark_request ( 42 ,  "sha1" ,  "sha-parent-1" ) 
489549                . await 
490550                . unwrap ( ) ; 
491551
492-             db. update_benchmark_request_status ( 
493-                 "sha1" , 
494-                 BenchmarkRequestStatus :: Completed  { 
495-                     completed_at :  Utc :: now ( ) , 
496-                 } , 
497-             ) 
498-             . await 
499-             . unwrap ( ) ; 
552+             complete_request ( & * db,  "sha-parent-1" ,  collector_name,  benchmark_set,  & target) . await ; 
553+             complete_request ( & * db,  "sha1" ,  collector_name,  benchmark_set,  & target) . await ; 
500554
501555            // This should be fine, req_a was completed 
502556            db. insert_benchmark_request ( & req_b) . await . unwrap ( ) ; 
@@ -543,8 +597,15 @@ mod tests {
543597    #[ tokio:: test]  
544598    async  fn  load_pending_benchmark_requests ( )  { 
545599        run_postgres_test ( |ctx| async  { 
546-             let  db = ctx. db_client ( ) ; 
600+             let  db = ctx. db_client ( ) . connection ( ) . await ; 
547601            let  time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ; 
602+             let  target = & Target :: X86_64UnknownLinuxGnu ; 
603+             let  collector_name = "collector-1" ; 
604+             let  benchmark_set = 1 ; 
605+ 
606+             db. add_collector_config ( collector_name,  & target,  benchmark_set,  true ) 
607+                 . await 
608+                 . unwrap ( ) ; 
548609
549610            // ArtifactsReady 
550611            let  req_a = BenchmarkRequest :: create_master ( "sha-1" ,  "parent-sha-1" ,  42 ,  time) ; 
@@ -555,24 +616,17 @@ mod tests {
555616            // InProgress 
556617            let  req_d = BenchmarkRequest :: create_master ( "sha-2" ,  "parent-sha-2" ,  51 ,  time) ; 
557618            // Completed 
558-             let  req_e = BenchmarkRequest :: create_master ( "sha-3" ,   "parent-sha-3" ,   52 ,  time) ; 
619+             let  req_e = BenchmarkRequest :: create_release ( "1.79.0" ,  time) ; 
559620
560-             let  db = db. connection ( ) . await ; 
561621            for  & req in  & [ & req_a,  & req_b,  & req_c,  & req_d,  & req_e]  { 
562622                db. insert_benchmark_request ( req) . await . unwrap ( ) ; 
563623            } 
564624
625+             complete_request ( & * db,  "1.79.0" ,  collector_name,  benchmark_set,  & target) . await ; 
626+ 
565627            db. update_benchmark_request_status ( "sha-2" ,  BenchmarkRequestStatus :: InProgress ) 
566628                . await 
567629                . unwrap ( ) ; 
568-             db. update_benchmark_request_status ( 
569-                 "sha-3" , 
570-                 BenchmarkRequestStatus :: Completed  { 
571-                     completed_at :  Utc :: now ( ) , 
572-                 } , 
573-             ) 
574-             . await 
575-             . unwrap ( ) ; 
576630
577631            let  requests = db. load_pending_benchmark_requests ( ) . await . unwrap ( ) ; 
578632
@@ -837,4 +891,87 @@ mod tests {
837891        } ) 
838892        . await ; 
839893    } 
894+ 
895+     #[ tokio:: test]  
896+     async  fn  mark_request_as_complete_empty ( )  { 
897+         run_postgres_test ( |ctx| async  { 
898+             let  db = ctx. db_client ( ) . connection ( ) . await ; 
899+             let  time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ; 
900+ 
901+             let  insert_result = db
902+                 . add_collector_config ( "collector-1" ,  & Target :: X86_64UnknownLinuxGnu ,  1 ,  true ) 
903+                 . await ; 
904+             assert ! ( insert_result. is_ok( ) ) ; 
905+ 
906+             let  benchmark_request =
907+                 BenchmarkRequest :: create_master ( "sha-1" ,  "parent-sha-1" ,  42 ,  time) ; 
908+             db. insert_benchmark_request ( & benchmark_request) 
909+                 . await 
910+                 . unwrap ( ) ; 
911+             assert_eq ! ( 
912+                 db. mark_benchmark_request_as_completed( "sha-1" ) 
913+                     . await 
914+                     . unwrap( ) , 
915+                 true 
916+             ) ; 
917+             Ok ( ctx) 
918+         } ) 
919+         . await ; 
920+     } 
921+ 
922+     #[ tokio:: test]  
923+     async  fn  mark_request_as_complete ( )  { 
924+         run_postgres_test ( |ctx| async  { 
925+             let  db = ctx. db_client ( ) . connection ( ) . await ; 
926+             let  time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ; 
927+             let  benchmark_set = BenchmarkSet ( 0u32 ) ; 
928+             let  tag = "sha-1" ; 
929+             let  collector_name = "collector-1" ; 
930+             let  target = Target :: X86_64UnknownLinuxGnu ; 
931+ 
932+             let  insert_result = db
933+                 . add_collector_config ( collector_name,  & target,  1 ,  true ) 
934+                 . await ; 
935+             assert ! ( insert_result. is_ok( ) ) ; 
936+ 
937+             /* Create the request */ 
938+             let  benchmark_request = BenchmarkRequest :: create_release ( tag,  time) ; 
939+             db. insert_benchmark_request ( & benchmark_request) 
940+                 . await 
941+                 . unwrap ( ) ; 
942+ 
943+             /* Create job for the request */ 
944+             db. enqueue_benchmark_job ( 
945+                 benchmark_request. tag ( ) . unwrap ( ) , 
946+                 & target, 
947+                 & CodegenBackend :: Llvm , 
948+                 & Profile :: Opt , 
949+                 benchmark_set. 0 , 
950+             ) 
951+             . await 
952+             . unwrap ( ) ; 
953+ 
954+             let  job = db
955+                 . dequeue_benchmark_job ( collector_name,  & target,  & benchmark_set) 
956+                 . await 
957+                 . unwrap ( ) 
958+                 . unwrap ( ) ; 
959+ 
960+             assert_eq ! ( job. request_tag( ) ,  benchmark_request. tag( ) . unwrap( ) ) ; 
961+ 
962+             /* Mark the job as complete */ 
963+             db. mark_benchmark_job_as_completed ( job. id ( ) ,  & BenchmarkJobConclusion :: Success ) 
964+                 . await 
965+                 . unwrap ( ) ; 
966+ 
967+             db. mark_benchmark_request_as_completed ( tag) . await . unwrap ( ) ; 
968+ 
969+             let  completed = db. load_benchmark_request_index ( ) . await . unwrap ( ) ; 
970+ 
971+             assert ! ( completed. contains_tag( "sha-1" ) ) ; 
972+ 
973+             Ok ( ctx) 
974+         } ) 
975+         . await ; 
976+     } 
840977} 
0 commit comments