Stabilize git driver tests

These tests relied on sleeps which can cause races when running
the full test suite in parallel.  Instead, wait for the events
we know will happen to happen.

Also remove the dependency on yarl now that aiohttp has made a
release which works with yarl 1.0 (however, it does not work with
<1.0 which is why this needs to be combined with this change to
fix tests).

Change-Id: Ib1c626cdd3f083dd1d23a3c6547bd7163b66567e
This commit is contained in:
James E. Blair 2018-01-17 07:57:54 -08:00
parent 168a94117b
commit 185b970bad
3 changed files with 33 additions and 16 deletions

View File

@ -25,6 +25,5 @@ cryptography>=1.6
cachecontrol
pyjwt
iso8601
yarl>=0.11,<1.0
aiohttp
uvloop;python_version>='3.5'

View File

@ -24,6 +24,11 @@ class TestGitDriver(ZuulTestCase):
config_file = 'zuul-git-driver.conf'
tenant_config_file = 'config/git-driver/main.yaml'
def setUp(self):
super(TestGitDriver, self).setUp()
self.git_connection = self.sched.connections.getSource('git').\
connection
def setup_config(self):
super(TestGitDriver, self).setup_config()
self.config.set('connection git', 'baseurl', self.upstream_root)
@ -70,8 +75,8 @@ class TestGitDriver(ZuulTestCase):
self.addCommitToRepo(
'common-config', 'Change zuul.yaml configuration', files)
# Let some time for the tenant reconfiguration to happen
time.sleep(2)
# Wait for the tenant reconfiguration to happen
count = self.waitForEvent()
self.waitUntilSettled()
A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
@ -109,8 +114,8 @@ class TestGitDriver(ZuulTestCase):
# Restart the git watcher
self.sched.connections.getSource('git').connection.w_pause = False
# Let some time for the tenant reconfiguration to happen
time.sleep(2)
# Wait for the tenant reconfiguration to happen
self.waitForEvent(count)
self.waitUntilSettled()
A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
@ -123,23 +128,33 @@ class TestGitDriver(ZuulTestCase):
def ensure_watcher_has_context(self):
# Make sure watcher have read initial refs shas
cnx = self.sched.connections.getSource('git').connection
delay = 0.1
max_delay = 1
while not cnx.projects_refs:
while not self.git_connection.projects_refs:
time.sleep(delay)
max_delay -= delay
if max_delay <= 0:
raise Exception("Timeout waiting for initial read")
return self.git_connection.watcher_thread._event_count
def waitForEvent(self, initial_count=0):
delay = 0.1
max_delay = 1
while self.git_connection.watcher_thread._event_count <= initial_count:
time.sleep(delay)
max_delay -= delay
if max_delay <= 0:
raise Exception("Timeout waiting for event")
return self.git_connection.watcher_thread._event_count
@simple_layout('layouts/basic-git.yaml', driver='git')
def test_ref_updated_event(self):
self.ensure_watcher_has_context()
count = self.ensure_watcher_has_context()
# Add a commit to trigger a ref-updated event
self.addCommitToRepo(
'org/project', 'A change for ref-updated', {'f1': 'Content'})
# Let some time for the git watcher to detect the ref-update event
time.sleep(0.2)
# Wait for the git watcher to detect the ref-update event
self.waitForEvent(count)
self.waitUntilSettled()
self.assertEqual(len(self.history), 1)
self.assertEqual('SUCCESS',
@ -147,12 +162,12 @@ class TestGitDriver(ZuulTestCase):
@simple_layout('layouts/basic-git.yaml', driver='git')
def test_ref_created(self):
self.ensure_watcher_has_context()
count = self.ensure_watcher_has_context()
# Tag HEAD to trigger a ref-updated event
self.addTagToRepo(
'org/project', 'atag', 'HEAD')
# Let some time for the git watcher to detect the ref-update event
time.sleep(0.2)
# Wait for the git watcher to detect the ref-update event
self.waitForEvent(count)
self.waitUntilSettled()
self.assertEqual(len(self.history), 1)
self.assertEqual('SUCCESS',
@ -160,12 +175,12 @@ class TestGitDriver(ZuulTestCase):
@simple_layout('layouts/basic-git.yaml', driver='git')
def test_ref_deleted(self):
self.ensure_watcher_has_context()
count = self.ensure_watcher_has_context()
# Delete default tag init to trigger a ref-updated event
self.delTagFromRepo(
'org/project', 'init')
# Let some time for the git watcher to detect the ref-update event
time.sleep(0.2)
# Wait for the git watcher to detect the ref-update event
self.waitForEvent(count)
self.waitUntilSettled()
# Make sure no job as run as ignore-delete is True by default
self.assertEqual(len(self.history), 0)

View File

@ -38,6 +38,8 @@ class GitWatcher(threading.Thread):
self.poll_delay = poll_delay
self._stopped = False
self.projects_refs = self.git_connection.projects_refs
# This is used by the test framework
self._event_count = 0
def compareRefs(self, project, refs):
partial_events = []
@ -112,6 +114,7 @@ class GitWatcher(threading.Thread):
self.git_connection.logEvent(event)
# Pass the event to the scheduler
self.git_connection.sched.addEvent(event)
self._event_count += 1
except Exception as e:
self.log.debug("Unexpected issue in _run loop: %s" % str(e))