Skip to content

Commit

Permalink
Attempt to fix an intermittent failure in GUI tests (#625)
Browse files Browse the repository at this point in the history
Move to using pyqt's qtbot fixture instead of a hand-rolled
wait function
  • Loading branch information
wlach committed Apr 22, 2020
1 parent c33f200 commit 5476acd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 61 deletions.
24 changes: 0 additions & 24 deletions gui/tests/__init__.py
@@ -1,27 +1,3 @@
from contextlib import contextmanager

from PySide2.QtCore import QEventLoop, QTimer
from PySide2.QtWidgets import QApplication

APP = QApplication([]) # we need an application to create widgets


@contextmanager
def wait_signal(signal, timeout=5):
"""Block loop until signal emitted, or timeout (s) elapses."""
loop = QEventLoop()
signal.connect(loop.quit)

yield

timed_out = []
if timeout is not None:

def quit_with_error():
timed_out.append(1)
loop.quit()

QTimer.singleShot(timeout * 1000, quit_with_error)
loop.exec_()
if timed_out:
assert False, "Timeout while waiting for %s" % signal
73 changes: 36 additions & 37 deletions gui/tests/test_build_runner.py
@@ -1,5 +1,4 @@
import os
import shutil
import tempfile
import time
import unittest
Expand All @@ -12,8 +11,6 @@
from mozregression.persist_limit import PersistLimit
from mozregui import build_runner

from . import wait_signal


@pytest.fixture(autouse=True)
def mock_send_telemetry_ping():
Expand All @@ -40,40 +37,6 @@ def iter_content(chunk_size=4):
response.iter_content = iter_content


class TestGuiBuildDownloadManager(unittest.TestCase):
def setUp(self):
self.session, self.session_response = mock_session()
tmpdir = tempfile.mkdtemp()
tpersist = PersistLimit(10 * 1073741824)
self.addCleanup(shutil.rmtree, tmpdir)
self.dl_manager = build_runner.GuiBuildDownloadManager(tmpdir, tpersist)
self.dl_manager.session = self.session
self.signals = {}
for sig in ("download_progress", "download_started", "download_finished"):
self.signals[sig] = Mock()
getattr(self.dl_manager, sig).connect(self.signals[sig])

@patch("mozregui.build_runner.GuiBuildDownloadManager._extract_download_info")
def test_focus_download(self, extract_info):
extract_info.return_value = ("http://foo", "foo")
mock_response(self.session_response, b"this is some data" * 10000, 0.01)
build_info = Mock()

with wait_signal(self.dl_manager.download_finished):
self.dl_manager.focus_download(build_info)

# build_path is defined
self.assertEqual(build_info.build_file, self.dl_manager.get_dest("foo"))

# signals have been emitted
self.assertEqual(self.signals["download_started"].call_count, 1)
self.assertEqual(self.signals["download_finished"].call_count, 1)
self.assertGreater(self.signals["download_progress"].call_count, 0)

# well, file has been downloaded finally
self.assertTrue(os.path.isfile(build_info.build_file))


class TestGuiTestRunner(unittest.TestCase):
def setUp(self):
self.evaluate_started = Mock()
Expand Down Expand Up @@ -108,6 +71,42 @@ def test_basic(self, create_launcher):
self.assertEqual(self.test_runner.verdict, "g")


@pytest.fixture()
def mock_extract_info():
with patch("mozregui.build_runner.GuiBuildDownloadManager._extract_download_info") as p:
yield p


def test_gui_build_download_manager_focus_download(qtbot, mock_extract_info):
session, session_response = mock_session()
with tempfile.TemporaryDirectory() as tmpdir:
tpersist = PersistLimit(10 * 1073741824)
dl_manager = build_runner.GuiBuildDownloadManager(tmpdir, tpersist)
dl_manager.session = session
signals = {}
for sig in ("download_progress", "download_started", "download_finished"):
signals[sig] = Mock()
getattr(dl_manager, sig).connect(signals[sig])

mock_extract_info.return_value = ("http://foo", "foo")
mock_response(session_response, b"this is some data" * 10000, 0.01)
build_info = Mock()

with qtbot.waitSignal(dl_manager.download_finished, raising=True):
dl_manager.focus_download(build_info)

# build_path is defined
assert build_info.build_file == dl_manager.get_dest("foo")

# signals have been emitted
assert signals["download_started"].call_count == 1
assert signals["download_finished"].call_count == 1
assert signals["download_progress"].call_count == 12

# well, file has been downloaded finally
assert os.path.isfile(build_info.build_file)


def test_abstract_build_runner(qtbot):
main_thread = QThread.currentThread()

Expand Down

0 comments on commit 5476acd

Please sign in to comment.