Skip to content

Commit b4c51df

Browse files
matusvaloOmer Katz
authored and
Omer Katz
committed
Added unittest for having_timeout (#217)
* Added unittest for having_timeout * Added comments describing unit tests
1 parent b0bc72f commit b4c51df

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

t/unit/test_transport.py

+46
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
from amqp.transport import _AbstractTransport
1313

1414

15+
class DummyException(Exception):
16+
pass
17+
18+
1519
class MockSocket(object):
1620
options = {}
1721

@@ -354,9 +358,51 @@ def test_write__EINTR(self):
354358
assert not self.t.connected
355359

356360
def test_having_timeout_none(self):
361+
# Checks that context manager does nothing when no timeout is provided
357362
with self.t.having_timeout(None) as actual_sock:
358363
assert actual_sock == self.t.sock
359364

365+
def test_set_timeout(self):
366+
# Checks that context manager sets and reverts timeout properly
367+
with patch.object(self.t, 'sock') as sock_mock:
368+
sock_mock.gettimeout.return_value = 3
369+
with self.t.having_timeout(5) as actual_sock:
370+
assert actual_sock == self.t.sock
371+
sock_mock.gettimeout.assert_called()
372+
sock_mock.settimeout.assert_has_calls(
373+
[
374+
call(5),
375+
call(3),
376+
]
377+
)
378+
379+
def test_set_timeout_exception_raised(self):
380+
# Checks that context manager sets and reverts timeout properly
381+
# when exception is raised.
382+
with patch.object(self.t, 'sock') as sock_mock:
383+
sock_mock.gettimeout.return_value = 3
384+
with pytest.raises(DummyException):
385+
with self.t.having_timeout(5) as actual_sock:
386+
assert actual_sock == self.t.sock
387+
raise DummyException()
388+
sock_mock.gettimeout.assert_called()
389+
sock_mock.settimeout.assert_has_calls(
390+
[
391+
call(5),
392+
call(3),
393+
]
394+
)
395+
396+
def test_set_same_timeout(self):
397+
# Checks that context manager does not set timeout when
398+
# it is same as currently set.
399+
with patch.object(self.t, 'sock') as sock_mock:
400+
sock_mock.gettimeout.return_value = 5
401+
with self.t.having_timeout(5) as actual_sock:
402+
assert actual_sock == self.t.sock
403+
sock_mock.gettimeout.assert_called()
404+
sock_mock.settimeout.assert_not_called()
405+
360406

361407
class test_AbstractTransport_connect:
362408

0 commit comments

Comments
 (0)