@@ -335,7 +335,7 @@ mod tests {
335335 use super :: * ;
336336 use crate :: {
337337 tests:: { run_db_test, run_postgres_test} ,
338- BenchmarkRequestStatus , Commit , CommitType , Date ,
338+ BenchmarkRequestStatus , BenchmarkRequestType , Commit , CommitType , Date ,
339339 } ;
340340
341341 /// Create a Commit
@@ -406,264 +406,179 @@ mod tests {
406406 . await ;
407407 }
408408
409+ // Check that we can't have multiple requests with the same SHA
409410 #[ tokio:: test]
410- async fn insert_benchmark_requests ( ) {
411+ async fn multiple_requests_same_sha ( ) {
411412 run_postgres_test ( |ctx| async {
412413 let db = ctx. db_client ( ) ;
413- let time = chrono :: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ;
414- let master_benchmark_request = BenchmarkRequest :: create_master (
414+ let db = db . connection ( ) . await ;
415+ db . insert_benchmark_request ( & BenchmarkRequest :: create_master (
415416 "a-sha-1" ,
416417 "parent-sha-1" ,
417418 42 ,
418- time,
419- BenchmarkRequestStatus :: ArtifactsReady ,
420- "llvm" ,
421- "" ,
422- ) ;
423-
424- let try_benchmark_request = BenchmarkRequest :: create_try_without_artifacts (
425- Some ( "b-sha-2" ) ,
426- Some ( "parent-sha-2" ) ,
427- 32 ,
428- time,
429- BenchmarkRequestStatus :: ArtifactsReady ,
430- "cranelift" ,
431- "" ,
432- ) ;
433-
434- let release_benchmark_request = BenchmarkRequest :: create_release (
435- "1.8.0" ,
436- time,
437- BenchmarkRequestStatus :: ArtifactsReady ,
438- ) ;
419+ Utc :: now ( ) ,
420+ ) )
421+ . await
422+ . unwrap ( ) ;
439423
440- let db = db. connection ( ) . await ;
441- db. insert_benchmark_request ( & master_benchmark_request)
424+ db. insert_benchmark_request ( & BenchmarkRequest :: create_release ( "a-sha-1" , Utc :: now ( ) ) )
442425 . await
443- . unwrap ( ) ;
444- db. insert_benchmark_request ( & try_benchmark_request)
445- . await
446- . unwrap ( ) ;
447- db. insert_benchmark_request ( & release_benchmark_request)
448- . await
449- . unwrap ( ) ;
450- // duplicate insert
451- assert ! ( db
452- . insert_benchmark_request( & master_benchmark_request)
453- . await
454- . is_err( ) ) ;
426+ . expect_err ( "it was possible to insert a second commit with the same SHA" ) ;
455427
456428 Ok ( ctx)
457429 } )
458430 . await ;
459431 }
460432
433+ // Check that we can't have multiple non-completed try requests on the same PR
461434 #[ tokio:: test]
462- async fn get_benchmark_requests_by_status ( ) {
463- // Ensure we get back the requests matching the status with no date
464- // limit
435+ async fn multiple_non_completed_try_requests ( ) {
465436 run_postgres_test ( |ctx| async {
466437 let db = ctx. db_client ( ) ;
467- let time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ;
468- let master_benchmark_request = BenchmarkRequest :: create_master (
469- "a-sha-1" ,
470- "parent-sha-1" ,
471- 42 ,
472- time,
473- BenchmarkRequestStatus :: ArtifactsReady ,
474- "llvm" ,
475- "" ,
476- ) ;
477-
478- let try_benchmark_request = BenchmarkRequest :: create_try_without_artifacts (
479- Some ( "b-sha-2" ) ,
480- Some ( "parent-sha-2" ) ,
481- 32 ,
482- time,
483- BenchmarkRequestStatus :: Completed ,
484- "cranelift" ,
485- "" ,
486- ) ;
438+ let db = db. connection ( ) . await ;
487439
488- let release_benchmark_request = BenchmarkRequest :: create_release (
489- "1.8.0" ,
490- time ,
491- BenchmarkRequestStatus :: ArtifactsReady ,
492- ) ;
440+ // Completed
441+ let req_a = BenchmarkRequest :: create_try_without_artifacts ( 42 , Utc :: now ( ) , "" , "" ) ;
442+ // WaitingForArtifacts
443+ let req_b = BenchmarkRequest :: create_try_without_artifacts ( 42 , Utc :: now ( ) , "" , "" ) ;
444+ let req_c = BenchmarkRequest :: create_try_without_artifacts ( 42 , Utc :: now ( ) , "" , "" ) ;
493445
494- let db = db. connection ( ) . await ;
495- db. insert_benchmark_request ( & master_benchmark_request)
496- . await
497- . unwrap ( ) ;
498- db. insert_benchmark_request ( & try_benchmark_request)
499- . await
500- . unwrap ( ) ;
501- db. insert_benchmark_request ( & release_benchmark_request)
446+ db. insert_benchmark_request ( & req_a) . await . unwrap ( ) ;
447+ db. attach_shas_to_try_benchmark_request ( 42 , "sha1" , "sha-parent-1" )
502448 . await
503449 . unwrap ( ) ;
504450
505- let requests = db
506- . load_benchmark_request_index ( & [ BenchmarkRequestStatus :: ArtifactsReady ] )
507- . await
508- . unwrap ( ) ;
451+ db. update_benchmark_request_status (
452+ "sha1" ,
453+ BenchmarkRequestStatus :: Completed {
454+ completed_at : Utc :: now ( ) ,
455+ } ,
456+ )
457+ . await
458+ . unwrap ( ) ;
509459
510- assert_eq ! ( requests. len( ) , 2 ) ;
511- assert_eq ! ( requests[ 0 ] . status, BenchmarkRequestStatus :: ArtifactsReady ) ;
512- assert_eq ! ( requests[ 1 ] . status, BenchmarkRequestStatus :: ArtifactsReady ) ;
460+ // This should be fine, req_a was completed
461+ db. insert_benchmark_request ( & req_b) . await . unwrap ( ) ;
462+ // This should fail, we can't have two queued requests at once
463+ db. insert_benchmark_request ( & req_c) . await . expect_err (
464+ "It was possible to record two try benchmark requests without artifacts" ,
465+ ) ;
513466
514467 Ok ( ctx)
515468 } )
516469 . await ;
517470 }
518471
472+ // Check that we can't have multiple master requests on the same PR
519473 #[ tokio:: test]
520- async fn update_benchmark_request_status ( ) {
521- // Insert one item into the database, change the status and then
522- // get the item back out again to ensure it has changed status
474+ async fn multiple_master_requests_same_pr ( ) {
523475 run_postgres_test ( |ctx| async {
524476 let db = ctx. db_client ( ) ;
525- let time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ;
526- let master_benchmark_request = BenchmarkRequest :: create_master (
477+ let db = db. connection ( ) . await ;
478+
479+ db. insert_benchmark_request ( & BenchmarkRequest :: create_master (
527480 "a-sha-1" ,
528481 "parent-sha-1" ,
529482 42 ,
530- time,
531- BenchmarkRequestStatus :: ArtifactsReady ,
532- "llvm" ,
533- "" ,
534- ) ;
535-
536- let mut db = db. connection ( ) . await ;
537- db. insert_benchmark_request ( & master_benchmark_request)
538- . await
539- . unwrap ( ) ;
540-
541- db. update_benchmark_request_status (
542- & master_benchmark_request,
543- BenchmarkRequestStatus :: InProgress ,
544- )
483+ Utc :: now ( ) ,
484+ ) )
545485 . await
546486 . unwrap ( ) ;
547487
548- let requests = db
549- . load_benchmark_request_index ( & [ BenchmarkRequestStatus :: InProgress ] )
550- . await
551- . unwrap ( ) ;
552-
553- assert_eq ! ( requests . len ( ) , 1 ) ;
554- assert_eq ! ( requests [ 0 ] . tag ( ) , master_benchmark_request . tag ( ) ) ;
555- assert_eq ! ( requests [ 0 ] . status , BenchmarkRequestStatus :: InProgress ) ;
488+ db . insert_benchmark_request ( & BenchmarkRequest :: create_master (
489+ "a-sha-2" ,
490+ "parent-sha-2" ,
491+ 42 ,
492+ Utc :: now ( ) ,
493+ ) )
494+ . await
495+ . expect_err ( "it was possible to insert a second master commit on the same PR" ) ;
556496
557497 Ok ( ctx)
558498 } )
559499 . await ;
560500 }
561501
562502 #[ tokio:: test]
563- async fn updating_try_commits ( ) {
503+ async fn load_pending_benchmark_requests ( ) {
564504 run_postgres_test ( |ctx| async {
565505 let db = ctx. db_client ( ) ;
566- let db = db. connection ( ) . await ;
567506 let time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ;
568- let pr = 42 ;
569-
570- let try_benchmark_request = BenchmarkRequest :: create_try_without_artifacts (
571- None ,
572- None ,
573- pr,
574- time,
575- BenchmarkRequestStatus :: WaitingForArtifacts ,
576- "cranelift" ,
577- "" ,
578- ) ;
579- db. insert_benchmark_request ( & try_benchmark_request)
580- . await
581- . unwrap ( ) ;
582- db. attach_shas_to_try_benchmark_request ( pr, "foo" , "bar" )
583- . await
584- . unwrap ( ) ;
585- let requests = db
586- . load_benchmark_request_index ( & [ BenchmarkRequestStatus :: ArtifactsReady ] )
507+
508+ // ArtifactsReady
509+ let req_a = BenchmarkRequest :: create_master ( "sha-1" , "parent-sha-1" , 42 , time) ;
510+ // ArtifactsReady
511+ let req_b = BenchmarkRequest :: create_release ( "1.80.0" , time) ;
512+ // WaitingForArtifacts
513+ let req_c = BenchmarkRequest :: create_try_without_artifacts ( 50 , time, "" , "" ) ;
514+ // InProgress
515+ let req_d = BenchmarkRequest :: create_master ( "sha-2" , "parent-sha-2" , 51 , time) ;
516+ // Completed
517+ let req_e = BenchmarkRequest :: create_master ( "sha-3" , "parent-sha-3" , 52 , time) ;
518+
519+ let db = db. connection ( ) . await ;
520+ for & req in & [ & req_a, & req_b, & req_c, & req_d, & req_e] {
521+ db. insert_benchmark_request ( req) . await . unwrap ( ) ;
522+ }
523+
524+ db. update_benchmark_request_status ( "sha-2" , BenchmarkRequestStatus :: InProgress )
587525 . await
588526 . unwrap ( ) ;
527+ db. update_benchmark_request_status (
528+ "sha-3" ,
529+ BenchmarkRequestStatus :: Completed {
530+ completed_at : Utc :: now ( ) ,
531+ } ,
532+ )
533+ . await
534+ . unwrap ( ) ;
589535
590- assert_eq ! ( requests. len( ) , 1 ) ;
591- assert_eq ! ( requests[ 0 ] . tag( ) , Some ( "foo" ) ) ;
592- assert_eq ! ( requests[ 0 ] . parent_sha( ) , Some ( "bar" ) ) ;
593- assert_eq ! ( requests[ 0 ] . status, BenchmarkRequestStatus :: ArtifactsReady ) ;
536+ let requests = db. load_pending_benchmark_requests ( ) . await . unwrap ( ) ;
537+
538+ assert_eq ! ( requests. len( ) , 3 ) ;
539+ for req in & [ req_a, req_b, req_d] {
540+ assert ! ( requests. iter( ) . any( |r| r. tag( ) == req. tag( ) ) ) ;
541+ }
594542
595543 Ok ( ctx)
596544 } )
597545 . await ;
598546 }
599547
600548 #[ tokio:: test]
601- async fn adding_try_commit_to_completed_request ( ) {
549+ async fn attach_shas_to_try_benchmark_request ( ) {
602550 run_postgres_test ( |ctx| async {
603551 let db = ctx. db_client ( ) ;
604552 let db = db. connection ( ) . await ;
605- let time = chrono:: DateTime :: from_str ( "2021-09-01T00:00:00.000Z" ) . unwrap ( ) ;
606- let pr = 42 ;
607-
608- let completed_try = BenchmarkRequest :: create_try_without_artifacts (
609- Some ( "sha-2" ) ,
610- Some ( "p-sha-1" ) ,
611- pr,
612- time,
613- BenchmarkRequestStatus :: Completed ,
614- "cranelift" ,
615- "" ,
616- ) ;
617- db. insert_benchmark_request ( & completed_try) . await . unwrap ( ) ;
618-
619- let try_benchmark_request = BenchmarkRequest :: create_try_without_artifacts (
620- None ,
621- None ,
622- pr,
623- time,
624- BenchmarkRequestStatus :: WaitingForArtifacts ,
625- "cranelift" ,
626- "" ,
627- ) ;
628- // deliberately insert twice
629- db. insert_benchmark_request ( & try_benchmark_request)
630- . await
631- . unwrap ( ) ;
632- // this one should fail
633- assert ! ( db
634- . insert_benchmark_request( & try_benchmark_request)
635- . await
636- . is_err( ) ) ;
637- db. attach_shas_to_try_benchmark_request ( pr, "foo" , "bar" )
553+
554+ let req = BenchmarkRequest :: create_try_without_artifacts ( 42 , Utc :: now ( ) , "" , "" ) ;
555+
556+ db. insert_benchmark_request ( & req) . await . unwrap ( ) ;
557+ db. attach_shas_to_try_benchmark_request ( 42 , "sha1" , "sha-parent-1" )
638558 . await
639559 . unwrap ( ) ;
640560
641- let requests = db
642- . load_benchmark_request_index ( & [
643- BenchmarkRequestStatus :: WaitingForArtifacts ,
644- BenchmarkRequestStatus :: ArtifactsReady ,
645- BenchmarkRequestStatus :: InProgress ,
646- BenchmarkRequestStatus :: Completed ,
647- ] )
561+ let req_db = db
562+ . load_pending_benchmark_requests ( )
648563 . await
564+ . unwrap ( )
565+ . into_iter ( )
566+ . next ( )
649567 . unwrap ( ) ;
650-
651- assert_eq ! ( requests. len( ) , 2 ) ;
652- let completed_try = requests
653- . iter ( )
654- . find ( |req| req. status == BenchmarkRequestStatus :: Completed ) ;
655- assert ! ( completed_try. is_some( ) ) ;
656- assert_eq ! ( completed_try. unwrap( ) . pr( ) , Some ( & pr) ) ;
657- assert_eq ! ( completed_try. unwrap( ) . tag( ) , Some ( "sha-2" ) ) ;
658- assert_eq ! ( completed_try. unwrap( ) . parent_sha( ) , Some ( "p-sha-1" ) ) ;
659-
660- let artifacts_ready_try = requests
661- . iter ( )
662- . find ( |req| req. status == BenchmarkRequestStatus :: ArtifactsReady ) ;
663- assert ! ( artifacts_ready_try. is_some( ) ) ;
664- assert_eq ! ( artifacts_ready_try. unwrap( ) . pr( ) , Some ( & pr) ) ;
665- assert_eq ! ( artifacts_ready_try. unwrap( ) . tag( ) , Some ( "foo" ) ) ;
666- assert_eq ! ( artifacts_ready_try. unwrap( ) . parent_sha( ) , Some ( "bar" ) ) ;
568+ assert_eq ! ( req. backends, req_db. backends) ;
569+ assert_eq ! ( req. profiles, req_db. profiles) ;
570+ assert ! ( matches!(
571+ req_db. status,
572+ BenchmarkRequestStatus :: ArtifactsReady
573+ ) ) ;
574+ assert ! ( matches!(
575+ req_db. commit_type,
576+ BenchmarkRequestType :: Try { .. }
577+ ) ) ;
578+
579+ assert_eq ! ( req_db. tag( ) . as_deref( ) , Some ( "sha1" ) ) ;
580+ assert_eq ! ( req_db. parent_sha( ) . as_deref( ) , Some ( "sha-parent-1" ) ) ;
581+ assert_eq ! ( req_db. pr( ) , Some ( & 42 ) ) ;
667582
668583 Ok ( ctx)
669584 } )
0 commit comments