|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import types as base_types |
| 16 | + |
15 | 17 | from google.auth import credentials |
16 | 18 | import mock |
17 | 19 | import pytest |
@@ -87,18 +89,61 @@ def test_blocking_consume_keyboard_interrupt(): |
87 | 89 | on_res.assert_called_once_with(consumer._policy, mock.sentinel.A) |
88 | 90 |
|
89 | 91 |
|
90 | | -@mock.patch.object(thread.Policy, 'call_rpc', autospec=True) |
91 | | -@mock.patch.object(thread.Policy, 'on_response', autospec=True) |
92 | | -@mock.patch.object(thread.Policy, 'on_exception', autospec=True) |
93 | | -def test_blocking_consume_exception_reraise(on_exc, on_res, call_rpc): |
94 | | - consumer = create_consumer() |
| 92 | +class OnException(object): |
| 93 | + |
| 94 | + def __init__(self, exiting_event, acceptable=None): |
| 95 | + self.exiting_event = exiting_event |
| 96 | + self.acceptable = acceptable |
| 97 | + |
| 98 | + def __call__(self, exception): |
| 99 | + if exception is self.acceptable: |
| 100 | + return True |
| 101 | + else: |
| 102 | + self.exiting_event.set() |
| 103 | + return False |
| 104 | + |
| 105 | + |
| 106 | +def test_blocking_consume_on_exception(): |
| 107 | + policy = mock.Mock(spec=('call_rpc', 'on_response', 'on_exception')) |
| 108 | + policy.call_rpc.return_value = (mock.sentinel.A, mock.sentinel.B) |
| 109 | + exc = TypeError('Bad things!') |
| 110 | + policy.on_response.side_effect = exc |
| 111 | + |
| 112 | + consumer = _consumer.Consumer(policy=policy) |
| 113 | + policy.on_exception.side_effect = OnException(consumer._exiting) |
| 114 | + |
| 115 | + # Establish that we get responses until we are sent the exiting event. |
| 116 | + consumer._blocking_consume() |
| 117 | + |
| 118 | + # Check mocks. |
| 119 | + policy.call_rpc.assert_called_once() |
| 120 | + policy.on_response.assert_called_once_with(mock.sentinel.A) |
| 121 | + policy.on_exception.assert_called_once_with(exc) |
| 122 | + |
| 123 | + |
| 124 | +def test_blocking_consume_two_exceptions(): |
| 125 | + policy = mock.Mock(spec=('call_rpc', 'on_response', 'on_exception')) |
| 126 | + policy.call_rpc.side_effect = ( |
| 127 | + (mock.sentinel.A,), |
| 128 | + (mock.sentinel.B,), |
| 129 | + ) |
| 130 | + exc1 = NameError('Oh noes.') |
| 131 | + exc2 = ValueError('Something grumble.') |
| 132 | + policy.on_response.side_effect = (exc1, exc2) |
| 133 | + |
| 134 | + consumer = _consumer.Consumer(policy=policy) |
| 135 | + policy.on_exception.side_effect = OnException( |
| 136 | + consumer._exiting, acceptable=exc1) |
95 | 137 |
|
96 | 138 | # Establish that we get responses until we are sent the exiting event. |
97 | | - call_rpc.return_value = (mock.sentinel.A, mock.sentinel.B) |
98 | | - on_res.side_effect = TypeError('Bad things!') |
99 | | - on_exc.side_effect = on_res.side_effect |
100 | | - with pytest.raises(TypeError): |
101 | | - consumer._blocking_consume() |
| 139 | + consumer._blocking_consume() |
| 140 | + |
| 141 | + # Check mocks. |
| 142 | + assert policy.call_rpc.call_count == 2 |
| 143 | + policy.on_response.assert_has_calls( |
| 144 | + [mock.call(mock.sentinel.A), mock.call(mock.sentinel.B)]) |
| 145 | + policy.on_exception.assert_has_calls( |
| 146 | + [mock.call(exc1), mock.call(exc2)]) |
102 | 147 |
|
103 | 148 |
|
104 | 149 | def test_start_consuming(): |
|
0 commit comments