diff options
author | Matus Valo <matusvalo@users.noreply.github.com> | 2018-11-07 15:21:55 +0100 |
---|---|---|
committer | Omer Katz <omer.drow@gmail.com> | 2018-11-07 16:21:55 +0200 |
commit | b4c51dfdb448e30627c7888cb74461ec0184d694 (patch) | |
tree | 39f4a606f798431ee4a9a6956018f67cfce5127f | |
parent | b0bc72fa09ad36635f61161e357b8a0b633cf6fb (diff) | |
download | py-amqp-b4c51dfdb448e30627c7888cb74461ec0184d694.tar.gz |
Added unittest for having_timeout (#217)
* Added unittest for having_timeout
* Added comments describing unit tests
-rw-r--r-- | t/unit/test_transport.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/t/unit/test_transport.py b/t/unit/test_transport.py index 9e4e27c..48bac65 100644 --- a/t/unit/test_transport.py +++ b/t/unit/test_transport.py @@ -12,6 +12,10 @@ from amqp.platform import pack from amqp.transport import _AbstractTransport +class DummyException(Exception): + pass + + class MockSocket(object): options = {} @@ -354,9 +358,51 @@ class test_AbstractTransport: assert not self.t.connected def test_having_timeout_none(self): + # Checks that context manager does nothing when no timeout is provided with self.t.having_timeout(None) as actual_sock: assert actual_sock == self.t.sock + def test_set_timeout(self): + # Checks that context manager sets and reverts timeout properly + with patch.object(self.t, 'sock') as sock_mock: + sock_mock.gettimeout.return_value = 3 + with self.t.having_timeout(5) as actual_sock: + assert actual_sock == self.t.sock + sock_mock.gettimeout.assert_called() + sock_mock.settimeout.assert_has_calls( + [ + call(5), + call(3), + ] + ) + + def test_set_timeout_exception_raised(self): + # Checks that context manager sets and reverts timeout properly + # when exception is raised. + with patch.object(self.t, 'sock') as sock_mock: + sock_mock.gettimeout.return_value = 3 + with pytest.raises(DummyException): + with self.t.having_timeout(5) as actual_sock: + assert actual_sock == self.t.sock + raise DummyException() + sock_mock.gettimeout.assert_called() + sock_mock.settimeout.assert_has_calls( + [ + call(5), + call(3), + ] + ) + + def test_set_same_timeout(self): + # Checks that context manager does not set timeout when + # it is same as currently set. + with patch.object(self.t, 'sock') as sock_mock: + sock_mock.gettimeout.return_value = 5 + with self.t.having_timeout(5) as actual_sock: + assert actual_sock == self.t.sock + sock_mock.gettimeout.assert_called() + sock_mock.settimeout.assert_not_called() + class test_AbstractTransport_connect: |