Merge "Generate OVSDB schema helper in a separate method"

This commit is contained in:
Jenkins 2016-10-20 11:01:54 +00:00 committed by Gerrit Code Review
commit cbb7dc0fb2
2 changed files with 46 additions and 19 deletions

View File

@ -61,6 +61,7 @@ class Connection(object):
self.lock = threading.Lock()
self.schema_name = schema_name
self.idl_class = idl_class or idl.Idl
self._schema_filter = None
def start(self, table_name_list=None):
"""
@ -70,30 +71,14 @@ class Connection(object):
schema_helper will register all tables for given schema_name as
default.
"""
self._schema_filter = table_name_list
with self.lock:
if self.idl is not None:
return
try:
helper = idlutils.get_schema_helper(self.connection,
self.schema_name)
except Exception:
# We may have failed do to set-manager not being called
helpers.enable_connection_uri(self.connection)
helper = self.get_schema_helper()
self.update_schema_helper(helper)
# There is a small window for a race, so retry up to a second
@retrying.retry(wait_exponential_multiplier=10,
stop_max_delay=1000)
def do_get_schema_helper():
return idlutils.get_schema_helper(self.connection,
self.schema_name)
helper = do_get_schema_helper()
if table_name_list is None:
helper.register_all()
else:
for table_name in table_name_list:
helper.register_table(table_name)
self.idl = self.idl_class(self.connection, helper)
idlutils.wait_for_change(self.idl, self.timeout)
self.poller = poller.Poller()
@ -101,6 +86,32 @@ class Connection(object):
self.thread.setDaemon(True)
self.thread.start()
def get_schema_helper(self):
"""Retrieve the schema helper object from OVSDB"""
try:
helper = idlutils.get_schema_helper(self.connection,
self.schema_name)
except Exception:
# We may have failed do to set-manager not being called
helpers.enable_connection_uri(self.connection)
# There is a small window for a race, so retry up to a second
@retrying.retry(wait_exponential_multiplier=10,
stop_max_delay=1000)
def do_get_schema_helper():
return idlutils.get_schema_helper(self.connection,
self.schema_name)
helper = do_get_schema_helper()
return helper
def update_schema_helper(self, helper):
if self._schema_filter:
for table_name in self._schema_filter:
helper.register_table(table_name)
else:
helper.register_all()
def run(self):
while True:
self.idl.wait(self.poller)

View File

@ -50,6 +50,22 @@ class TestOVSNativeConnection(base.BaseTestCase):
def test_start_with_table_name_list(self):
self._test_start(table_name_list=['fake-table1', 'fake-table2'])
@mock.patch.object(connection, 'TransactionQueue')
@mock.patch.object(idl, 'Idl')
@mock.patch.object(idlutils, 'wait_for_change')
def test_start_call_graph(self, wait_for_change, idl, transaction_queue):
self.connection = connection.Connection(
mock.sentinel, mock.sentinel, mock.sentinel)
self.connection.get_schema_helper = mock.Mock()
helper = self.connection.get_schema_helper.return_value
self.connection.update_schema_helper = mock.Mock()
with mock.patch.object(poller, 'Poller') as poller_mock,\
mock.patch('threading.Thread'):
poller_mock.return_value.block.side_effect = eventlet.sleep
self.connection.start()
self.connection.get_schema_helper.assert_called_once_with()
self.connection.update_schema_helper.assert_called_once_with(helper)
def test_transaction_queue_init(self):
# a test to cover py34 failure during initialization (LP Bug #1580270)
# make sure no ValueError: can't have unbuffered text I/O is raised