def run_behavior(self, behavior_script, timeout=900): self.send_to_chrome( method='Runtime.evaluate', suppress_logging=True, params={'expression': behavior_script}) start = time.time() while True: elapsed = time.time() - start if elapsed > timeout: logging.info( 'behavior reached hard timeout after %.1fs', elapsed) return brozzler.sleep(7) self.websock_thread.expect_result(self._command_id.peek()) msg_id = self.send_to_chrome( method='Runtime.evaluate', suppress_logging=True, params={'expression': 'umbraBehaviorFinished()'}) try: self._wait_for( lambda: self.websock_thread.received_result(msg_id), timeout=5) msg = self.websock_thread.pop_result(msg_id) if (msg and 'result' in msg and not ('exceptionDetails' in msg['result']) and not ('wasThrown' in msg['result'] and msg['result']['wasThrown']) and 'result' in msg['result'] and type(msg['result']['result']['value']) == bool and msg['result']['result']['value']): self.logger.info('behavior decided it has finished') return except BrowsingTimeout: pass
def accept_immediately(): try: with brozzler.thread_accept_exceptions(): brozzler.sleep(2) except Exception as e: nonlocal thread_caught_exception thread_caught_exception = e
def accept_eventually(): try: brozzler.sleep(2) with brozzler.thread_accept_exceptions(): pass except Exception as e: nonlocal thread_caught_exception thread_caught_exception = e
def _wait_for(self, callback, timeout=None): ''' Spins until callback() returns truthy. ''' start = time.time() while True: if callback(): return elapsed = time.time() - start if timeout and elapsed > timeout: raise BrowsingTimeout('timed out after %.1fs waiting for: %s' % (elapsed, callback)) brozzler.sleep(self._wait_interval)
def delay_context_exit(): gate = brozzler.thread_accept_exceptions() orig_exit = type(gate).__exit__ try: type(gate).__exit__ = lambda self, et, ev, t: ( brozzler.sleep(2), orig_exit(self, et, ev, t), False)[-1] with brozzler.thread_accept_exceptions() as gate: brozzler.sleep(2) except Exception as e: nonlocal thread_caught_exception thread_caught_exception = e finally: type(gate).__exit__ = orig_exit
def _wait_for(self, callback, timeout=None): ''' Spins until callback() returns truthy. ''' start = time.time() while True: if callback(): return elapsed = time.time() - start if timeout and elapsed > timeout: raise BrowsingTimeout( 'timed out after %.1fs waiting for: %s' % ( elapsed, callback)) brozzler.sleep(self._wait_interval)
def two_with_blocks(): try: with brozzler.thread_accept_exceptions(): time.sleep(2) return # test fails except Exception1 as e: pass except: return # fail test try: with brozzler.thread_accept_exceptions(): brozzler.sleep(2) except Exception as e: nonlocal thread_caught_exception thread_caught_exception = e
def never_accept(): try: brozzler.sleep(2) except Exception as e: nonlocal thread_caught_exception thread_caught_exception = e