def test_freeze_empty_args(self): test_future = Future("TestData") def test_method(): pass test_future.freeze(test_method) assert test_future.method == test_method assert test_future.args == () assert test_future.kwdargs == {}
def test_freeze(self): test_future = Future("TestData") def test_method(*args, **kwargs): pass test_future.freeze( test_method, "arg1", "arg2", kwarg1="test", kwarg2="test") assert test_future.method == test_method assert test_future.args == ("arg1", "arg2") assert test_future.kwdargs == {"kwarg1": "test", "kwarg2": "test"}
def test_get_value_no_suppress_exception(self): test_future = Future("TestData") def test_method(*args, **kwargs): raise TestError("Test Error") test_future.freeze(test_method) self.future_thread = threading.Thread(target=test_future.thaw) self.future_thread.start() self.assertRaises(TestError, test_future.get_value)
def test_get_value_no_block_fail(self): test_future = Future("TestData") def test_method(*args, **kwargs): time.sleep(2) return True test_future.freeze(test_method) self.future_thread = threading.Thread(target=test_future.thaw) self.future_thread.start() self.assertRaises(TimeoutError, test_future.get_value, False)
def test_wait_timeout(self): test_future = Future("TestData") def test_method(*args, **kwargs): time.sleep(2) return True test_future.freeze(test_method) self.future_thread = threading.Thread(target=test_future.thaw) self.future_thread.start() self.assertRaises(TimeoutError, test_future.wait, 1)
def test_thaw_not_suppress_exception_raise_exception(self): test_future = Future("TestData") def test_method(): return True test_future.freeze( test_method, "arg1", "arg2", kwarg1="test", kwarg2="test") self.assertRaises(TypeError, test_future.thaw, False) assert test_future.res == None assert test_future.evt.isSet() == False
def test_get_value_suppress_exception(self): test_future = Future("TestData") def test_method(*args, **kwargs): raise _TestError("Test Error") test_future.freeze(test_method) self.future_thread = threading.Thread(target=test_future.thaw) self.future_thread.start() future_value = test_future.get_value(True, None, True) assert isinstance(future_value, Exception)
def test_get_value_suppress_exception(self): test_future = Future("TestData") def test_method(*args, **kwargs): raise TestError("Test Error") test_future.freeze(test_method) self.future_thread = threading.Thread(target=test_future.thaw) self.future_thread.start() future_value = test_future.get_value(True, None, True) assert isinstance(future_value, Exception)
def test_thaw_not_suppress_exception_raise_exception(self): test_future = Future("TestData") def test_method(): return True test_future.freeze( test_method, "arg1", "arg2", kwarg1="test", kwarg2="test") with pytest.raises(TypeError): test_future.thaw(False) assert test_future.res is None assert test_future.evt.is_set() is False
def test_thaw_not_suppress_exception_no_exception(self): test_future = Future("TestData") def test_method(*args, **kwargs): return True test_future.freeze(test_method) expected = True actual = test_future.thaw(False) assert expected == actual assert test_future.res == True assert test_future.evt.isSet() == True
def test_get_value_block_timeout(self): test_future = Future("TestData") def test_method(*args, **kwargs): time.sleep(2) return True test_future.freeze(test_method) self.future_thread = threading.Thread(target=test_future.thaw) self.future_thread.start() with pytest.raises(TimeoutError): test_future.get_value(True, 1)
def test_thaw_suppress_exception_no_exception(self): test_future = Future("TestData") def test_method(*args, **kwargs): return True test_future.freeze(test_method) expected = True actual = test_future.thaw() assert expected == actual assert test_future.res is True assert test_future.evt.is_set() is True
def test_thaw_suppress_exception_exception(self): test_future = Future("TestData") def test_method(): return True test_future.freeze( test_method, "arg1", "arg2", kwarg1="test", kwarg2="test") test_result = test_future.thaw() assert isinstance(test_result, TypeError) assert isinstance(test_future.res, TypeError) assert test_future.evt.isSet() == True
def test_get_value_block_no_timeout(self): test_future = Future("TestData") def test_method(*args, **kwargs): time.sleep(2) return True test_future.freeze(test_method) self.future_thread = threading.Thread(target=test_future.thaw) self.future_thread.start() expected = True actual = test_future.get_value() assert expected == actual
def test_thaw_not_suppress_exception_raise_exception(self): test_future = Future("TestData") def test_method(): return True test_future.freeze(test_method, "arg1", "arg2", kwarg1="test", kwarg2="test") with pytest.raises(TypeError): test_future.thaw(False) assert test_future.res is None assert test_future.evt.is_set() is False
def test_get_value_no_block_pass(self): test_future = Future("TestData") def test_method(*args, **kwargs): return True test_future.freeze(test_method) self.future_thread = threading.Thread(target=test_future.thaw) self.future_thread.start() # Making the test running thread to sleep for a while # for the self.future_thread to complete and ensure success time.sleep(0.5) expected = True actual = test_future.get_value() assert expected == actual
def autoload_ginga_image(self, filename, extnum, cachekey): """Automatically load a given image extension into Ginga viewer. Parameters ---------- filename : str Image filename. extnum : int Image extension number. cachekey : str Key for Ginga data cache. Usually, this is in the format of ``prefix[extname, extver]``. Returns ------- image : ``ginga.AstroImage.AstroImage`` Ginga image object. """ # Image already loaded if cachekey in self.chinfo.datasrc: self.logger.debug('Loading {0} from cache'.format(cachekey)) image = self.chinfo.datasrc[cachekey] # Auto load image data else: self.logger.debug('Loading {0} from {1}'.format( cachekey, filename)) image = self.fv.load_image(filename, idx=extnum) future = Future() future.freeze(self.fv.load_image, filename, idx=extnum) image.set(path=filename, idx=extnum, name=cachekey, image_future=future) self.fv.add_image(cachekey, image, chname=self.chname, silent=True) self.fv.advertise_image(self.chname, image) return image
def autoload_ginga_image(self, filename, extnum, cachekey): """Automatically load a given image extension into Ginga viewer. Parameters ---------- filename : str Image filename. extnum : int Image extension number. cachekey : str Key for Ginga data cache. Usually, this is in the format of ``prefix[extname, extver]``. Returns ------- image : ``ginga.AstroImage.AstroImage`` Ginga image object. """ # Image already loaded if cachekey in self.chinfo.datasrc: self.logger.debug('Loading {0} from cache'.format(cachekey)) image = self.chinfo.datasrc[cachekey] # Auto load image data else: self.logger.debug( 'Loading {0} from {1}'.format(cachekey, filename)) image = self.fv.load_image(filename, idx=extnum) future = Future() future.freeze(self.fv.load_image, filename, idx=extnum) image.set(path=filename, idx=extnum, name=cachekey, image_future=future) self.fv.add_image(cachekey, image, chname=self.chname, silent=True) self.fv.advertise_image(self.chname, image) return image