From fb3ab00d1d8778ff4460ec475124585f4febe042 Mon Sep 17 00:00:00 2001
From: Kenneth Giusti <kgiusti@gmail.com>
Date: Mon, 5 Nov 2018 16:02:06 -0500
Subject: [PATCH] Use ensure_connection to prevent loss of connection error
 logs

Change-Id: I31e3f83304c0780cfd380aa63ec2962c3c28ec78
Closes-Bug: #1801011
---
 oslo_messaging/_drivers/impl_rabbit.py           | 7 ++++++-
 oslo_messaging/tests/drivers/test_impl_rabbit.py | 9 ++++++---
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/oslo_messaging/_drivers/impl_rabbit.py b/oslo_messaging/_drivers/impl_rabbit.py
index e2ac5e176..1d77cfd5e 100644
--- a/oslo_messaging/_drivers/impl_rabbit.py
+++ b/oslo_messaging/_drivers/impl_rabbit.py
@@ -637,8 +637,13 @@ class Connection(object):
     def ensure_connection(self):
         # NOTE(sileht): we reset the channel and ensure
         # the kombu underlying connection works
+        def on_error(exc, interval):
+            LOG.error("Connection failed: %s (retrying in %s seconds)",
+                      str(exc), interval)
+
         self._set_current_channel(None)
-        self.ensure(method=self.connection.connect)
+        self.connection.ensure_connection(errback=on_error)
+        self._set_current_channel(self.connection.channel())
         self.set_transport_socket_timeout()
 
     def ensure(self, method, retry=None,
diff --git a/oslo_messaging/tests/drivers/test_impl_rabbit.py b/oslo_messaging/tests/drivers/test_impl_rabbit.py
index cfbef742f..b5f9e5290 100644
--- a/oslo_messaging/tests/drivers/test_impl_rabbit.py
+++ b/oslo_messaging/tests/drivers/test_impl_rabbit.py
@@ -123,7 +123,8 @@ class TestRabbitDriverLoad(test_utils.BaseTestCase):
         ('rabbit+memory', dict(transport_url='kombu+memory:/'))
     ]
 
-    @mock.patch('oslo_messaging._drivers.impl_rabbit.Connection.ensure')
+    @mock.patch('oslo_messaging._drivers.impl_rabbit.Connection'
+                '.ensure_connection')
     @mock.patch('oslo_messaging._drivers.impl_rabbit.Connection.reset')
     def test_driver_load(self, fake_ensure, fake_reset):
         self.config(heartbeat_timeout_threshold=60,
@@ -155,7 +156,8 @@ class TestRabbitDriverLoadSSL(test_utils.BaseTestCase):
                                                 cert_reqs=ssl.CERT_REQUIRED))),
     ]
 
-    @mock.patch('oslo_messaging._drivers.impl_rabbit.Connection.ensure')
+    @mock.patch('oslo_messaging._drivers.impl_rabbit.Connection'
+                '.ensure_connection')
     @mock.patch('kombu.connection.Connection')
     def test_driver_load(self, connection_klass, fake_ensure):
         self.config(group="oslo_messaging_rabbit", **self.options)
@@ -406,7 +408,8 @@ class TestRabbitTransportURL(test_utils.BaseTestCase):
         self.config(heartbeat_timeout_threshold=0,
                     group='oslo_messaging_rabbit')
 
-    @mock.patch('oslo_messaging._drivers.impl_rabbit.Connection.ensure')
+    @mock.patch('oslo_messaging._drivers.impl_rabbit.Connection'
+                '.ensure_connection')
     @mock.patch('oslo_messaging._drivers.impl_rabbit.Connection.reset')
     def test_transport_url(self, fake_reset, fake_ensure):
         transport = oslo_messaging.get_transport(self.conf, self.url)