# Copyright (c) 2018 Red Hat, Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from neutron.agent.common import ovs_lib
from neutron.common import utils as common_utils
from neutron.tests.functional import base


class BridgeMonitorTestCase(base.BaseLoggingTestCase):

    def _delete_bridges(self, bridges):
        for bridge in bridges:
            self.ovs.delete_bridge(bridge)

    def test_create_bridges(self):
        bridges_added = []
        bridges_to_monitor = ['br01', 'br02', 'br03']
        bridges_to_create = ['br01', 'br02', 'br03', 'br04', 'br05']
        self.ovs = ovs_lib.BaseOVS()
        self.ovs.ovsdb.idl_monitor.start_bridge_monitor(bridges_to_monitor)
        self.addCleanup(self._delete_bridges, bridges_to_create)

        for bridge in bridges_to_create:
            self.ovs.add_bridge(bridge)

        def retrieve_bridges(bridges_added):
            bridges_added += self.ovs.ovsdb.idl_monitor.bridges_added
            return len(bridges_added)

        common_utils.wait_until_true(
            lambda: retrieve_bridges(bridges_added) == len(bridges_to_monitor),
            timeout=5)
        bridges_added.sort()
        self.assertEqual(bridges_to_monitor, bridges_added)
        self.assertEqual([], self.ovs.ovsdb.idl_monitor.bridges_added)

@@ -626,16 +626,62 @@
 cense.%0A%0A
+import threading%0A%0Afrom ovsdbapp import event%0A%0A
 from neu
@@ -730,85 +730,756 @@
 ron.
-common import utils as common_utils%0Afrom neutron.tests.functional import base
+tests.functional import base%0A%0A%0Aclass WaitForBridgesEvent(event.RowEvent):%0A    event_name = 'WaitForBridgesEvent'%0A    ONETIME = True%0A%0A    def __init__(self, bridges, timeout=5):%0A        self.bridges_not_seen = set(bridges)%0A        self.timeout = timeout%0A        self.event = threading.Event()%0A        super(WaitForBridgesEvent, self).__init__(%0A            (self.ROW_CREATE,), 'Bridge', None)%0A%0A    def matches(self, event, row, old=None):%0A        if event not in self.events or row._table.name != self.table:%0A            return False%0A        self.bridges_not_seen.discard(row.name)%0A        return not self.bridges_not_seen%0A%0A    def run(self, event, row, old):%0A        self.event.set()%0A%0A    def wait(self):%0A        return self.event.wait(self.timeout)
 %0A%0A%0Ac
@@ -1687,35 +1687,8 @@
 f):%0A
-        bridges_added = %5B%5D%0A
     
@@ -1984,25 +1984,24 @@
 create)%0A
-%0A
         
 for brid
@@ -1996,119 +1996,155 @@
     
-for bridge in bridges_to_create:%0A            self.ovs.add_bridge(bridge)%0A%0A        def retrieve_
+event = WaitForBridgesEvent(bridges_to_monitor)%0A        self.ovs.ovsdb.idl_monitor.notify_handler.watch_event(event)%0A        for 
 bridge
-s(
+ in 
 bridges_
 adde
@@ -2139,22 +2139,25 @@
 bridges_
-added)
+to_create
 :%0A      
@@ -2166,201 +2166,98 @@
     
-bridges_added += self.ovs.ovsdb.idl_monitor.bridges_added%0A            return len(bridges_added)%0A%0A        common_utils.wait_until_true(%0A            lambda: retrieve_bridges(bridges_added) == len
+self.ovs.add_bridge(bridge)%0A        self.assertTrue(event.wait())%0A        self.assertEqual
 (bri
@@ -2271,17 +2271,16 @@
 _monitor
-)
 ,%0A      
@@ -2289,91 +2289,47 @@
     
-timeout=5)%0A        bridges_added.sort()%0A        self.assertEqual(bridges_to
+             self.ovs.ovsdb.idl
 _monitor
 , br
@@ -2324,18 +2324,17 @@
 _monitor
-, 
+.
 bridges_

Remove polling from test_create_bridges