Skip to content

Commit

Permalink
Merge pull request #1041 from vitaly-krugl/issue-1036-fix-heartbeat-c…
Browse files Browse the repository at this point in the history
…heckter-active

Fixed access to non-existent attribute Connection.heartbeat in HeartbeatChecker.active property getter
  • Loading branch information
michaelklishin committed May 9, 2018
2 parents d9a1baa + 44da5e5 commit b713f98
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ extensions.
- Python 2.7 and 3.4+ are supported.

- Since threads aren't appropriate to every situation, it doesn't
require threads. It takes care not to forbid them, either. The same
goes for greenlets, callbacks, continuations and generators. It is
not necessarily thread-safe however, and your mileage will vary.
require threads. Pika core takes care not to forbid them, either. The same
goes for greenlets, callbacks, continuations, and generators. An instance of
Pika's built-in connection adapters is not thread-safe, however.

- People may be using direct sockets, plain old `select()`,
or any of the wide variety of ways of getting network events to and from a
Expand Down
2 changes: 1 addition & 1 deletion pika/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def active(self):
:rtype True
"""
return self._connection.heartbeat is self
return self._connection._heartbeat_checker is self

@property
def bytes_received_on_connection(self):
Expand Down
34 changes: 29 additions & 5 deletions tests/unit/heartbeat_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,39 @@
# pylint: disable=C0103


class ConstructableConnection(connection.Connection):
"""Adds dummy overrides for `Connection`'s abstract methods so
that we can instantiate and test it.
"""
def _adapter_connect_stream(self):
pass

def _adapter_disconnect_stream(self):
raise NotImplementedError

def add_timeout(self, deadline, callback):
raise NotImplementedError

def remove_timeout(self, timeout_id):
raise NotImplementedError

def _adapter_emit_data(self, data):
raise NotImplementedError

def _adapter_get_write_buffer_size(self):
raise NotImplementedError


class HeartbeatTests(unittest.TestCase):

INTERVAL = 5

def setUp(self):
self.mock_conn = mock.Mock(spec=connection.Connection)
self.mock_conn = mock.Mock(spec_set=ConstructableConnection())
self.mock_conn.bytes_received = 100
self.mock_conn.bytes_sent = 100
self.mock_conn.heartbeat = mock.Mock(spec=heartbeat.HeartbeatChecker)
self.mock_conn._heartbeat_checker = mock.Mock(spec=heartbeat.HeartbeatChecker)
self.obj = heartbeat.HeartbeatChecker(self.mock_conn, self.INTERVAL)

def tearDown(self):
Expand Down Expand Up @@ -65,11 +89,11 @@ def test_constructor_called_setup_timer(self, timer):
timer.assert_called_once_with()

def test_active_true(self):
self.mock_conn.heartbeat = self.obj
self.mock_conn._heartbeat_checker = self.obj
self.assertTrue(self.obj.active)

def test_active_false(self):
self.mock_conn.heartbeat = mock.Mock()
self.mock_conn._heartbeat_checker = mock.Mock()
self.assertFalse(self.obj.active)

def test_bytes_received_on_connection(self):
Expand Down Expand Up @@ -178,7 +202,7 @@ def test_start_timer_not_active(self, setup_timer):

@mock.patch('pika.heartbeat.HeartbeatChecker._setup_timer')
def test_start_timer_active(self, setup_timer):
self.mock_conn.heartbeat = self.obj
self.mock_conn._heartbeat_checker = self.obj
self.obj._start_timer()
self.assertTrue(setup_timer.called)

Expand Down

0 comments on commit b713f98

Please sign in to comment.