|
| 1 | +import pytest |
| 2 | + |
| 3 | +from conductor.shared.worker.task_options import ( |
| 4 | + TaskOptions, |
| 5 | + get_task_options, |
| 6 | + task_options, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +def test_task_options_all_parameters(): |
| 11 | + options = TaskOptions( |
| 12 | + timeout_seconds=120, |
| 13 | + response_timeout_seconds=60, |
| 14 | + poll_timeout_seconds=30, |
| 15 | + retry_count=3, |
| 16 | + retry_logic="LINEAR_BACKOFF", |
| 17 | + retry_delay_seconds=1, |
| 18 | + backoff_scale_factor=2, |
| 19 | + rate_limit_per_frequency=100, |
| 20 | + rate_limit_frequency_in_seconds=10, |
| 21 | + concurrent_exec_limit=5, |
| 22 | + timeout_policy="TIME_OUT_WF", |
| 23 | + owner_email="test@example.com", |
| 24 | + description="Test task", |
| 25 | + ) |
| 26 | + |
| 27 | + assert options.timeout_seconds == 120 |
| 28 | + assert options.response_timeout_seconds == 60 |
| 29 | + assert options.poll_timeout_seconds == 30 |
| 30 | + assert options.retry_count == 3 |
| 31 | + assert options.retry_logic == "LINEAR_BACKOFF" |
| 32 | + assert options.retry_delay_seconds == 1 |
| 33 | + assert options.backoff_scale_factor == 2 |
| 34 | + assert options.rate_limit_per_frequency == 100 |
| 35 | + assert options.rate_limit_frequency_in_seconds == 10 |
| 36 | + assert options.concurrent_exec_limit == 5 |
| 37 | + assert options.timeout_policy == "TIME_OUT_WF" |
| 38 | + assert options.owner_email == "test@example.com" |
| 39 | + assert options.description == "Test task" |
| 40 | + |
| 41 | + |
| 42 | +def test_task_options_partial_parameters(): |
| 43 | + options = TaskOptions( |
| 44 | + timeout_seconds=120, |
| 45 | + retry_count=3, |
| 46 | + ) |
| 47 | + |
| 48 | + assert options.timeout_seconds == 120 |
| 49 | + assert options.retry_count == 3 |
| 50 | + assert options.response_timeout_seconds is None |
| 51 | + assert options.poll_timeout_seconds is None |
| 52 | + |
| 53 | + |
| 54 | +def test_task_options_to_dict(): |
| 55 | + options = TaskOptions( |
| 56 | + timeout_seconds=120, |
| 57 | + retry_count=3, |
| 58 | + retry_logic="LINEAR_BACKOFF", |
| 59 | + ) |
| 60 | + |
| 61 | + result = options.to_dict() |
| 62 | + |
| 63 | + assert result["timeout_seconds"] == 120 |
| 64 | + assert result["retry_count"] == 3 |
| 65 | + assert result["retry_logic"] == "LINEAR_BACKOFF" |
| 66 | + assert "response_timeout_seconds" not in result |
| 67 | + |
| 68 | + |
| 69 | +def test_task_options_validation_negative_timeout(): |
| 70 | + with pytest.raises(ValueError, match="timeout_seconds must be >= 0"): |
| 71 | + TaskOptions(timeout_seconds=-1) |
| 72 | + |
| 73 | + |
| 74 | +def test_task_options_validation_response_timeout_zero(): |
| 75 | + with pytest.raises(ValueError, match="response_timeout_seconds must be >= 1"): |
| 76 | + TaskOptions(response_timeout_seconds=0) |
| 77 | + |
| 78 | + |
| 79 | +def test_task_options_validation_negative_retry_count(): |
| 80 | + with pytest.raises(ValueError, match="retry_count must be >= 0"): |
| 81 | + TaskOptions(retry_count=-1) |
| 82 | + |
| 83 | + |
| 84 | +def test_task_options_validation_invalid_retry_logic(): |
| 85 | + with pytest.raises(ValueError, match="retry_logic must be one of"): |
| 86 | + TaskOptions(retry_logic="INVALID") |
| 87 | + |
| 88 | + |
| 89 | +def test_task_options_validation_valid_retry_logics(): |
| 90 | + for logic in ["FIXED", "LINEAR_BACKOFF", "EXPONENTIAL_BACKOFF"]: |
| 91 | + options = TaskOptions(retry_logic=logic) |
| 92 | + assert options.retry_logic == logic |
| 93 | + |
| 94 | + |
| 95 | +def test_task_options_validation_invalid_timeout_policy(): |
| 96 | + with pytest.raises(ValueError, match="timeout_policy must be one of"): |
| 97 | + TaskOptions(timeout_policy="INVALID") |
| 98 | + |
| 99 | + |
| 100 | +def test_task_options_validation_valid_timeout_policies(): |
| 101 | + for policy in ["TIME_OUT_WF", "ALERT_ONLY", "RETRY"]: |
| 102 | + options = TaskOptions(timeout_policy=policy) |
| 103 | + assert options.timeout_policy == policy |
| 104 | + |
| 105 | + |
| 106 | +def test_task_options_validation_negative_backoff_scale_factor(): |
| 107 | + with pytest.raises(ValueError, match="backoff_scale_factor must be >= 1"): |
| 108 | + TaskOptions(backoff_scale_factor=0) |
| 109 | + |
| 110 | + |
| 111 | +def test_task_options_validation_negative_rate_limit(): |
| 112 | + with pytest.raises(ValueError, match="rate_limit_per_frequency must be >= 0"): |
| 113 | + TaskOptions(rate_limit_per_frequency=-1) |
| 114 | + |
| 115 | + |
| 116 | +def test_task_options_validation_negative_concurrent_exec_limit(): |
| 117 | + with pytest.raises(ValueError, match="concurrent_exec_limit must be >= 0"): |
| 118 | + TaskOptions(concurrent_exec_limit=-1) |
| 119 | + |
| 120 | + |
| 121 | +def test_task_options_decorator_basic(): |
| 122 | + @task_options(timeout_seconds=120, retry_count=3) |
| 123 | + def my_task(_input_data): |
| 124 | + return {"result": "success"} |
| 125 | + |
| 126 | + options = get_task_options(my_task) |
| 127 | + |
| 128 | + assert options is not None |
| 129 | + assert options.timeout_seconds == 120 |
| 130 | + assert options.retry_count == 3 |
| 131 | + |
| 132 | + |
| 133 | +def test_task_options_decorator_all_parameters(): |
| 134 | + @task_options( |
| 135 | + timeout_seconds=120, |
| 136 | + response_timeout_seconds=60, |
| 137 | + poll_timeout_seconds=30, |
| 138 | + retry_count=3, |
| 139 | + retry_logic="LINEAR_BACKOFF", |
| 140 | + retry_delay_seconds=1, |
| 141 | + backoff_scale_factor=2, |
| 142 | + rate_limit_per_frequency=100, |
| 143 | + rate_limit_frequency_in_seconds=10, |
| 144 | + concurrent_exec_limit=5, |
| 145 | + timeout_policy="TIME_OUT_WF", |
| 146 | + owner_email="test@example.com", |
| 147 | + description="Test task", |
| 148 | + ) |
| 149 | + def my_task(_input_data): |
| 150 | + return {"result": "success"} |
| 151 | + |
| 152 | + options = get_task_options(my_task) |
| 153 | + |
| 154 | + assert options.timeout_seconds == 120 |
| 155 | + assert options.response_timeout_seconds == 60 |
| 156 | + assert options.poll_timeout_seconds == 30 |
| 157 | + assert options.retry_count == 3 |
| 158 | + assert options.retry_logic == "LINEAR_BACKOFF" |
| 159 | + assert options.retry_delay_seconds == 1 |
| 160 | + assert options.backoff_scale_factor == 2 |
| 161 | + assert options.rate_limit_per_frequency == 100 |
| 162 | + assert options.rate_limit_frequency_in_seconds == 10 |
| 163 | + assert options.concurrent_exec_limit == 5 |
| 164 | + assert options.timeout_policy == "TIME_OUT_WF" |
| 165 | + assert options.owner_email == "test@example.com" |
| 166 | + assert options.description == "Test task" |
| 167 | + |
| 168 | + |
| 169 | +def test_task_options_decorator_function_still_callable(): |
| 170 | + @task_options(timeout_seconds=120) |
| 171 | + def my_task(input_data): |
| 172 | + return {"result": input_data} |
| 173 | + |
| 174 | + result = my_task("test") |
| 175 | + assert result == {"result": "test"} |
| 176 | + |
| 177 | + |
| 178 | +def test_task_options_decorator_function_name_preserved(): |
| 179 | + @task_options(timeout_seconds=120) |
| 180 | + def my_task(_input_data): |
| 181 | + return {"result": "success"} |
| 182 | + |
| 183 | + assert my_task.__name__ == "my_task" |
| 184 | + |
| 185 | + |
| 186 | +def test_get_task_options_no_decorator(): |
| 187 | + def my_task(_input_data): |
| 188 | + return {"result": "success"} |
| 189 | + |
| 190 | + options = get_task_options(my_task) |
| 191 | + assert options is None |
| 192 | + |
| 193 | + |
| 194 | +def test_task_options_decorator_validation_error(): |
| 195 | + with pytest.raises(ValueError, match="timeout_seconds must be >= 0"): |
| 196 | + |
| 197 | + @task_options(timeout_seconds=-1) |
| 198 | + def my_task(_input_data): |
| 199 | + return {"result": "success"} |
0 commit comments