|
| 1 | +import time |
| 2 | +import unittest |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +from avocado.utils import wait |
| 6 | + |
| 7 | + |
| 8 | +class WaitForTest(unittest.TestCase): |
| 9 | + """Unit tests for wait.wait_for function.""" |
| 10 | + |
| 11 | + def test_basic_success_immediate(self): |
| 12 | + """Test wait_for with function that succeeds immediately.""" |
| 13 | + func = mock.Mock(return_value=True) |
| 14 | + result = wait.wait_for(func, timeout=5) |
| 15 | + self.assertTrue(result) |
| 16 | + func.assert_called_once() |
| 17 | + |
| 18 | + def test_basic_success_with_value(self): |
| 19 | + """Test wait_for returns the truthy value from function.""" |
| 20 | + func = mock.Mock(return_value="success") |
| 21 | + result = wait.wait_for(func, timeout=5) |
| 22 | + self.assertEqual(result, "success") |
| 23 | + |
| 24 | + def test_timeout_returns_none(self): |
| 25 | + """Test wait_for returns None when timeout expires.""" |
| 26 | + func = mock.Mock(return_value=False) |
| 27 | + start = time.time() |
| 28 | + result = wait.wait_for(func, timeout=0.5, step=0.1) |
| 29 | + elapsed = time.time() - start |
| 30 | + self.assertIsNone(result) |
| 31 | + self.assertGreaterEqual(elapsed, 0.5) |
| 32 | + self.assertLess(elapsed, 0.7) # Should not wait much longer than timeout |
| 33 | + |
| 34 | + def test_function_eventually_succeeds(self): |
| 35 | + """Test wait_for succeeds when function returns True after retries.""" |
| 36 | + call_count = {"count": 0} |
| 37 | + |
| 38 | + def func_on_third_call(): |
| 39 | + call_count["count"] += 1 |
| 40 | + return call_count["count"] >= 3 |
| 41 | + |
| 42 | + result = wait.wait_for(func_on_third_call, timeout=5, step=0.1) |
| 43 | + self.assertTrue(result) |
| 44 | + self.assertEqual(call_count["count"], 3) |
| 45 | + |
| 46 | + def test_first_delay(self): |
| 47 | + """Test wait_for respects first delay parameter.""" |
| 48 | + func = mock.Mock(return_value=True) |
| 49 | + start = time.time() |
| 50 | + wait.wait_for(func, timeout=5, first=0.2) |
| 51 | + elapsed = time.time() - start |
| 52 | + self.assertGreaterEqual(elapsed, 0.2) |
| 53 | + |
| 54 | + def test_step_interval(self): |
| 55 | + """Test wait_for respects step interval between attempts.""" |
| 56 | + func = mock.Mock(return_value=False) |
| 57 | + wait.wait_for(func, timeout=0.5, step=0.15, first=0.0) |
| 58 | + # Should make roughly 0.5/0.15 = 3-4 attempts |
| 59 | + call_count = func.call_count |
| 60 | + self.assertGreaterEqual(call_count, 3) |
| 61 | + self.assertLessEqual(call_count, 5) |
| 62 | + |
| 63 | + def test_zero_timeout(self): |
| 64 | + """Test wait_for with zero timeout.""" |
| 65 | + func = mock.Mock(return_value=False) |
| 66 | + result = wait.wait_for(func, timeout=0) |
| 67 | + self.assertIsNone(result) |
| 68 | + |
| 69 | + def test_with_positional_args(self): |
| 70 | + """Test wait_for passes positional arguments to function.""" |
| 71 | + func = mock.Mock(return_value=True) |
| 72 | + wait.wait_for(func, timeout=5, args=["arg1", "arg2", 3]) |
| 73 | + func.assert_called_with("arg1", "arg2", 3) |
| 74 | + |
| 75 | + def test_with_keyword_args(self): |
| 76 | + """Test wait_for passes keyword arguments to function.""" |
| 77 | + func = mock.Mock(return_value=True) |
| 78 | + wait.wait_for(func, timeout=5, kwargs={"key1": "value1", "key2": 42}) |
| 79 | + func.assert_called_with(key1="value1", key2=42) |
| 80 | + |
| 81 | + def test_with_both_args_and_kwargs(self): |
| 82 | + """Test wait_for passes both positional and keyword arguments.""" |
| 83 | + func = mock.Mock(return_value=True) |
| 84 | + wait.wait_for( |
| 85 | + func, timeout=5, args=["pos1", "pos2"], kwargs={"kw1": "val1", "kw2": 99} |
| 86 | + ) |
| 87 | + func.assert_called_with("pos1", "pos2", kw1="val1", kw2=99) |
| 88 | + |
| 89 | + def test_text_logging(self): |
| 90 | + """Test wait_for logs debug messages when text is provided.""" |
| 91 | + func = mock.Mock(return_value=False) |
| 92 | + with self.assertLogs("avocado.utils.wait", level="DEBUG") as log_context: |
| 93 | + wait.wait_for(func, timeout=0.3, step=0.1, text="Waiting for condition") |
| 94 | + # Should have logged at least once |
| 95 | + self.assertGreater(len(log_context.output), 0) |
| 96 | + self.assertIn("Waiting for condition", log_context.output[0]) |
| 97 | + |
| 98 | + def test_no_logging_without_text(self): |
| 99 | + """Test wait_for does not log when text is None.""" |
| 100 | + func = mock.Mock(return_value=False) |
| 101 | + with self.assertRaises(AssertionError): |
| 102 | + # Should not log anything |
| 103 | + with self.assertLogs("avocado.utils.wait", level="DEBUG"): |
| 104 | + wait.wait_for(func, timeout=0.2, step=0.1, text=None) |
| 105 | + |
| 106 | + def test_returns_first_truthy_value(self): |
| 107 | + """Test wait_for returns first truthy value encountered.""" |
| 108 | + values = [False, 0, None, "", "found"] |
| 109 | + |
| 110 | + def func_with_sequence(): |
| 111 | + return values.pop(0) |
| 112 | + |
| 113 | + result = wait.wait_for(func_with_sequence, timeout=5, step=0.05) |
| 114 | + self.assertEqual(result, "found") |
| 115 | + # Should have values left since it stopped early |
| 116 | + self.assertEqual(len(values), 0) |
| 117 | + |
| 118 | + def test_function_returns_zero(self): |
| 119 | + """Test wait_for treats zero as falsy and continues waiting.""" |
| 120 | + func = mock.Mock(return_value=0) |
| 121 | + result = wait.wait_for(func, timeout=0.2, step=0.05) |
| 122 | + self.assertIsNone(result) |
| 123 | + self.assertGreater(func.call_count, 1) |
| 124 | + |
| 125 | + def test_function_returns_empty_string(self): |
| 126 | + """Test wait_for treats empty string as falsy.""" |
| 127 | + func = mock.Mock(return_value="") |
| 128 | + result = wait.wait_for(func, timeout=0.2, step=0.05) |
| 129 | + self.assertIsNone(result) |
| 130 | + |
| 131 | + def test_function_returns_list(self): |
| 132 | + """Test wait_for can return list when function returns truthy list.""" |
| 133 | + func = mock.Mock(return_value=["item1", "item2"]) |
| 134 | + result = wait.wait_for(func, timeout=5) |
| 135 | + self.assertEqual(result, ["item1", "item2"]) |
| 136 | + |
| 137 | + def test_function_returns_dict(self): |
| 138 | + """Test wait_for can return dict when function returns truthy dict.""" |
| 139 | + expected = {"key": "value"} |
| 140 | + func = mock.Mock(return_value=expected) |
| 141 | + result = wait.wait_for(func, timeout=5) |
| 142 | + self.assertEqual(result, expected) |
| 143 | + |
| 144 | + def test_function_raises_exception(self): |
| 145 | + """Test wait_for propagates exceptions from the function.""" |
| 146 | + func = mock.Mock(side_effect=ValueError("Test error")) |
| 147 | + with self.assertRaises(ValueError) as context: |
| 148 | + wait.wait_for(func, timeout=5) |
| 149 | + self.assertEqual(str(context.exception), "Test error") |
| 150 | + |
| 151 | + def test_negative_timeout(self): |
| 152 | + """Test wait_for with negative timeout.""" |
| 153 | + func = mock.Mock(return_value=False) |
| 154 | + result = wait.wait_for(func, timeout=-1) |
| 155 | + self.assertIsNone(result) |
| 156 | + |
| 157 | + def test_large_step_vs_timeout(self): |
| 158 | + """Test wait_for when step is larger than timeout. |
| 159 | +
|
| 160 | + Note: The function sleeps for full step duration even if it exceeds timeout. |
| 161 | + """ |
| 162 | + func = mock.Mock(return_value=False) |
| 163 | + start = time.time() |
| 164 | + result = wait.wait_for(func, timeout=0.2, step=5.0) |
| 165 | + elapsed = time.time() - start |
| 166 | + self.assertIsNone(result) |
| 167 | + # Function will sleep for full step duration after first attempt |
| 168 | + self.assertGreaterEqual(elapsed, 5.0) |
| 169 | + # Should be called once before sleeping |
| 170 | + self.assertEqual(func.call_count, 1) |
| 171 | + |
| 172 | + def test_very_small_timeout(self): |
| 173 | + """Test wait_for with very small timeout value.""" |
| 174 | + func = mock.Mock(return_value=False) |
| 175 | + result = wait.wait_for(func, timeout=0.01, step=0.001) |
| 176 | + self.assertIsNone(result) |
| 177 | + |
| 178 | + def test_none_args_and_kwargs(self): |
| 179 | + """Test wait_for with None values for args and kwargs.""" |
| 180 | + func = mock.Mock(return_value=True) |
| 181 | + result = wait.wait_for(func, timeout=5, args=None, kwargs=None) |
| 182 | + self.assertTrue(result) |
| 183 | + func.assert_called_once_with() |
| 184 | + |
| 185 | + def test_empty_args_and_kwargs(self): |
| 186 | + """Test wait_for with empty args and kwargs.""" |
| 187 | + func = mock.Mock(return_value=True) |
| 188 | + result = wait.wait_for(func, timeout=5, args=[], kwargs={}) |
| 189 | + self.assertTrue(result) |
| 190 | + func.assert_called_once_with() |
| 191 | + |
| 192 | + def test_callable_object(self): |
| 193 | + """Test wait_for works with callable objects, not just functions.""" |
| 194 | + |
| 195 | + class CallableCounter: |
| 196 | + def __init__(self): |
| 197 | + self.count = 0 |
| 198 | + |
| 199 | + def __call__(self): |
| 200 | + self.count += 1 |
| 201 | + return self.count >= 3 |
| 202 | + |
| 203 | + callable_obj = CallableCounter() |
| 204 | + result = wait.wait_for(callable_obj, timeout=5, step=0.1) |
| 205 | + self.assertTrue(result) |
| 206 | + self.assertEqual(callable_obj.count, 3) |
| 207 | + |
| 208 | + def test_lambda_function(self): |
| 209 | + """Test wait_for works with lambda functions.""" |
| 210 | + counter = {"value": 0} |
| 211 | + |
| 212 | + def increment(): |
| 213 | + counter["value"] += 1 |
| 214 | + return counter["value"] >= 2 |
| 215 | + |
| 216 | + result = wait.wait_for(increment, timeout=5, step=0.1) |
| 217 | + self.assertTrue(result) |
| 218 | + |
| 219 | + def test_timing_precision(self): |
| 220 | + """Test wait_for timeout is reasonably accurate.""" |
| 221 | + func = mock.Mock(return_value=False) |
| 222 | + timeout_val = 1.0 |
| 223 | + start = time.time() |
| 224 | + wait.wait_for(func, timeout=timeout_val, step=0.1) |
| 225 | + elapsed = time.time() - start |
| 226 | + # Should be close to timeout (within 20% tolerance for system variance) |
| 227 | + self.assertGreaterEqual(elapsed, timeout_val) |
| 228 | + self.assertLess(elapsed, timeout_val * 1.2) |
| 229 | + |
| 230 | + |
| 231 | +if __name__ == "__main__": |
| 232 | + unittest.main() |
0 commit comments