diff --git a/sdk/cosmos/azure-cosmos/tests/test_availability_strategy.py b/sdk/cosmos/azure-cosmos/tests/test_availability_strategy.py index 53ef61bb88a4..4637b77bc57e 100644 --- a/sdk/cosmos/azure-cosmos/tests/test_availability_strategy.py +++ b/sdk/cosmos/azure-cosmos/tests/test_availability_strategy.py @@ -9,6 +9,7 @@ import uuid from concurrent.futures import ThreadPoolExecutor from typing import Optional, Any, Union +from unittest.mock import patch import pytest from azure.core.exceptions import ServiceResponseError @@ -716,9 +717,12 @@ def test_no_cross_region_request_with_retry_write_disabled(self, operation): @pytest.mark.parametrize("operation", [READ, QUERY_PK, CHANGE_FEED, CREATE, UPSERT, REPLACE, DELETE, PATCH, BATCH]) def test_per_partition_circular_breaker_with_cancelled_first_future(self, operation): # QUERY, READ_ALL are not included because currently they are not targeting to a specific pkRange - os.environ["AZURE_COSMOS_ENABLE_CIRCUIT_BREAKER"] = "True" - os.environ["AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_WRITE"] = "5" - os.environ["AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_READ"] = "5" + env_patch = patch.dict(os.environ, { + "AZURE_COSMOS_ENABLE_CIRCUIT_BREAKER": "True", + "AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_WRITE": "5", + "AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_READ": "5", + }) + env_patch.start() try: """Test that when per partition circular breaker is enabled and after hitting the threshold, subsequent requests go directly to second region. @@ -799,9 +803,7 @@ def test_per_partition_circular_breaker_with_cancelled_first_future(self, operat availability_strategy=strategy) finally: - del os.environ["AZURE_COSMOS_ENABLE_CIRCUIT_BREAKER"] - del os.environ["AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_WRITE"] - del os.environ["AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_READ"] + env_patch.stop() self._clean_up_container(setup_with_fault_injection['db'].id, setup_with_fault_injection['col'].id) diff --git a/sdk/cosmos/azure-cosmos/tests/test_availability_strategy_async.py b/sdk/cosmos/azure-cosmos/tests/test_availability_strategy_async.py index 2017c6d7a9dd..46eab5e94080 100644 --- a/sdk/cosmos/azure-cosmos/tests/test_availability_strategy_async.py +++ b/sdk/cosmos/azure-cosmos/tests/test_availability_strategy_async.py @@ -7,6 +7,7 @@ import unittest import uuid from typing import Optional, Any, Union +from unittest.mock import patch import pytest import pytest_asyncio @@ -847,9 +848,12 @@ async def test_no_cross_region_request_with_exclude_regions_async(self, operatio @pytest.mark.parametrize("operation", [READ, QUERY_PK, CHANGE_FEED, CREATE, UPSERT, REPLACE, DELETE, PATCH, BATCH]) async def test_per_partition_circular_breaker_with_cancelled_first_future_async(self, operation, setup): # QUERY, READ_ALL are not included because currently they are not targeting to a specific pkRange - os.environ["AZURE_COSMOS_ENABLE_CIRCUIT_BREAKER"] = "True" - os.environ["AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_WRITE"] = "5" - os.environ["AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_READ"] = "5" + env_patch = patch.dict(os.environ, { + "AZURE_COSMOS_ENABLE_CIRCUIT_BREAKER": "True", + "AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_WRITE": "5", + "AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_READ": "5", + }) + env_patch.start() try: """Test that when per partition circular breaker is enabled and after hitting the threshold, subsequent requests go directly to second region. @@ -938,9 +942,7 @@ async def test_per_partition_circular_breaker_with_cancelled_first_future_async( await setup_with_fault_injection['client'].close() await setup_without_fault['client'].close() finally: - del os.environ["AZURE_COSMOS_ENABLE_CIRCUIT_BREAKER"] - del os.environ["AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_WRITE"] - del os.environ["AZURE_COSMOS_CONSECUTIVE_ERROR_COUNT_TOLERATED_FOR_READ"] + env_patch.stop() await self._clean_up_container(setup['client_without_fault'], setup_with_fault_injection['db'].id, setup_with_fault_injection['col'].id) @pytest.mark.asyncio