Merge "rpc: initialize notifier when get_notifier is called"

This commit is contained in:
Zuul 2024-02-21 01:12:34 +00:00 committed by Gerrit Code Review
commit df103c9ee8
2 changed files with 20 additions and 52 deletions

View File

@ -35,7 +35,6 @@ from osprofiler import profiler
LOG = logging.getLogger(__name__)
TRANSPORT = None
NOTIFICATION_TRANSPORT = None
NOTIFIER = None
_DFT_EXMODS = runtime.list_package_modules(exceptions.__name__)
@ -47,7 +46,7 @@ def init(conf, rpc_ext_mods=None):
:param rpc_ext_mods: Exception modules to expose via RPC.
:returns: None.
"""
global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
global TRANSPORT, NOTIFICATION_TRANSPORT
if rpc_ext_mods is None:
rpc_ext_mods = _DFT_EXMODS
@ -58,9 +57,6 @@ def init(conf, rpc_ext_mods=None):
conf, allowed_remote_exmods=rpc_ext_mods)
NOTIFICATION_TRANSPORT = oslo_messaging.get_notification_transport(
conf, allowed_remote_exmods=rpc_ext_mods)
serializer = RequestContextSerializer()
NOTIFIER = oslo_messaging.Notifier(NOTIFICATION_TRANSPORT,
serializer=serializer)
def cleanup():
@ -68,18 +64,16 @@ def cleanup():
:returns: None.
"""
global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
global TRANSPORT, NOTIFICATION_TRANSPORT
if TRANSPORT is None:
raise AssertionError(_("'TRANSPORT' must not be None"))
if NOTIFICATION_TRANSPORT is None:
raise AssertionError(
_("'NOTIFICATION_TRANSPORT' must not be None"))
if NOTIFIER is None:
raise AssertionError(_("'NOTIFIER' must not be None"))
TRANSPORT.cleanup()
NOTIFICATION_TRANSPORT.cleanup()
_BackingOffContextWrapper.reset_timeouts()
TRANSPORT = NOTIFICATION_TRANSPORT = NOTIFIER = None
TRANSPORT = NOTIFICATION_TRANSPORT = None
def _get_default_method_timeout():
@ -243,11 +237,14 @@ def get_notifier(service=None, host=None, publisher_id=None):
`service` and `host` arguments.
:returns: A new RPC notifier reference.
"""
if NOTIFIER is None:
raise AssertionError(_("'NOTIFIER' must not be None"))
if NOTIFICATION_TRANSPORT is None:
raise AssertionError(_("'NOTIFICATION_TRANSPORT' must not be None"))
if not publisher_id:
publisher_id = "%s.%s" % (service, host or cfg.CONF.host)
return NOTIFIER.prepare(publisher_id=publisher_id)
serializer = RequestContextSerializer()
return oslo_messaging.Notifier(NOTIFICATION_TRANSPORT,
serializer=serializer,
publisher_id=publisher_id)
class RequestContextSerializer(om_serializer.Serializer):

View File

@ -34,18 +34,13 @@ class TestRPC(base.BaseTestCase):
@mock.patch.object(rpc, 'RequestContextSerializer')
@mock.patch.object(messaging, 'get_rpc_transport')
@mock.patch.object(messaging, 'get_notification_transport')
@mock.patch.object(messaging, 'Notifier')
def test_init(self, mock_not, mock_noti_trans, mock_trans, mock_ser):
notifier = mock.Mock()
def test_init(self, mock_noti_trans, mock_trans, mock_ser):
transport = mock.Mock()
noti_transport = mock.Mock()
serializer = mock.Mock()
conf = mock.Mock()
mock_trans.return_value = transport
mock_noti_trans.return_value = noti_transport
mock_ser.return_value = serializer
mock_not.return_value = notifier
rpc.init(conf, rpc_ext_mods=['foo'])
@ -54,14 +49,10 @@ class TestRPC(base.BaseTestCase):
conf, allowed_remote_exmods=expected_mods)
mock_noti_trans.assert_called_once_with(
conf, allowed_remote_exmods=expected_mods)
mock_not.assert_called_once_with(noti_transport,
serializer=serializer)
self.assertIsNotNone(rpc.TRANSPORT)
self.assertIsNotNone(rpc.NOTIFICATION_TRANSPORT)
self.assertIsNotNone(rpc.NOTIFIER)
def test_cleanup_transport_null(self):
rpc.NOTIFIER = mock.Mock()
rpc.NOTIFICATION_TRANSPORT = mock.Mock()
rpc.TRANSPORT = None
self.assertRaises(AssertionError, rpc.cleanup)
@ -69,20 +60,11 @@ class TestRPC(base.BaseTestCase):
def test_cleanup_notification_transport_null(self):
rpc.TRANSPORT = mock.Mock()
rpc.NOTIFIER = mock.Mock()
rpc.NOTIFICATION_TRANSPORT = None
self.assertRaises(AssertionError, rpc.cleanup)
rpc.NOTIFICATION_TRANSPORT = mock.Mock()
def test_cleanup_notifier_null(self):
rpc.TRANSPORT = mock.Mock()
rpc.NOTIFICATION_TRANSPORT = mock.Mock()
rpc.NOTIFIER = None
self.assertRaises(AssertionError, rpc.cleanup)
rpc.NOTIFIER = mock.Mock()
def test_cleanup(self):
rpc.NOTIFIER = mock.Mock()
rpc.NOTIFICATION_TRANSPORT = mock.Mock()
rpc.TRANSPORT = mock.Mock()
trans_cleanup = mock.Mock()
@ -96,10 +78,8 @@ class TestRPC(base.BaseTestCase):
not_trans_cleanup.assert_called_once_with()
self.assertIsNone(rpc.TRANSPORT)
self.assertIsNone(rpc.NOTIFICATION_TRANSPORT)
self.assertIsNone(rpc.NOTIFIER)
rpc.TRANSPORT = mock.Mock()
rpc.NOTIFIER = mock.Mock()
rpc.NOTIFICATION_TRANSPORT = mock.Mock()
@mock.patch.object(rpc, 'RequestContextSerializer')
@ -123,7 +103,6 @@ class TestRPC(base.BaseTestCase):
@mock.patch.object(rpc, 'RequestContextSerializer')
@mock.patch.object(messaging, 'get_rpc_server')
def test_get_server(self, mock_get, mock_ser):
rpc.TRANSPORT = mock.Mock()
ser = mock.Mock()
tgt = mock.Mock()
ends = mock.Mock()
@ -138,26 +117,18 @@ class TestRPC(base.BaseTestCase):
self.assertEqual('server', server)
def test_get_notifier(self):
rpc.NOTIFIER = mock.Mock()
mock_prep = mock.Mock()
mock_prep.return_value = 'notifier'
rpc.NOTIFIER.prepare = mock_prep
notifier = rpc.get_notifier('service', publisher_id='foo')
mock_prep.assert_called_once_with(publisher_id='foo')
self.assertEqual('notifier', notifier)
mock_notifier = mock.Mock(return_value=None)
messaging.Notifier.__init__ = mock_notifier
rpc.get_notifier('service', publisher_id='foo')
mock_notifier.assert_called_once_with(
mock.ANY, serializer=mock.ANY, publisher_id='foo')
def test_get_notifier_null_publisher(self):
rpc.NOTIFIER = mock.Mock()
mock_prep = mock.Mock()
mock_prep.return_value = 'notifier'
rpc.NOTIFIER.prepare = mock_prep
notifier = rpc.get_notifier('service', host='bar')
mock_prep.assert_called_once_with(publisher_id='service.bar')
self.assertEqual('notifier', notifier)
mock_notifier = mock.Mock(return_value=None)
messaging.Notifier.__init__ = mock_notifier
rpc.get_notifier('service', host='bar')
mock_notifier.assert_called_once_with(
mock.ANY, serializer=mock.ANY, publisher_id='service.bar')
class TestRequestContextSerializer(base.BaseTestCase):