Ejemplo n.º 1
0
    def _schedule_action(self, action, args=None, priority=PRIORITY_NORMAL):
        if not hasattr(self, action):
            raise ActionNotFoundError("service %s doesn't have action %s" %
                                      (self.name, action))

        method = getattr(self, action)
        if not callable(method):
            raise ActionNotFoundError("%s is not a function" % action)

        # make sure the argument we pass are correct
        kwargs_enable = False
        s = inspect.signature(method, follow_wrapped=True)
        for param in s.parameters.values():
            if param.kind == param.VAR_KEYWORD:
                kwargs_enable = True
            if args is None:
                args = {}
            if param.default == s.empty and param.name not in args and param.kind != param.VAR_KEYWORD:
                raise BadActionArgumentError(
                    "parameter %s is mandatory but not passed to in args" %
                    param.name)

        if args is not None:
            signature_keys = set(s.parameters.keys())
            args_keys = set(args.keys())
            diff = args_keys.difference(signature_keys)
            if diff and not kwargs_enable:
                raise BadActionArgumentError(
                    'arguments "%s" are not present in the signature of the action'
                    % ','.join(diff))

        task = Task(method, args)
        self.task_list.put(task, priority=priority)
        return task
Ejemplo n.º 2
0
 def _get_tasks(self, nr):
     tasks = []
     for i in range(nr):
         s = FakeService("s%d" % i)
         t = Task(s.foo, {})
         tasks.append(t)
     return tasks
Ejemplo n.º 3
0
    def test_priority(self):

        s1 = FakeService("s1")
        s2 = FakeService("s2")
        s3 = FakeService("s3")

        t1 = Task(s1.foo, {})
        t2 = Task(s1.foo, {})
        t3 = Task(s1.foo, {})

        self.tl.put(t1, priority=PRIORITY_NORMAL)
        self.tl.put(t2, priority=PRIORITY_NORMAL)
        self.tl.put(t3, priority=PRIORITY_SYSTEM)

        tasks = []
        while not self.tl.empty():
            tasks.append(self.tl.get())

        self.assertEqual(tasks, [t3, t1, t2], "task with higher priority should be extracted first")
Ejemplo n.º 4
0
    def test_put(self):
        with self.assertRaises(ValueError, msg="should raise when trying to put an object that is not a Task"):
            self.tl.put('string')

        self.assertTrue(self.tl.empty())
        srv = FakeService('s1')
        t = Task(srv.foo, {})
        self.tl.put(t)
        self.assertFalse(self.tl.empty())
        t2 = self.tl.get()
        self.tl.done(t2)
        self.assertEqual(t, t2, "task extracted from the list should be the same as the task added")
        self.assertEqual(self.tl._done.list()[0].guid, t.guid, "task extracted from the list should be kept in the _done list")