|
3 | 3 | import time |
4 | 4 | import unittest |
5 | 5 |
|
6 | | -from mock import patch |
| 6 | +from mock import patch, Mock |
7 | 7 | import pytest |
8 | 8 |
|
9 | 9 | import polling2 |
@@ -95,6 +95,20 @@ def test_max_call_no_sleep(self): |
95 | 95 | polling2.poll(lambda: False, step=sleep, max_tries=tries) |
96 | 96 | assert time.time() - start_time < tries * sleep, 'Poll function slept before MaxCallException' |
97 | 97 |
|
| 98 | + def test_ignore_specified_exceptions(self): |
| 99 | + """ |
| 100 | + Test that ignore_exceptions tuple will ignore exceptions specified. |
| 101 | + Should throw any errors not in the tuple. |
| 102 | + """ |
| 103 | + # raises_errors is a function that returns 3 different things, each time it is called. |
| 104 | + # First it raises a ValueError, then EOFError, then a TypeError. |
| 105 | + raises_errors = Mock(return_value=True, side_effect=[ValueError, EOFError, RuntimeError]) |
| 106 | + with pytest.raises(RuntimeError): |
| 107 | + # We are ignoring the exceptions other than a TypeError. |
| 108 | + polling2.poll(target=raises_errors, step=0.1, max_tries=3, |
| 109 | + ignore_exceptions=(ValueError, EOFError)) |
| 110 | + assert raises_errors.call_count == 3 |
| 111 | + |
98 | 112 |
|
99 | 113 | @pytest.mark.skipif(is_py_34(), reason="pytest logcap fixture isn't available on 3.4") |
100 | 114 | class TestPollLogging(object): |
@@ -128,3 +142,34 @@ def test_default_is_not_log(self, caplog): |
128 | 142 | with caplog.at_level(logging.DEBUG): |
129 | 143 | polling2.poll(target=lambda: True, step=0.1, max_tries=1) |
130 | 144 | assert len(caplog.records) == 0, "Should not be any log records" |
| 145 | + |
| 146 | + def test_log_error_default_is_not_log(self, caplog): |
| 147 | + """ |
| 148 | + Shouldn't log anything unless explicitly asked to do so. |
| 149 | + """ |
| 150 | + raises_errors = Mock(side_effect=ValueError('msg is this')) |
| 151 | + with caplog.at_level(logging.DEBUG), pytest.raises(polling2.MaxCallException): |
| 152 | + polling2.poll(target=raises_errors, ignore_exceptions=(ValueError), |
| 153 | + step=0.1, max_tries=2) |
| 154 | + assert len(caplog.records) == 0, "Wrong number of log records." |
| 155 | + # Test that logging.NOTSET does not print log records either. |
| 156 | + polling2.poll(target=raises_errors, ignore_exceptions=(ValueError), |
| 157 | + step=0.1, max_tries=2, log_error=logging.NOTSET) |
| 158 | + assert len(caplog.records) == 0, "Wrong number of log records." |
| 159 | + |
| 160 | + def test_log_error_set_at_debug_level(self, caplog): |
| 161 | + """ |
| 162 | + Test that when the log_error parameter is set to debug level, the ignored |
| 163 | + errors are sent to the logger. |
| 164 | + """ |
| 165 | + raises_errors = Mock(side_effect=[ValueError('msg this'), RuntimeError('this msg')]) |
| 166 | + with caplog.at_level(logging.DEBUG), pytest.raises(polling2.MaxCallException): |
| 167 | + polling2.poll(target=raises_errors, ignore_exceptions=(ValueError, RuntimeError), |
| 168 | + step=0.1, max_tries=2, log_error=logging.DEBUG) |
| 169 | + assert len(caplog.records) == 2, "Wrong number of log records." |
| 170 | + assert caplog.records[0].message == "poll() ignored exception ValueError('msg this',)" |
| 171 | + assert caplog.records[1].message == "poll() ignored exception RuntimeError('this msg',)" |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
0 commit comments