Skip to content

Commit

Permalink
ut: Patch get_ident for race transaction test
Browse files Browse the repository at this point in the history
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 64342ba
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions ovsdbapp/tests/unit/test_api.py
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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]

Expand Down

0 comments on commit 64342ba

Please sign in to comment.