Permalink
Browse files

ut: Patch get_ident for race transaction test

In case eventlet is installed on the system, the time.sleep() function
doesn't yield.  This patch replaces time.sleep with eventlet.sleep and
thread.get_ident functions on systems with eventlet installed. With this
patch, a concurrent transaction test can be executed both with and
without eventlet successfuly.

Change-Id: I4572355d7d420111e562d4d1f4ea3dd95d4e3f7d
Related-bug: #1793499
  • Loading branch information...
cubeek committed Sep 21, 2018
1 parent 19be761 commit 64342ba52cc3e80b91ea0c845c349d2ce386f4ae
Showing with 24 additions and 4 deletions.
  1. +24 −4 ovsdbapp/tests/unit/test_api.py
@@ -12,27 +12,46 @@
# License for the specific language governing permissions and limitations
# under the License.
import sys
import fixtures
import mock
import testtools
import time
from ovsdbapp import api
from ovsdbapp.tests import base
try:
import eventlet
from eventlet.green import thread
sleep = eventlet.sleep
def create_thread(executable):
eventlet.spawn_n(executable)
except ImportError:
import threading
import time
sleep = time.sleep
def create_thread(executable):
thread = threading.Thread(target=executable)
thread.start()
class GreenThreadingFixture(fixtures.Fixture):
def _setUp(self):
if 'eventlet' in sys.modules:
self._orig = api.thread.get_ident
api.thread.get_ident = thread.get_ident
self.addCleanup(self.cleanup)
def cleanup(self):
api.thread.get_ident = self._orig
class FakeTransaction(object):
def __enter__(self):
return self
@@ -57,6 +76,7 @@ def setUp(self):
super(TransactionTestCase, self).setUp()
self.api = TestingAPI()
mock.patch.object(FakeTransaction, 'commit').start()
self.useFixture(GreenThreadingFixture())
def test_transaction_nested(self):
with self.api.transaction() as txn1:
@@ -82,12 +102,12 @@ def thread1():
with self.api.transaction() as txn:
shared_resource.append(txn)
while len(shared_resource) == 1:
time.sleep(0.1)
sleep(0.1)
shared_resource.append(0)
def thread2():
while len(shared_resource) != 1:
time.sleep(0.1)
sleep(0.1)
with self.api.transaction() as txn:
shared_resource.append(txn)
shared_resource.append(0)
@@ -96,7 +116,7 @@ def thread2():
create_thread(thread2)
while len(shared_resource) != 4:
time.sleep(0.1)
sleep(0.1)
txn1, txn2 = shared_resource[:2]

0 comments on commit 64342ba

Please sign in to comment.