def test_stores_exception(self): exception_raised = Exception('intentional') def boom(): raise exception_raised sut = TimeController(target=boom, daemon=True) sut.start() sut.join() expect(sut.exception_caught).to(equal(exception_raised))
def test_expires_after_timeout(self): timeout = 1 self.context.set_env(browserstack_tunnel_timeout=timeout) e = WebDriverException(ERROR_MESSAGE) self.fake_webdriver.init_exception = e clock = TimeController(target=Browser, parent_context=self.context) clock.start() sleep(0.05) # Give the SUT a chance to start clock.advance(seconds=timeout + 0.01) clock.join() expect(clock.exception_caught).to(equal(e))
def test_waits_up_to_configured_limit(self): self.webdriver.Remote = func_that_raises(AllBrowsersBusy()) ctl = TimeController(target=Browser, parent_context=self.context) ctl.start() sleep(0.05) ctl.advance(seconds=self.timeout - 0.01) sleep(0.05) try: expect(ctl.exception_caught).to(equal(None)) finally: ctl.advance(days=180) ctl.join()
def test_re_raises_after_timeout(self): raised = AllBrowsersBusy() self.webdriver.Remote = func_that_raises(raised) ctl = TimeController(target=Browser, parent_context=self.context) ctl.start() sleep(0.05) ctl.advance(seconds=self.timeout + 0.01) sleep(0.05) try: expect(ctl.exception_caught).to(equal(raised)) finally: ctl.advance(days=180) ctl.join()
def test_waits_up_to_timeout_for_element_to_appear(self): timeout = 42 def find(): return self.sut.find_unique_element(id="whatever", timeout=timeout) clock = TimeController(target=find) clock.start() clock.advance(seconds=timeout - 0.001) element = object() self.fake.elements = [element] clock.join() expect(clock.exception_caught).to(equal(None)) expect(clock.value_returned).to(equal(element))
def test_time_controller(self): real_spam = 'real SPAM' fake_spam = 'phoney balogna' parent = open_dependency_context() try: parent.inject(real_spam, fake_spam) tc = TimeController(target=lambda: dependency(real_spam), parent_context=parent) tc.start() tc.join() finally: parent.close() if tc.exception_caught: raise tc.exception_caught expect(tc.value_returned).to(equal(fake_spam))
def test_advance_advances_time_by_specified_delta(self): reported_time = None def canary(): nonlocal reported_time while True: reported_time = dependency(datetime).now() sut = TimeController(target=canary, daemon=True) sut.start() sleep(0.05) # Give SUT a chance to get started start_time = sut.fake_datetime.now() advance_delta = 42 sut.advance(seconds=advance_delta) sleep(0.05) # Give SUT a chance to cycle expect(reported_time).to( equal(start_time + timedelta(seconds=advance_delta)))
def test_wait_for_raises_timeout_error_if_timeout_exceeded(self): calls = 0 timeout = 5 throttle = 0.1 def func(): nonlocal calls calls += 1 return False def do_it(): wait_for(func=func, timeout=timeout, throttle=throttle) tc = TimeController(target=do_it) tc.start() tc.advance(seconds=timeout + 0.1) sleep(0.15) # Give the thread a chance to cycle expect(tc.exception_caught).to(be_a(TimeoutError))
def test_wait_for_retries(self): calls = 0 timeout = 5 throttle = 0.1 def func(): nonlocal calls calls += 1 return False def do_it(): wait_for(func=func, timeout=timeout, throttle=throttle) tc = TimeController(target=do_it) tc.start() tc.advance(seconds=timeout - 0.1) last_check = calls sleep(2 * throttle) expect(calls).to(be_above(last_check))
def test_retries_up_to_specified_timeout(self): calls = 0 timeout = 5 throttle = 0.01 def func(): nonlocal calls calls += 1 raise SpamException("intentional") def do_it(): call_with_exception_tolerance(func=func, tolerate=SpamException, timeout=timeout, throttle=throttle) tc = TimeController(target=do_it) tc.start() tc.advance(seconds=timeout - 0.01) last_check = calls sleep(2 * throttle) expect(calls).to(be_above(last_check))
def test_stores_return_value(self): expected = 42 sut = TimeController(target=lambda: expected, daemon=True) sut.start() sut.join() expect(sut.value_returned).to(equal(expected))