8
8
from httmock import response
9
9
from requests import Response
10
10
from requests .exceptions import HTTPError
11
+ from tenacity .stop import stop_after_attempt
11
12
12
13
from cloudpub .common import BaseService
13
14
from cloudpub .error import InvalidStateError , NotFoundError
@@ -730,6 +731,8 @@ def test_ensure_can_publish_success(
730
731
"created" : "2024-07-04T22:06:16.2895521Z" ,
731
732
}
732
733
mock_getsubst .return_value = ProductSubmission .from_json (submission )
734
+ azure_service .ensure_can_publish .retry .sleep = mock .MagicMock () # type: ignore
735
+ azure_service .ensure_can_publish .retry .stop = stop_after_attempt (1 ) # type: ignore
733
736
734
737
azure_service .ensure_can_publish ("ffffffff-ffff-ffff-ffff-ffffffffffff" )
735
738
@@ -741,6 +744,48 @@ def test_ensure_can_publish_success(
741
744
]
742
745
)
743
746
747
+ @pytest .mark .parametrize ("target" , ["preview" , "live" ])
748
+ @mock .patch ("cloudpub.ms_azure.AzureService.get_submission_state" )
749
+ def test_ensure_can_publish_success_after_retry (
750
+ self ,
751
+ mock_getsubst : mock .MagicMock ,
752
+ target : str ,
753
+ azure_service : AzureService ,
754
+ ) -> None :
755
+ running = {
756
+ "$schema" : "https://product-ingestion.azureedge.net/schema/submission/2022-03-01-preview2" , # noqa: E501
757
+ "id" : "submission/ffffffff-ffff-ffff-ffff-ffffffffffff/0" ,
758
+ "product" : "product/ffffffff-ffff-ffff-ffff-ffffffffffff" ,
759
+ "target" : {"targetType" : target },
760
+ "lifecycleState" : "generallyAvailable" ,
761
+ "status" : "running" ,
762
+ "result" : "pending" ,
763
+ "created" : "2024-07-04T22:06:16.2895521Z" ,
764
+ }
765
+ complete = {
766
+ "$schema" : "https://product-ingestion.azureedge.net/schema/submission/2022-03-01-preview2" , # noqa: E501
767
+ "id" : "submission/ffffffff-ffff-ffff-ffff-ffffffffffff/0" ,
768
+ "product" : "product/ffffffff-ffff-ffff-ffff-ffffffffffff" ,
769
+ "target" : {"targetType" : target },
770
+ "lifecycleState" : "generallyAvailable" ,
771
+ "status" : "completed" ,
772
+ "result" : "succeeded" ,
773
+ "created" : "2024-07-04T22:06:16.2895521Z" ,
774
+ }
775
+ mock_getsubst .side_effect = [
776
+ ProductSubmission .from_json (running ),
777
+ ProductSubmission .from_json (running ),
778
+ ProductSubmission .from_json (complete ),
779
+ ProductSubmission .from_json (complete ),
780
+ ]
781
+ azure_service .ensure_can_publish .retry .sleep = mock .MagicMock () # type: ignore
782
+ azure_service .ensure_can_publish .retry .stop = stop_after_attempt (3 ) # type: ignore
783
+
784
+ azure_service .ensure_can_publish ("ffffffff-ffff-ffff-ffff-ffffffffffff" )
785
+
786
+ # Calls for "live" and "preview" for 2 times before success == 4
787
+ assert mock_getsubst .call_count == 4
788
+
744
789
@pytest .mark .parametrize ("target" , ["preview" , "live" ])
745
790
@mock .patch ("cloudpub.ms_azure.AzureService.get_submission_state" )
746
791
def test_ensure_can_publish_raises (
@@ -782,6 +827,8 @@ def test_ensure_can_publish_raises(
782
827
err = (
783
828
f"The offer ffffffff-ffff-ffff-ffff-ffffffffffff is already being published to { target } "
784
829
)
830
+ azure_service .ensure_can_publish .retry .sleep = mock .MagicMock () # type: ignore
831
+ azure_service .ensure_can_publish .retry .stop = stop_after_attempt (1 ) # type: ignore
785
832
786
833
with pytest .raises (RuntimeError , match = err ):
787
834
azure_service .ensure_can_publish ("ffffffff-ffff-ffff-ffff-ffffffffffff" )
0 commit comments