|
3 | 3 |
|
4 | 4 | import pytest
|
5 | 5 |
|
| 6 | +from redis.connection import Connection |
6 | 7 | from redis.exceptions import InvalidResponse
|
7 | 8 | from redis.utils import HIREDIS_AVAILABLE
|
8 | 9 |
|
@@ -40,3 +41,36 @@ def inner():
|
40 | 41 | # mod = j(modclient)
|
41 | 42 | # mod.set("fookey", ".", d)
|
42 | 43 | # assert mod.get('fookey') == d
|
| 44 | + |
| 45 | + |
| 46 | +class TestConnection: |
| 47 | + def test_disconnect(self): |
| 48 | + conn = Connection() |
| 49 | + mock_sock = mock.Mock() |
| 50 | + conn._sock = mock_sock |
| 51 | + conn.disconnect() |
| 52 | + mock_sock.shutdown.assert_called_once() |
| 53 | + mock_sock.close.assert_called_once() |
| 54 | + assert conn._sock is None |
| 55 | + |
| 56 | + def test_disconnect__shutdown_OSError(self): |
| 57 | + """An OSError on socket shutdown will still close the socket.""" |
| 58 | + conn = Connection() |
| 59 | + mock_sock = mock.Mock() |
| 60 | + conn._sock = mock_sock |
| 61 | + conn._sock.shutdown.side_effect = OSError |
| 62 | + conn.disconnect() |
| 63 | + mock_sock.shutdown.assert_called_once() |
| 64 | + mock_sock.close.assert_called_once() |
| 65 | + assert conn._sock is None |
| 66 | + |
| 67 | + def test_disconnect__close_OSError(self): |
| 68 | + """An OSError on socket close will still clear out the socket.""" |
| 69 | + conn = Connection() |
| 70 | + mock_sock = mock.Mock() |
| 71 | + conn._sock = mock_sock |
| 72 | + conn._sock.close.side_effect = OSError |
| 73 | + conn.disconnect() |
| 74 | + mock_sock.shutdown.assert_called_once() |
| 75 | + mock_sock.close.assert_called_once() |
| 76 | + assert conn._sock is None |
0 commit comments