|
12 | 12 | from amqp.transport import _AbstractTransport
|
13 | 13 |
|
14 | 14 |
|
| 15 | +class DummyException(Exception): |
| 16 | + pass |
| 17 | + |
| 18 | + |
15 | 19 | class MockSocket(object):
|
16 | 20 | options = {}
|
17 | 21 |
|
@@ -354,9 +358,51 @@ def test_write__EINTR(self):
|
354 | 358 | assert not self.t.connected
|
355 | 359 |
|
356 | 360 | def test_having_timeout_none(self):
|
| 361 | + # Checks that context manager does nothing when no timeout is provided |
357 | 362 | with self.t.having_timeout(None) as actual_sock:
|
358 | 363 | assert actual_sock == self.t.sock
|
359 | 364 |
|
| 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 | + |
360 | 406 |
|
361 | 407 | class test_AbstractTransport_connect:
|
362 | 408 |
|
|
0 commit comments