Ejemplo n.º 1
0
 def test_set_exception(self):
     f = Future(self.task)
     e = ValueError("test Error")
     f.set_exception(e)
     self.assertTrue(f.done())
     self.assertRaises(ValueError, f.result, 0)
     self.assertEqual(f.exception(), e)
Ejemplo n.º 2
0
    def test_result(self):
        f = Future(self.context)

        def wait_all_futures(fs, timeout):
            fs[0].set_result(32)

        self.context.wait_all_futures.side_effect = wait_all_futures

        assert f.result() == 32
        self.context.wait_all_futures.assert_called_once_with([f], None)
        self.context.wait_all_futures.reset_mock()
        assert f.result() == 32
        self.context.wait_all_futures.assert_not_called()
Ejemplo n.º 3
0
 def test_exception(self):
     # timeout due to no response arriving
     f0 = Future(self.task)
     f1 = Future(self.task)
     self.task._futures = {0: f0, 1: f1}
     self.assertRaises(queue.Empty, f0.exception, 0)
     # return after waiting for response object
     resp0 = Return(0, None, None)
     resp0.set_value('testVal')
     resp1 = Error(1, None, None)
     resp1.set_message('test Error')
     self.task.q.put(resp0)
     self.task.q.put(resp1)
     self.assertEqual(f1.exception(), 'test Error')
Ejemplo n.º 4
0
 def test_result(self):
     # timeout due to no response arriving
     f0 = Future(self.task)
     f1 = Future(self.task)
     self.task._futures = {0: f0, 1: f1}
     self.assertRaises(queue.Empty, f0.result, 0)
     # return after waiting for response object
     resp0 = Return(0, None, None)
     resp0.set_value('testVal')
     resp1 = Error(1, None, "test Error")
     resp1.set_message('test Error')
     self.task.q.put(resp0)
     self.task.q.put(resp1)
     self.assertEqual(f0.result(), 'testVal')
Ejemplo n.º 5
0
    def test_exception(self):
        f = Future(self.context)

        def wait_all_futures(fs, timeout):
            fs[0].set_exception(MyError())

        self.context.wait_all_futures.side_effect = wait_all_futures

        with self.assertRaises(MyError):
            f.result()

        self.context.wait_all_futures.assert_called_once_with([f], None)
        self.context.wait_all_futures.reset_mock()
        self.assertIsInstance(f.exception(), MyError)
        self.context.wait_all_futures.assert_not_called()
Ejemplo n.º 6
0
 def test_set_exception(self):
     f = Future(self.context)
     e = ValueError("test Error")
     f.set_exception(e)
     assert f.done()
     with self.assertRaises(ValueError):
         f.result(timeout=0)
     assert f.exception() == e
Ejemplo n.º 7
0
 def test_exception(self):
     # timeout due to no response arriving
     f0 = Future(self.task)
     f1 = Future(self.task)
     self.task._futures = {0: f0, 1: f1}
     self.assertRaises(queue.Empty, f0.exception, 0)
     # return after waiting for response object
     resp0 = Return(0, None, None)
     resp0.set_value('testVal')
     resp1 = Error(1, None, None)
     resp1.set_message('test Error')
     self.task.q.put(resp0)
     self.task.q.put(resp1)
     with self.assertRaises(ValueError) as cm:
         f1.exception()
     self.assertEqual(str(cm.exception), 'test Error')
Ejemplo n.º 8
0
    def put_async(self, attr_or_items, value=None):
        """"puts a value or values into an attribute or attributes and returns
            immediately

            Args:
                attr_or_items (Attribute or Dict): The attribute or dictionary
                    of {attributes: values} to set
                value (object): For single attr, the value set

            Returns:
                 a list of futures to monitor when each put completes"""
        if value:
            attr_or_items = {attr_or_items: value}
        result_f = []

        for attr, value in attr_or_items.items():
            assert isinstance(attr, Attribute), \
                "Expected Attribute, got %r" % (attr,)

            endpoint = attr.path_relative_to(self.process) + ["value"]
            request = Put(None, self.q, endpoint, value)
            f = Future(self)
            new_id = self._save_future(f)
            request.set_id(new_id)
            self.process.q.put(request)
            result_f.append(f)

        return result_f
Ejemplo n.º 9
0
    def put_async(self, attr_or_items, value=None):
        """"puts a value or values into an attribute or attributes and returns
            immediately

            Args:
                attr_or_items (Attribute or Dict): The attribute or dictionary
                    of {attributes: values} to set
                value (object): For single attr, the value set

            Returns:
                 a list of futures to monitor when each put completes"""
        if value:
            attr_or_items = {attr_or_items: value}
        result_f = []

        for attr, value in attr_or_items.items():
            endpoint = [attr.parent.name, attr.name, "value"]
            request = Post(None, self.q, endpoint, value)
            f = Future(self)
            new_id = self._save_future(f)
            request.set_id(new_id)
            self.process.q.put(request)
            result_f.append(f)

        return result_f
Ejemplo n.º 10
0
    def test_wait_all_missing_futures(self):
        # unsolicited response
        t = Task("testTask", self.proc)
        f1 = Future(t)
        resp10 = Return(10, None, None)
        t.q.put(resp10)
        t.q.put(Task.TASK_STOP)
        self.assertRaises(RuntimeWarning, t.wait_all, f1, 0)

        # same future twice
        f2 = Future(t)
        t._futures = {1: f2}
        resp1 = Return(1, None, None)
        t.q.put(resp1)
        t.q.put(Task.TASK_STOP)
        t.wait_all(f2, 0)
        t.wait_all(f2, 0)
Ejemplo n.º 11
0
    def test_wait_all_missing_futures(self):
        # unsolicited response
        t = Task("testTask", self.proc)
        f1 = Future(t)
        resp10 = Return(10, None, None)
        t.q.put(resp10)
        t.q.put(Spawnable.STOP)
        self.assertRaises(StopIteration, t.wait_all, f1, 0)

        # same future twice
        f2 = Future(t)
        t._futures = {1: f2}
        resp1 = Return(1, None, None)
        t.q.put(resp1)
        t.q.put(Spawnable.STOP)
        t.wait_all(f2, 0)
        t.wait_all(f2, 0)
Ejemplo n.º 12
0
 def test_callback_unexpected(self):
     t = Task("testTask", self.proc)
     resp = Delta(0, None, None)
     t.q.put(resp)
     t.stop()
     t.subscribe(self.attr, self._callback, 3, 5)
     f1 = Future(t)
     t._futures = {1: f1}
     self.assertRaises(ValueError, t.wait_all, f1, 0)
Ejemplo n.º 13
0
    def sleep(self, seconds):
        """Services all futures while waiting

        Args:
            seconds (float): Time to wait
        """
        try:
            self.wait_all([Future(task=None)], seconds)
        except queue.Empty:
            return
Ejemplo n.º 14
0
    def test_callback_error(self):
        t = Task("testTask", self.proc)
        resp = Error(0, None, None)
        resp.set_message('error')
        t.q.put(resp)
        t.stop()

        t.subscribe(self.attr, self._callback, 3, 5)
        f1 = Future(t)
        t._futures = {1: f1}
        self.assertRaises(RuntimeError, t.wait_all, f1, 0)
Ejemplo n.º 15
0
    def test_callback_crash(self):
        t = Task("testTask", self.proc)
        resp = Update(0, None, None)
        resp.set_value('changedVal')
        t.q.put(resp)
        t.stop()

        t.subscribe(self.attr, self._bad_callback)
        f1 = Future(t)
        t._futures = {1: f1}
        self.assertRaises(StopIteration, t.wait_all, f1, 0)
        self.assertEquals(self.bad_called_back, True)
Ejemplo n.º 16
0
 def test_set_exception(self):
     f = Future(self.task)
     e = ValueError("test Error")
     f.set_exception(e)
     self.assertTrue(f.done())
     self.assertRaises(ValueError, f.result, 0)
     self.assertEqual(f.exception(), e)
Ejemplo n.º 17
0
    def post_async(self, method, params):
        """Asynchronously calls a function on a child block

            Returns a list of one future which will proved the return value
            on completion"""
        endpoint = [method.parent.name, method.name]
        request = Post(None, self.q, endpoint, params)
        f = Future(self)
        new_id = self._save_future(f)
        request.set_id(new_id)
        self.process.q.put(request)

        return [f]
Ejemplo n.º 18
0
    def test_subscribe(self):
        t = Task("testTask", self.proc)
        resp = Update(0, None, None)
        resp.set_value('changedVal')
        t.q.put(resp)
        t.stop()

        new_id = t.subscribe(self.attr, self._callback, 3, 5)
        f1 = Future(t)
        t._futures = {1: f1}

        self.assertRaises(StopIteration, t.wait_all, f1, 0)
        self.assertEqual(self.callback_value, 'changedVal')
        self.assertEqual(self.callback_result, 8)
        t.unsubscribe(new_id)
Ejemplo n.º 19
0
    def test_wait_all(self):
        t = Task("testTask", self.proc)
        f1 = Future(t)
        f2 = Future(t)
        f3 = Future(t)
        f0 = Future(t)
        t._futures = {0: f0, 1: f1, 2: f2, 3: f3}
        f_wait1 = [f2, f0]
        self.assertRaises(queue.Empty, t.wait_all, f_wait1, 0)

        resp0 = Return(0, None, None)
        resp0.set_value('testVal')
        resp2 = Error(2, None, None)
        t.q.put(resp0)
        t.q.put(resp2)
        t.wait_all( f_wait1, 0)
        self.assertEqual(t._futures, {1: f1, 3: f3})
        self.assertEqual(f0.done(), True)
        self.assertEqual(f1.done(), False)
        self.assertEqual(f2.done(), True)
        self.assertEqual(f3.done(), False)
        self.assertEqual(self.proc.q.qsize(), 0)

        resp3 = Delta(3, None, None)
        t.q.put(resp3)
        f_wait1 = [f3]
        self.assertRaises(ValueError, t.wait_all, f_wait1, 0.01)
        t.stop()
        self.assertRaises(RuntimeWarning, t.wait_all, f_wait1, 0.01)

        resp1 = Return(1, None, None)
        resp1.set_value('testVal')
        t.q.put(resp1)
        self.assertRaises(queue.Empty, t.wait_all, f_wait1, 0.01)
        self.assertEqual(t._futures, {})

        t._futures = {0: f0, 1: f1, 2: f2}
        t.q.put(resp1)
        t.q.put(Task.TASK_STOP)
        self.assertEqual(f1.result(), 'testVal')
Ejemplo n.º 20
0
    def post_async(self, method, params=None):
        """Asynchronously calls a function on a child block

            Returns a list of one future which will proved the return value
            on completion"""
        assert isinstance(method, MethodMeta), \
            "Expected MethodMeta, got %r" % (method,)

        endpoint = method.path_relative_to(self.process)

        request = Post(None, self.q, endpoint, params)
        f = Future(self)
        new_id = self._save_future(f)
        request.set_id(new_id)
        self.process.q.put(request)

        return [f]
Ejemplo n.º 21
0
    def when_matches(self, attr, value):
        """ Wait for an attribute to become a given value

            Args:
                attr (Attribute): The attribute to wait for
                value (object): the value to wait for

            Returns: a list of one futures which will complete when
                all attribute values match the input"""

        f = Future(self)
        self._save_future(f)
        subscription_ids = []
        subscription_id = self.subscribe(
            attr, self._match_update, f, value, subscription_ids)
        subscription_ids.append(subscription_id)

        return [f]
Ejemplo n.º 22
0
 def _create_future(self):
     future = Future(weakref.proxy(self))
     return future
Ejemplo n.º 23
0
 def test_set_result(self):
     f = Future(self.context)
     f.set_result("testResult")
     assert f.done()
     assert f.result(0) == "testResult"
Ejemplo n.º 24
0
 def test_init(self):
     f = Future(self.task)
     self.assertEqual(f._task, self.task)
Ejemplo n.º 25
0
    def test_wait_all(self):
        t = Task("testTask", self.proc)
        f1 = Future(t)
        f2 = Future(t)
        f3 = Future(t)
        f0 = Future(t)
        t._futures = {0: f0, 1: f1, 2: f2, 3: f3}
        f_wait1 = [f2, f0]
        self.assertRaises(queue.Empty, t.wait_all, f_wait1, 0)

        resp0 = Return(0, None, None)
        resp0.set_value('testVal')
        resp2 = Error(2, None, None)
        t.q.put(resp0)
        t.q.put(resp2)
        self.assertRaises(ValueError, t.wait_all, f_wait1, 0)
        self.assertEqual(t._futures, {1: f1, 3: f3})
        self.assertEqual(f0.done(), True)
        self.assertEqual(f1.done(), False)
        self.assertEqual(f2.done(), True)
        self.assertEqual(f3.done(), False)
        self.assertEqual(self.proc.q.qsize(), 0)

        resp3 = Delta(3, None, None)
        t.q.put(resp3)
        f_wait1 = [f3]
        self.assertRaises(ValueError, t.wait_all, f_wait1, 0.01)
        t.stop()
        self.assertRaises(StopIteration, t.wait_all, f_wait1, 0.01)

        resp1 = Return(1, None, None)
        resp1.set_value('testVal')
        t.q.put(resp1)
        self.assertRaises(queue.Empty, t.wait_all, f_wait1, 0.01)
        self.assertEqual(t._futures, {})

        t._futures = {0: f0, 1: f1, 2: f2}
        t.q.put(resp1)
        t.q.put(Spawnable.STOP)
        self.assertEqual(f1.result(), 'testVal')
Ejemplo n.º 26
0
 def test_set_result(self):
     f = Future(self.task)
     f.set_result("testResult")
     self.assertTrue(f.done())
     self.assertEqual(f.result(0), "testResult")
Ejemplo n.º 27
0
 def test_set_exception(self):
     f = Future(self.task)
     f.set_exception("test Error")
     self.assertTrue(f.done())
     self.assertRaises(RemoteError, f.result, 0)
     self.assertEqual(f.exception(), 'test Error')
Ejemplo n.º 28
0
 def test_set_result(self):
     f = Future(self.task)
     f.set_result("testResult")
     self.assertTrue(f.done())
     self.assertEqual(f.result(0), "testResult")
Ejemplo n.º 29
0
 def test_set_exception(self):
     f = Future(self.task)
     f.set_exception("test Error")
     self.assertTrue(f.done())
     self.assertRaises(RemoteError, f.result, 0)
     self.assertEqual(f.exception(),'test Error')