Ejemplo n.º 1
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 == {}
Ejemplo n.º 2
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 == {}
Ejemplo n.º 3
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"}
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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
Ejemplo n.º 8
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)
Ejemplo n.º 9
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)
Ejemplo n.º 10
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
Ejemplo n.º 11
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
Ejemplo n.º 12
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)
Ejemplo n.º 13
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
Ejemplo n.º 14
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)
Ejemplo n.º 15
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
Ejemplo n.º 16
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
Ejemplo n.º 17
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
Ejemplo n.º 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
Ejemplo n.º 19
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
Ejemplo n.º 20
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
Ejemplo n.º 21
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
Ejemplo n.º 22
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