Beispiel #1
0
 def test_register_same_name(self):
     """
     Trying to register a Tukio task with a name already used must raise a
     `ValueError` exception.
     """
     register('my-coro-task')(other_coro)
     with self.assertRaisesRegex(ValueError, 'already registered'):
         register('my-coro-task')(other_coro)
Beispiel #2
0
 def test_register_same_name(self):
     """
     Trying to register a Tukio task with a name already used must raise a
     `ValueError` exception.
     """
     register('my-coro-task')(other_coro)
     with self.assertRaisesRegex(ValueError, 'already registered'):
         register('my-coro-task')(other_coro)
Beispiel #3
0
 def setUpClass(cls):
     # Save the initial state of `TaskRegistry`
     cls._backup = _save_registry()
     # Register all tasks used in the tests
     register('my-task-holder', 'do_it')(MyTaskHolder)
     register('my-coro-task')(my_coro_task)
     register('task-bad-inputs', 'dummy')(BadInitTaskHolder1)
     register('task-init-exc', 'dummy')(BadInitTaskHolder2)
     register('task-bad-coro', 'dummy')(BadExecTaskHolder1)
Beispiel #4
0
 def test_register_basic_class(self):
     """
     A class that does not inherit from `TaskHolder` is still a valid task
     holder if registered properly.
     """
     register('basic-task', 'mycoro')(BasicTaskHolder)
     klass, coro_fn = TaskRegistry.get('basic-task')
     self.assertIs(klass, BasicTaskHolder)
     self.assertIs(coro_fn, BasicTaskHolder.mycoro)
Beispiel #5
0
 def setUpClass(cls):
     # Save the initial state of `TaskRegistry`
     cls._backup = _save_registry()
     # Register all tasks used in the tests
     register('my-task-holder', 'do_it')(MyTaskHolder)
     register('my-coro-task')(my_coro_task)
     register('task-bad-inputs', 'dummy')(BadInitTaskHolder1)
     register('task-init-exc', 'dummy')(BadInitTaskHolder2)
     register('task-bad-coro', 'dummy')(BadExecTaskHolder1)
Beispiel #6
0
 def test_register_basic_class(self):
     """
     A class that does not inherit from `TaskHolder` is still a valid task
     holder if registered properly.
     """
     register('basic-task', 'mycoro')(BasicTaskHolder)
     klass, coro_fn = TaskRegistry.get('basic-task')
     self.assertIs(klass, BasicTaskHolder)
     self.assertIs(coro_fn, BasicTaskHolder.mycoro)
Beispiel #7
0
 def setUpClass(cls):
     cls._backup = _save_registry()
     register('my-task-holder', 'do_it')(MyTaskHolder)
     register('basic-holder', 'mycoro')(BasicTaskHolder)
     register('dummy-coro')(my_coro_task)
     register('bad-coro-task')(bad_coro_task)
     cls.loop = asyncio.get_event_loop()
     cls.holder = MyTaskHolder({'hello': 'world'})
     cls.basic = BasicTaskHolder({'hello': 'world'})
Beispiel #8
0
 def setUpClass(cls):
     cls._backup = _save_registry()
     register('my-task-holder', 'do_it')(MyTaskHolder)
     register('basic-holder', 'mycoro')(BasicTaskHolder)
     register('dummy-coro')(my_coro_task)
     register('bad-coro-task')(bad_coro_task)
     cls.loop = asyncio.get_event_loop()
     cls.holder = MyTaskHolder({'hello': 'world'})
     cls.basic = BasicTaskHolder({'hello': 'world'})
Beispiel #9
0
    def test_register_not_coroutine(self):
        """
        Trying to register a function or method that is not a coroutine must
        raise a `TypeError` exception.
        """
        err = 'not a coroutine function'
        registered = len(TaskRegistry.all())

        # A regular function cannot be registered
        with self.assertRaisesRegex(TypeError, err):
            register('dummy')(my_func)

        # A generator cannot be registered either
        with self.assertRaisesRegex(TypeError, err):
            register('dummy')(my_gen)
        with self.assertRaisesRegex(TypeError, err):
            register('dummy')(my_gen())

        # A simple method is not a valid task
        with self.assertRaisesRegex(TypeError, err):
            register('dummy-task', 'dummy')(BadInitTaskHolder3)

        # No new item must have been registered
        self.assertEqual(registered, len(TaskRegistry.all()))
Beispiel #10
0
    def test_register_not_coroutine(self):
        """
        Trying to register a function or method that is not a coroutine must
        raise a `TypeError` exception.
        """
        err = 'not a coroutine function'
        registered = len(TaskRegistry.all())

        # A regular function cannot be registered
        with self.assertRaisesRegex(TypeError, err):
            register('dummy')(my_func)

        # A generator cannot be registered either
        with self.assertRaisesRegex(TypeError, err):
            register('dummy')(my_gen)
        with self.assertRaisesRegex(TypeError, err):
            register('dummy')(my_gen())

        # A simple method is not a valid task
        with self.assertRaisesRegex(TypeError, err):
            register('dummy-task', 'dummy')(BadInitTaskHolder3)

        # No new item must have been registered
        self.assertEqual(registered, len(TaskRegistry.all()))
Beispiel #11
0
    def test_bad_register_class(self):
        """
        Trying to register a class with invalid/missing parameters passed to
        `register()` must raise a `ValueError` or `AttributeError` exception.
        """
        registered = len(TaskRegistry.all())

        # Register a class without the name of the method to register
        with self.assertRaisesRegex(TypeError, 'getattr()'):
            register('dummy-task')(MyTaskHolder)

        # Register a class with an invalid `coro_name` arg
        with self.assertRaisesRegex(TypeError, 'getattr()'):
            register('dummy-task', object())(MyTaskHolder)

        # Register a class with a wrong method name
        with self.assertRaisesRegex(AttributeError, 'yolo'):
            register('dummy-task', 'yolo')(MyTaskHolder)

        # No new item must have been registered
        self.assertEqual(registered, len(TaskRegistry.all()))
Beispiel #12
0
    def test_bad_register_class(self):
        """
        Trying to register a class with invalid/missing parameters passed to
        `register()` must raise a `ValueError` or `AttributeError` exception.
        """
        registered = len(TaskRegistry.all())

        # Register a class without the name of the method to register
        with self.assertRaisesRegex(TypeError, 'getattr()'):
            register('dummy-task')(MyTaskHolder)

        # Register a class with an invalid `coro_name` arg
        with self.assertRaisesRegex(TypeError, 'getattr()'):
            register('dummy-task', object())(MyTaskHolder)

        # Register a class with a wrong method name
        with self.assertRaisesRegex(AttributeError, 'yolo'):
            register('dummy-task', 'yolo')(MyTaskHolder)

        # No new item must have been registered
        self.assertEqual(registered, len(TaskRegistry.all()))
Beispiel #13
0
    def test_valid_registrations(self):
        """
        Check the current registry (provisioned by registered task holder and
        coroutine) holds the right references.
        """
        # A task holder is registered as a tuple made of (klass, coro_fn)
        register('my-task-holder', 'do_it')(MyTaskHolder)
        klass, coro_fn = TaskRegistry.get('my-task-holder')
        self.assertIs(klass, MyTaskHolder)
        self.assertIs(coro_fn, MyTaskHolder.do_it)
        self.assertEqual(MyTaskHolder.TASK_NAME, 'my-task-holder')

        # A coroutine is registered as a tuple made of (None, coro_fn)
        register('my-coro-task')(my_coro_task)
        klass, coro_fn = TaskRegistry.get('my-coro-task')
        self.assertIs(klass, None)
        self.assertIs(coro_fn, my_coro_task)

        # A coroutine can be registered sevral times with distinct names.
        # Note that an extra parameter passed to register will be ignored.
        register('other-coro-task', 'dummy')(my_coro_task)
        klass, coro_fn = TaskRegistry.get('other-coro-task')
        self.assertIs(klass, None)
        self.assertIs(coro_fn, my_coro_task)
Beispiel #14
0
    def test_valid_registrations(self):
        """
        Check the current registry (provisioned by registered task holder and
        coroutine) holds the right references.
        """
        # A task holder is registered as a tuple made of (klass, coro_fn)
        register('my-task-holder', 'do_it')(MyTaskHolder)
        klass, coro_fn = TaskRegistry.get('my-task-holder')
        self.assertIs(klass, MyTaskHolder)
        self.assertIs(coro_fn, MyTaskHolder.do_it)
        self.assertEqual(MyTaskHolder.TASK_NAME, 'my-task-holder')

        # A coroutine is registered as a tuple made of (None, coro_fn)
        register('my-coro-task')(my_coro_task)
        klass, coro_fn = TaskRegistry.get('my-coro-task')
        self.assertIs(klass, None)
        self.assertIs(coro_fn, my_coro_task)

        # A coroutine can be registered sevral times with distinct names.
        # Note that an extra parameter passed to register will be ignored.
        register('other-coro-task', 'dummy')(my_coro_task)
        klass, coro_fn = TaskRegistry.get('other-coro-task')
        self.assertIs(klass, None)
        self.assertIs(coro_fn, my_coro_task)