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

View File

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