Пример #1
0
    def test_resolve(self):
        test_future = Future("TestData")

        test_future.resolve(True)

        assert test_future.res is True
        assert test_future.evt.is_set() is True
Пример #2
0
    def test_resolve(self):
        test_future = Future("TestData")

        test_future.resolve(True)

        assert test_future.res == True
        assert test_future.evt.isSet() == True
Пример #3
0
    def test_resolve(self):
        test_future = Future("TestData")

        test_future.resolve(True)

        assert test_future.res is True
        assert test_future.evt.is_set() is True
Пример #4
0
    def test_has_value_set(self):
        test_future = Future("TestData")

        test_future.evt.set()

        expected = True
        actual = test_future.has_value()
        assert expected == actual
Пример #5
0
    def test_has_value_set(self):
        test_future = Future("TestData")

        test_future.evt.set()

        expected = True
        actual = test_future.has_value()
        assert expected == actual
Пример #6
0
    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 == {}
Пример #7
0
    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 == {}
Пример #8
0
    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"}
Пример #9
0
    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)
Пример #10
0
    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)
Пример #11
0
    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)
Пример #12
0
    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
Пример #13
0
    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)
Пример #14
0
    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)
Пример #15
0
    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
Пример #16
0
    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)
Пример #17
0
    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
Пример #18
0
    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
Пример #19
0
    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
Пример #20
0
    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
Пример #21
0
    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
Пример #22
0
    def test_resolve_callback(self):
        test_future = Future("TestData")

        def test_callback(obj):
            try:
                obj.res = not obj.res
            except:
                pass

        test_future.add_callback('resolved', test_callback)

        test_future.resolve(True)

        # Callback reverses the boolean 'res' value
        assert test_future.res == False
        assert test_future.evt.isSet() == True
Пример #23
0
    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
Пример #24
0
    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
Пример #25
0
    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
Пример #26
0
    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
Пример #27
0
    def test_init(self):
        test_future = Future()

        assert hasattr(test_future, 'cb')

        if sys.version_info.major == 2:
            assert isinstance(test_future.evt, threading._Event)
        elif sys.version_info.major == 3:
            assert isinstance(test_future.evt, threading.Event)

        assert test_future.evt.isSet() == False
        assert test_future.res == None
        assert test_future.data == None
        assert 'resolved' in test_future.cb

        expected = []
        actual = test_future.cb['resolved']
        assert expected == actual
Пример #28
0
    def test_init_with_data(self):
        test_future = Future("TestData")

        assert hasattr(test_future, 'cb')

        if sys.version_info.major == 2:
            assert isinstance(test_future.evt, threading._Event)
        elif sys.version_info.major == 3:
            assert isinstance(test_future.evt, threading.Event)

        assert test_future.evt.is_set() is False
        assert test_future.res is None
        assert test_future.data == "TestData"
        assert 'resolved' in test_future.cb

        expected = []
        actual = test_future.cb['resolved']
        assert expected == actual
Пример #29
0
    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)
Пример #30
0
    def test_resolve_callback(self):
        test_future = Future("TestData")

        def test_callback(obj):
            try:
                obj.res = not obj.res
            except Exception:
                pass

        test_future.add_callback('resolved', test_callback)

        test_future.resolve(True)

        # Callback reverses the boolean 'res' value
        assert test_future.res is False
        assert test_future.evt.is_set() is True
Пример #31
0
    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
Пример #32
0
    def test_get_data_no_data(self):
        test_future = Future()

        expected = None
        actual = test_future.get_data()
        assert expected == actual
Пример #33
0
    def test_get_data_some_data(self):
        test_future = Future("TestData")

        expected = "TestData"
        actual = test_future.get_data()
        assert expected == actual
Пример #34
0
    def test_get_data_no_data(self):
        test_future = Future()

        expected = None
        actual = test_future.get_data()
        assert expected == actual
Пример #35
0
    def test_get_data_some_data(self):
        test_future = Future("TestData")

        expected = "TestData"
        actual = test_future.get_data()
        assert expected == actual
Пример #36
0
    def test_has_value_unset(self):
        test_future = Future("TestData")

        expected = False
        actual = test_future.has_value()
        assert expected == actual
Пример #37
0
    def test_has_value_unset(self):
        test_future = Future("TestData")

        expected = False
        actual = test_future.has_value()
        assert expected == actual