|
| 1 | +# Copyright (c) "Neo4j" |
| 2 | +# Neo4j Sweden AB [https://neo4j.com] |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License.import asyncio |
| 15 | + |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from neo4j._exceptions import SocketDeadlineExceeded |
| 20 | +from neo4j.io._common import Inbox |
| 21 | +from neo4j.packstream import Unpacker |
| 22 | + |
| 23 | + |
| 24 | +def _on_error_side_effect(e): |
| 25 | + raise e |
| 26 | + |
| 27 | + |
| 28 | +class InboxMockHolder: |
| 29 | + def __init__(self, mocker): |
| 30 | + self.socket_mock = mocker.Mock() |
| 31 | + self.socket_mock.getsockname.return_value = ("host", 1234) |
| 32 | + self.on_error = mocker.MagicMock() |
| 33 | + self.on_error.side_effect = _on_error_side_effect |
| 34 | + self.inbox = Inbox(self.socket_mock, self.on_error) |
| 35 | + self.unpacker = None |
| 36 | + self._unpacker_failure = None |
| 37 | + mocker.patch( |
| 38 | + "neo4j.io._common.Unpacker", |
| 39 | + new=self._make_unpacker, |
| 40 | + ) |
| 41 | + self.inbox._unpacker = self.unpacker |
| 42 | + # plenty of nonsense messages to read |
| 43 | + self.mock_set_data(b"\x00\x01\xff\x00\x00" * 1000) |
| 44 | + self._mocker = mocker |
| 45 | + |
| 46 | + def _make_unpacker(self, buffer): |
| 47 | + if self.unpacker is not None: |
| 48 | + pytest.fail("Unexpected 2nd instantiation of Unpacker") |
| 49 | + self.unpacker = self._mocker.Mock(wraps=Unpacker(buffer)) |
| 50 | + if self._unpacker_failure is not None: |
| 51 | + self.mock_unpack_failure(self._unpacker_failure) |
| 52 | + return self.unpacker |
| 53 | + |
| 54 | + def mock_set_data(self, data): |
| 55 | + def side_effect(buffer, n): |
| 56 | + nonlocal data |
| 57 | + |
| 58 | + if not data: |
| 59 | + pytest.fail("Read more data than mocked") |
| 60 | + |
| 61 | + n = min(len(data), len(buffer), n) |
| 62 | + buffer[:n] = data[:n] |
| 63 | + data = data[n:] |
| 64 | + return n |
| 65 | + |
| 66 | + self.socket_mock.recv_into.side_effect = side_effect |
| 67 | + |
| 68 | + def assert_no_error(self): |
| 69 | + self.on_error.assert_not_called() |
| 70 | + assert next(self.inbox, None) is not None |
| 71 | + |
| 72 | + def mock_receive_failure(self, exception): |
| 73 | + self.socket_mock.recv_into.side_effect = exception |
| 74 | + |
| 75 | + def mock_unpack_failure(self, exception): |
| 76 | + self._unpacker_failure = exception |
| 77 | + if self.unpacker is not None: |
| 78 | + self.unpacker.unpack_structure_header.side_effect = exception |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize( |
| 82 | + "error", |
| 83 | + ( |
| 84 | + SocketDeadlineExceeded("test"), |
| 85 | + OSError("test"), |
| 86 | + ), |
| 87 | +) |
| 88 | +def test_inbox_receive_failure_error_handler(mocker, error): |
| 89 | + mocks = InboxMockHolder(mocker) |
| 90 | + mocks.mock_receive_failure(error) |
| 91 | + inbox = mocks.inbox |
| 92 | + |
| 93 | + with pytest.raises(type(error)) as exc: |
| 94 | + next(inbox) |
| 95 | + |
| 96 | + assert exc.value is error |
| 97 | + mocks.on_error.assert_called_once_with(error) |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize( |
| 101 | + "error", |
| 102 | + ( |
| 103 | + SocketDeadlineExceeded("test"), |
| 104 | + OSError("test"), |
| 105 | + RecursionError("2deep4u"), |
| 106 | + RuntimeError("something funny happened"), |
| 107 | + ), |
| 108 | +) |
| 109 | +def test_inbox_unpack_failure(mocker, error): |
| 110 | + mocks = InboxMockHolder(mocker) |
| 111 | + mocks.mock_unpack_failure(error) |
| 112 | + inbox = mocks.inbox |
| 113 | + |
| 114 | + with pytest.raises(type(error)) as exc: |
| 115 | + next(inbox) |
| 116 | + |
| 117 | + assert exc.value is error |
| 118 | + mocks.on_error.assert_called_once_with(error) |
0 commit comments