|
1 | 1 | import re |
2 | 2 | import socket |
3 | 3 | import warnings |
4 | | -from array import array |
5 | 4 | from unittest.mock import Mock, call, patch |
6 | 5 |
|
7 | 6 | import pytest |
| 7 | +import time |
8 | 8 |
|
9 | 9 | from amqp import Connection, spec |
10 | 10 | from amqp.connection import SSLError |
@@ -514,6 +514,38 @@ def test_heartbeat_tick(self): |
514 | 514 | with pytest.raises(ConnectionError): |
515 | 515 | self.conn.heartbeat_tick() |
516 | 516 |
|
| 517 | + def _test_heartbeat_rate_tick(self, rate): |
| 518 | + # Doing 22 calls, |
| 519 | + # First one is setting the variables |
| 520 | + # All nexts may send heartbeats, depending on rate |
| 521 | + for i in range(1, 22): |
| 522 | + self.conn.heartbeat_tick(rate) |
| 523 | + time.sleep(0.1) |
| 524 | + |
| 525 | + def test_heartbeat_check_rate_default(self): |
| 526 | + # Heartbeat set to 2 secs |
| 527 | + self.conn.heartbeat = 2 |
| 528 | + # Default rate is 2 --> should send frames every sec |
| 529 | + self._test_heartbeat_rate_tick(2) |
| 530 | + # Verify that we wrote 2 frames |
| 531 | + assert self.conn.frame_writer.call_count == 2 |
| 532 | + |
| 533 | + def test_heartbeat_check_rate_four(self): |
| 534 | + # Heartbeat set to 2 secs |
| 535 | + self.conn.heartbeat = 2 |
| 536 | + # Rate 4 --> should send frames every 0.5sec |
| 537 | + self._test_heartbeat_rate_tick(4) |
| 538 | + # Verify that we wrote 4 frames |
| 539 | + assert self.conn.frame_writer.call_count == 4 |
| 540 | + |
| 541 | + def test_heartbeat_check_rate_wrong(self): |
| 542 | + # Heartbeat set to 2 secs |
| 543 | + self.conn.heartbeat = 2 |
| 544 | + # Default rate is 2 --> should send frames every sec |
| 545 | + self._test_heartbeat_rate_tick(-42) |
| 546 | + # Verify that we wrote 2 frames |
| 547 | + assert self.conn.frame_writer.call_count == 2 |
| 548 | + |
517 | 549 | def test_server_capabilities(self): |
518 | 550 | self.conn.server_properties['capabilities'] = {'foo': 1} |
519 | 551 | assert self.conn.server_capabilities == {'foo': 1} |
|
0 commit comments