Repository: pika/pika

Issue:
Include the Pika version number in the log when opening a connection
See @vitaly-krugl's comment [here](https://github.com/pika/pika/issues/1046#issuecomment-392585166)

Comments:

Patch:
diff --git a/examples/consume.py b/examples/consume.py
index 17667de..2254cd1 100644
--- a/examples/consume.py
+++ b/examples/consume.py
@@ -1,12 +1,19 @@
 import functools
+import logging
 import pika
 
+LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
+              '-35s %(lineno) -5d: %(message)s')
+LOGGER = logging.getLogger(__name__)
+
+logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
+
 def on_message(channel, method_frame, header_frame, body, userdata=None):
-    print('Userdata: {} Message body: {}'.format(userdata, body))
+    LOGGER.info('Userdata: {} Message body: {}'.format(userdata, body))
     channel.basic_ack(delivery_tag=method_frame.delivery_tag)
 
 credentials = pika.PlainCredentials('guest', 'guest')
-parameters =  pika.ConnectionParameters('localhost', credentials=credentials)
+parameters = pika.ConnectionParameters('localhost', credentials=credentials)
 connection = pika.BlockingConnection(parameters)
 
 channel = connection.channel()
diff --git a/pika/adapters/utils/connection_workflow.py b/pika/adapters/utils/connection_workflow.py
index e6926c9..e3567b5 100644
--- a/pika/adapters/utils/connection_workflow.py
+++ b/pika/adapters/utils/connection_workflow.py
@@ -14,6 +14,7 @@ import socket
 import pika.compat
 import pika.exceptions
 import pika.tcp_socket_opts
+from pika import __version__
 
 
 _LOG = logging.getLogger(__name__)
@@ -186,7 +187,8 @@ class AMQPConnector(object):
         self._sock.setblocking(False)
 
         addr = self._addr_record[4]
-        _LOG.info('Connecting to AMQP broker at %r', addr)
+        _LOG.info('Pika version %s connecting to %r',
+                  __version__, addr)
         self._task_ref = self._nbio.connect_socket(
             self._sock,
             addr,
diff --git a/pika/heartbeat.py b/pika/heartbeat.py
index 4eaaa59..3c9f46f 100644
--- a/pika/heartbeat.py
+++ b/pika/heartbeat.py
@@ -12,21 +12,24 @@ class HeartbeatChecker(object):
     intervals.
 
     """
+    DEFAULT_INTERVAL = 60
     MAX_IDLE_COUNT = 2
+
     _STALE_CONNECTION = "Too Many Missed Heartbeats, No reply in %i seconds"
 
-    def __init__(self, connection, interval, idle_count=MAX_IDLE_COUNT):
+    def __init__(self, connection, interval=DEFAULT_INTERVAL, idle_count=MAX_IDLE_COUNT):
         """Create a heartbeat on connection sending a heartbeat frame every
         interval seconds.
 
         :param pika.connection.Connection: Connection object
-        :param int interval: Heartbeat check interval
-        :param int idle_count: Number of heartbeat intervals missed until the
-                               connection is considered idle and disconnects
+        :param int interval: Heartbeat check interval. Note: heartbeats will
+                             be sent at interval / 2 frequency.
 
         """
         self._connection = connection
-        self._interval = interval
+        # Note: see the following document:
+        # https://www.rabbitmq.com/heartbeats.html#heartbeats-timeout
+        self._interval = float(interval / 2)
         self._max_idle_count = idle_count
 
         # Initialize counters


Test patch:
diff --git a/tests/unit/connection_tests.py b/tests/unit/connection_tests.py
index 9f40320..438633c 100644
--- a/tests/unit/connection_tests.py
+++ b/tests/unit/connection_tests.py
@@ -536,7 +536,7 @@ class ConnectionTests(unittest.TestCase):  # pylint: disable=R0904
                                 _adapter_emit_data,
                                 method,
                                 heartbeat_checker):
-        """make sure on connection tune turns the connection params"""
+        """make sure _on_connection_tune tunes the connection params"""
         heartbeat_checker.return_value = 'hearbeat obj'
         self.connection._flush_outbound = mock.Mock()
         marshal = mock.Mock(return_value='ab')
diff --git a/tests/unit/heartbeat_tests.py b/tests/unit/heartbeat_tests.py
index c5a7ca5..eaf339f 100644
--- a/tests/unit/heartbeat_tests.py
+++ b/tests/unit/heartbeat_tests.py
@@ -55,27 +55,31 @@ class ConstructableConnection(connection.Connection):
 
 class HeartbeatTests(unittest.TestCase):
 
-    INTERVAL = 5
+    INTERVAL = 60
+    HALF_INTERVAL = INTERVAL / 2
 
     def setUp(self):
         self.mock_conn = mock.Mock(spec_set=ConstructableConnection())
         self.mock_conn.bytes_received = 100
         self.mock_conn.bytes_sent = 100
         self.mock_conn._heartbeat_checker = mock.Mock(spec=heartbeat.HeartbeatChecker)
-        self.obj = heartbeat.HeartbeatChecker(self.mock_conn, self.INTERVAL)
+        self.obj = heartbeat.HeartbeatChecker(self.mock_conn)
 
     def tearDown(self):
         del self.obj
         del self.mock_conn
 
+    def test_default_initialization_interval(self):
+        self.assertEqual(self.obj._interval, self.HALF_INTERVAL)
+
     def test_default_initialization_max_idle_count(self):
         self.assertEqual(self.obj._max_idle_count, self.obj.MAX_IDLE_COUNT)
 
     def test_constructor_assignment_connection(self):
-        self.assertEqual(self.obj._connection, self.mock_conn)
+        self.assertIs(self.obj._connection, self.mock_conn)
 
     def test_constructor_assignment_heartbeat_interval(self):
-        self.assertEqual(self.obj._interval, self.INTERVAL)
+        self.assertEqual(self.obj._interval, self.HALF_INTERVAL)
 
     def test_constructor_initial_bytes_received(self):
         self.assertEqual(self.obj._bytes_received, 0)
@@ -94,7 +98,7 @@ class HeartbeatTests(unittest.TestCase):
 
     @mock.patch('pika.heartbeat.HeartbeatChecker._setup_timer')
     def test_constructor_called_setup_timer(self, timer):
-        heartbeat.HeartbeatChecker(self.mock_conn, self.INTERVAL)
+        heartbeat.HeartbeatChecker(self.mock_conn)
         timer.assert_called_once_with()
 
     def test_active_true(self):
@@ -122,13 +126,13 @@ class HeartbeatTests(unittest.TestCase):
 
     @mock.patch('pika.heartbeat.HeartbeatChecker._close_connection')
     def test_send_and_check_not_closed(self, close_connection):
-        obj = heartbeat.HeartbeatChecker(self.mock_conn, self.INTERVAL)
+        obj = heartbeat.HeartbeatChecker(self.mock_conn)
         obj.send_and_check()
         close_connection.assert_not_called()
 
     @mock.patch('pika.heartbeat.HeartbeatChecker._close_connection')
     def test_send_and_check_missed_bytes(self, close_connection):
-        obj = heartbeat.HeartbeatChecker(self.mock_conn, self.INTERVAL)
+        obj = heartbeat.HeartbeatChecker(self.mock_conn)
         obj._idle_byte_intervals = self.INTERVAL
         obj.send_and_check()
         close_connection.assert_called_once_with()
@@ -147,19 +151,19 @@ class HeartbeatTests(unittest.TestCase):
 
     @mock.patch('pika.heartbeat.HeartbeatChecker._update_counters')
     def test_send_and_check_update_counters(self, update_counters):
-        obj = heartbeat.HeartbeatChecker(self.mock_conn, self.INTERVAL)
+        obj = heartbeat.HeartbeatChecker(self.mock_conn)
         obj.send_and_check()
         update_counters.assert_called_once_with()
 
     @mock.patch('pika.heartbeat.HeartbeatChecker._send_heartbeat_frame')
     def test_send_and_check_send_heartbeat_frame(self, send_heartbeat_frame):
-        obj = heartbeat.HeartbeatChecker(self.mock_conn, self.INTERVAL)
+        obj = heartbeat.HeartbeatChecker(self.mock_conn)
         obj.send_and_check()
         send_heartbeat_frame.assert_called_once_with()
 
     @mock.patch('pika.heartbeat.HeartbeatChecker._start_timer')
     def test_send_and_check_start_timer(self, start_timer):
-        obj = heartbeat.HeartbeatChecker(self.mock_conn, self.INTERVAL)
+        obj = heartbeat.HeartbeatChecker(self.mock_conn)
         obj.send_and_check()
         start_timer.assert_called_once_with()
 
@@ -202,7 +206,7 @@ class HeartbeatTests(unittest.TestCase):
 
     def test_setup_timer_called(self):
         self.mock_conn._adapter_add_timeout.assert_called_once_with(
-            self.INTERVAL, self.obj.send_and_check)
+            self.HALF_INTERVAL, self.obj.send_and_check)
 
     @mock.patch('pika.heartbeat.HeartbeatChecker._setup_timer')
     def test_start_timer_not_active(self, setup_timer):