|
| 1 | +""" |
| 2 | +Unit tests for DatabricksRetryPolicy to verify retry decisions independent of HTTP adapter. |
| 3 | +""" |
| 4 | +import time |
| 5 | +import pytest |
| 6 | +from databricks.sql.auth.retry import ( |
| 7 | + DatabricksRetryPolicy, |
| 8 | + CommandType, |
| 9 | +) |
| 10 | +from databricks.sql.exc import MaxRetryDurationError |
| 11 | + |
| 12 | + |
| 13 | +def make_policy(**overrides): |
| 14 | + # Base parameters: small counts and durations for quick tests |
| 15 | + params = dict( |
| 16 | + delay_min=0.1, |
| 17 | + delay_max=0.5, |
| 18 | + stop_after_attempts_count=3, |
| 19 | + stop_after_attempts_duration=1.0, |
| 20 | + delay_default=0.1, |
| 21 | + force_dangerous_codes=[], |
| 22 | + ) |
| 23 | + params.update(overrides) |
| 24 | + return DatabricksRetryPolicy(**params) |
| 25 | + |
| 26 | + |
| 27 | +def test_retry_for_429_and_503_execute_statement(): |
| 28 | + policy = make_policy() |
| 29 | + policy.command_type = CommandType.EXECUTE_STATEMENT |
| 30 | + # 429 is in status_forcelist |
| 31 | + can_retry_429, _ = policy.should_retry("POST", 429) |
| 32 | + assert can_retry_429 |
| 33 | + # 503 is in status_forcelist |
| 34 | + can_retry_503, _ = policy.should_retry("POST", 503) |
| 35 | + assert can_retry_503 |
| 36 | + |
| 37 | + |
| 38 | +def test_no_retry_for_502_execute_statement_by_default(): |
| 39 | + policy = make_policy() |
| 40 | + policy.command_type = CommandType.EXECUTE_STATEMENT |
| 41 | + can_retry_502, _ = policy.should_retry("POST", 502) |
| 42 | + assert not can_retry_502 |
| 43 | + |
| 44 | + |
| 45 | +def test_retry_for_502_when_forced(): |
| 46 | + policy = make_policy(force_dangerous_codes=[502]) |
| 47 | + policy.command_type = CommandType.EXECUTE_STATEMENT |
| 48 | + can_retry_502, _ = policy.should_retry("POST", 502) |
| 49 | + assert can_retry_502 |
| 50 | + |
| 51 | + |
| 52 | +def test_no_retry_for_get_method(): |
| 53 | + policy = make_policy() |
| 54 | + policy.command_type = CommandType.EXECUTE_STATEMENT |
| 55 | + # GET should not retry |
| 56 | + can_retry_get, _ = policy.should_retry("GET", 503) |
| 57 | + assert not can_retry_get |
| 58 | + |
| 59 | + |
| 60 | +def test_max_retry_duration_error(): |
| 61 | + policy = make_policy(stop_after_attempts_duration=0.2) |
| 62 | + policy.start_retry_timer() |
| 63 | + time.sleep(0.25) |
| 64 | + # Even with a small backoff, check_proposed_wait should raise |
| 65 | + with pytest.raises(MaxRetryDurationError): |
| 66 | + policy.check_proposed_wait(0.1) |
0 commit comments