def test_start_pause_and_resume(self): app_state = AppState() manager = DownloadManager(app_state) conf = DownloadConfiguration(number_of_images=5, images_per_category=1, download_destination=self.image_net_home) app_state.set_configuration(conf) paused_spy = QSignalSpy(manager.downloadPaused) resumed_spy = QSignalSpy(manager.downloadResumed) finished_spy = QSignalSpy(manager.allDownloaded) manager.start() manager.pause_download() received = paused_spy.wait(timeout=500) self.assertTrue(received) time.sleep(0.5) manager.resume_download() received = finished_spy.wait(timeout=500) self.assertTrue(received) self._assert_expected_directories_exist() self._assert_files_are_correct() self.stop_the_thread(manager)
def _wait_for_new(self, timeout, do_skip, **kwargs): """Wait for a log message which doesn't exist yet. Called via wait_for. """ __tracebackhide__ = lambda e: e.errisinstance(WaitForTimeout) message = kwargs.get('message', None) if message is not None: elided = quteutils.elide(repr(message), 50) self._log("\n----> Waiting for {} in the log".format(elided)) spy = QSignalSpy(self.new_data) elapsed_timer = QElapsedTimer() elapsed_timer.start() while True: # Skip if there are pending messages causing a skip self._maybe_skip() got_signal = spy.wait(timeout) if not got_signal or elapsed_timer.hasExpired(timeout): msg = "Timed out after {}ms waiting for {!r}.".format( timeout, kwargs) if do_skip: pytest.skip(msg) else: raise WaitForTimeout(msg) match = self._wait_for_match(spy, kwargs) if match is not None: if message is not None: self._log("----> found it") return match
def wait_for_completion(self, manager): spy = QSignalSpy(manager.allDownloaded) self.assertTrue(spy.isValid()) manager.start() received = spy.wait(timeout=500) self.assertTrue(received)
def _wait_for_new(self, timeout, do_skip, **kwargs): """Wait for a log message which doesn't exist yet. Called via wait_for. """ __tracebackhide__ = lambda e: e.errisinstance(WaitForTimeout) message = kwargs.get('message', None) if message is not None: elided = quteutils.elide(repr(message), 100) self._log("\n----> Waiting for {} in the log".format(elided)) spy = QSignalSpy(self.new_data) elapsed_timer = QElapsedTimer() elapsed_timer.start() while True: # Skip if there are pending messages causing a skip self._maybe_skip() got_signal = spy.wait(timeout) if not got_signal or elapsed_timer.hasExpired(timeout): msg = "Timed out after {}ms waiting for {!r}.".format( timeout, kwargs) if do_skip: pytest.skip(msg) else: raise WaitForTimeout(msg) match = self._wait_for_match(spy, kwargs) if match is not None: if message is not None: self._log("----> found it") return match raise quteutils.Unreachable
def test_finished_state(self): worker = Worker() worker.configure(destination=self.image_net_home, number_of_images=5, images_per_category=10) change_spy = QSignalSpy(worker.stateChanged) worker.start_download() while not worker.complete: received = change_spy.wait(500) worker = Worker() self.assertEqual(worker.download_state, 'finished') self.assertTrue(worker.complete) worker = Worker() expected_path = os.path.abspath(self.image_net_home) state_data = json.loads(worker.state_data_json) self.assertEqual(state_data['downloadPath'], expected_path) self.assertEqual(state_data['numberOfImages'], 5) self.assertEqual(state_data['imagesPerCategory'], 10) self.assertEqual(state_data['timeLeft'], '0 seconds') self.assertEqual(state_data['imagesLoaded'], 5) self.assertEqual(state_data['failures'], 0) self.assertEqual(state_data['failedUrls'], []) self.assertEqual(state_data['progress'], 1.0)
def wait_for(self, timeout=None, *, override_waited_for=False, do_skip=False, **kwargs): """Wait until a given value is found in the data. Keyword arguments to this function get interpreted as attributes of the searched data. Every given argument is treated as a pattern which the attribute has to match against. Args: timeout: How long to wait for the message. override_waited_for: If set, gets triggered by previous messages again. do_skip: If set, call pytest.skip on a timeout. Return: The matched line. """ __tracebackhide__ = True if timeout is None: if do_skip: timeout = 2000 elif 'CI' in os.environ: timeout = 15000 else: timeout = 5000 if not kwargs: raise TypeError("No keyword arguments given!") for key in kwargs: assert key in self.KEYS # Search existing messages existing = self._wait_for_existing(override_waited_for, **kwargs) if existing is not None: return existing # If there is none, wait for the message spy = QSignalSpy(self.new_data) elapsed_timer = QElapsedTimer() elapsed_timer.start() while True: # Skip if there are pending messages causing a skip self._maybe_skip() got_signal = spy.wait(timeout) if not got_signal or elapsed_timer.hasExpired(timeout): msg = "Timed out after {}ms waiting for {!r}.".format( timeout, kwargs) if do_skip: pytest.skip(msg) else: raise WaitForTimeout(msg) match = self._wait_for_match(spy, kwargs) if match is not None: return match
def wait_for(self, timeout=None, *, override_waited_for=False, **kwargs): """Wait until a given value is found in the data. Keyword arguments to this function get interpreted as attributes of the searched data. Every given argument is treated as a pattern which the attribute has to match against. Args: timeout: How long to wait for the message. override_waited_for: If set, gets triggered by previous messages again. Return: The matched line. """ __tracebackhide__ = True if timeout is None: if 'CI' in os.environ: timeout = 15000 else: timeout = 5000 if not kwargs: raise TypeError("No keyword arguments given!") for key in kwargs: assert key in self.KEYS # Search existing messages existing = self._wait_for_existing(override_waited_for, **kwargs) if existing is not None: return existing # If there is none, wait for the message spy = QSignalSpy(self.new_data) elapsed_timer = QElapsedTimer() elapsed_timer.start() while True: got_signal = spy.wait(timeout) if not got_signal or elapsed_timer.hasExpired(timeout): raise WaitForTimeout("Timed out after {}ms waiting for " "{!r}.".format(timeout, kwargs)) for args in spy: assert len(args) == 1 line = args[0] matches = [] for key, expected in kwargs.items(): value = getattr(line, key) matches.append(self._match_data(value, expected)) if all(matches): # If we waited for this line, chances are we don't mean the # same thing the next time we use wait_for and it matches # this line again. line.waited_for = True return line
def wait_for_signal(self, signal, value): # halt the execution of the test until either only signal (in this # case value must be None), or a specific value associated with signal # is emitted; spy.wait() runs an event loop until it registers the # signal spy = QSignalSpy(signal) if value is None: spy.wait() else: # wait for x*5 seconds (spy.wait() loops for 5 seconds) for i in range(10): # if a signal can emit different values, the while loop runs # until the signal emits the desired value; every emitted value # is appended to spy (last value is most recently emitted one) spy.wait() # print(len(spy), spy[-1][0]) if spy[-1][0] == value: break
def wait_for(self, timeout=None, **kwargs): """Wait until a given value is found in the data. Keyword arguments to this function get interpreted as attributes of the searched data. Every given argument is treated as a pattern which the attribute has to match against. Return: The matched line. """ if timeout is None: if 'CI' in os.environ: timeout = 15000 else: timeout = 5000 # Search existing messages for line in self._data: matches = [] for key, expected in kwargs.items(): value = getattr(line, key) matches.append(self._match_data(value, expected)) if all(matches) and not line.waited_for: # If we waited for this line, chances are we don't mean the # same thing the next time we use wait_for and it matches # this line again. line.waited_for = True return line # If there is none, wait for the message spy = QSignalSpy(self.new_data) elapsed_timer = QElapsedTimer() elapsed_timer.start() while True: got_signal = spy.wait(timeout) if not got_signal or elapsed_timer.hasExpired(timeout): raise WaitForTimeout("Timed out after {}ms waiting for " "{!r}.".format(timeout, kwargs)) for args in spy: assert len(args) == 1 line = args[0] matches = [] for key, expected in kwargs.items(): value = getattr(line, key) matches.append(self._match_data(value, expected)) if all(matches): # If we waited for this line, chances are we don't mean the # same thing the next time we use wait_for and it matches # this line again. line.waited_for = True return line
def test_monitor_times(self): app = QtWidgets.QApplication(sys.argv) status = DogStatus() status_spy = QSignalSpy(status.pooping.status_changed) time_spy = QSignalSpy(status.pooping.time_changed) status.start() self.assertEqual(len(time_spy), 0) status_emitted = status_spy.wait(1) self.assertTrue(status_emitted) self.assertEqual(len(time_spy), 1)
def test_case3(self): try: for i in range(9): QTest.mouseClick(gv.window.toggleButton, Qt.LeftButton) QTest.qWait(100) spy = QSignalSpy(gv.window.notify) t = spy.wait(timeout=10000) print(f"Spy signal: {t}") except Exception as e: self.fail(e)
def receive_message(self, sniffer): if len(sniffer.messages) > 0: return sniffer.messages.pop(0) spy = QSignalSpy(sniffer.message_sniffed) if spy.wait(self.project_manager.simulator_timeout_ms): try: return sniffer.messages.pop(0) except IndexError: self.log_message("Could not receive message") return None else: self.log_message("Receive timeout") return None
def test_wait_until_download_complete(self): worker = Worker() worker.configure(destination=self.image_net_home, number_of_images=10, images_per_category=5) change_spy = QSignalSpy(worker.stateChanged) worker.start_download() while not worker.complete: received = change_spy.wait(500) self.assertEqual(worker.download_state, 'finished') self.assertTrue(worker.complete)
def _assert_signal_emitted(self, signal): app_state = AppState() manager = DownloadManager(app_state) conf = DownloadConfiguration(number_of_images=5, images_per_category=10, download_destination=self.image_net_home) app_state.set_configuration(conf) signal = getattr(manager, signal) spy = QSignalSpy(signal) manager.start() received = spy.wait(timeout=500) self.assertTrue(received) self.stop_the_thread(manager)