Example #1
0
    def test_update_tasks(self):
        event_obj = models.Event.objects.create(app_name='app',
                                                event_name='event')
        initial_event_updated_on = event_obj.updated_on
        current_task_objs = [
            models.Task.objects.create(name='task_1'),
            models.Task.objects.create(name='task_2')
        ]

        backend = DjangoDBBackend(registry)
        event = Event.local_instance('app', 'event', app=app)
        event.backend_obj = event_obj
        tasks = [
            Task('task_1', queue='queue_1'),
            Task('task_2', queue='queue_2')
        ]
        for task_obj, task in zip(current_task_objs, tasks):
            task.backend_obj = task_obj
        backend.update_tasks(event, tasks)

        event_objs = models.Event.objects.all()
        self.assertEqual(1, event_objs.count())
        event_obj = event_objs[0]
        self.assertEqual(event_obj, event.backend_obj)
        self.assertLess(initial_event_updated_on, event_obj.updated_on)
        task_objs = models.Task.objects.all().order_by('name')
        self.assertEqual(2, task_objs.count())
        for task_obj, task in zip(task_objs, tasks):
            self.assertEqual(task_obj.name, task.name)
            self.assertEqual(task_obj.queue, task.queue)
Example #2
0
    def test_remove_tasks(self):
        event_obj = models.Event.objects.create(app_name='app_1',
                                                event_name='event')
        initial_event_updated_on = event_obj.updated_on
        current_task_objs = [
            models.Task.objects.create(name='task_1'),
            models.Task.objects.create(name='task_2')
        ]
        event_obj.tasks.set(current_task_objs)

        backend = DjangoDBBackend(registry)
        event = Event.local_instance('app_1', 'event', app=app)
        event.backend_obj = event_obj
        task = Task.local_instance('task_1', app=app)
        task.backend_obj = current_task_objs[0]
        backend.remove_tasks(event, [task])

        event_objs = models.Event.objects.all()
        self.assertEqual(1, event_objs.count())
        event_obj = event_objs[0]
        self.assertEqual(event_obj, event.backend_obj)
        self.assertLess(initial_event_updated_on, event_obj.updated_on)
        task_objs = event_objs.first().tasks.order_by('name')
        self.assertEqual(1, task_objs.count())
        task_obj, task = task_objs.first(), Task.local_instance('task_2',
                                                                app=app)
        self.assertEqual(task_obj.name, task.name)
        self.assertEqual(task_obj.queue, task.queue)
Example #3
0
    def test_update_local_event_no_event_from_backend(self):
        local_event = self.registry.create_local_event('app_1', 'event_1')
        local_event.add_task(Task.remote_instance('task_1', use_routes=False))

        backend = self.backend_cls(self.registry)
        backend.update_local_event(local_event)

        self.assertEqual(1, len(self.registry.events))
        self.assertEqual(local_event, self.registry.events[0])
        self.assertEqual([Task.remote_instance('task_1', use_routes=False)],
                         self.registry.events[0].tasks)
Example #4
0
    def test_ready(self):
        event_1 = registry.create_local_event('app_1', 'event_1')
        event_2 = registry.remote_event('app_2', 'event_2')
        self.mock_module.EVENT_1 = event_1
        self.mock_module.EVENT_2 = event_2
        self.mock_module.get_event_c_tasks = lambda event: [
            self.CTask(event.event_name + '_task')
        ]

        DjangoCeleryEventsConfig.ready(None)
        self.assertEqual([Task('event_1_task', use_routes=False)],
                         event_1.tasks)
        self.assertEqual([Task('event_2_task', use_routes=False)],
                         event_2.tasks)
Example #5
0
    def test_broadcast_async(self, mock_signature):
        broadcaster = Broadcaster(self.app, 'broadcast_queue')
        broadcaster.broadcast_task = mock.Mock()
        broadcaster.broadcast_task.apply_async.side_effect = lambda kwargs, queue: broadcaster.broadcast_func(
            **kwargs)
        event = self.app.registry.create_local_event('app',
                                                     'event',
                                                     kwarg_keys=['a', 'b'])
        event.add_task(Task.local_instance('task', queue='task_queue'))

        broadcaster.broadcast_async(app_name='app',
                                    event_name='event',
                                    a='a',
                                    b='b')

        self.assertEqual([event], self.update_local_event_events)
        broadcaster.broadcast_task.apply_async.assert_called_with(
            kwargs={
                'app_name': 'app',
                'event_name': 'event',
                'a': 'a',
                'b': 'b'
            },
            queue='broadcast_queue')
        mock_signature.assert_called_with('task',
                                          kwargs={
                                              'a': 'a',
                                              'b': 'b'
                                          },
                                          queue='task_queue')
Example #6
0
    def test_sync_remove_events_update_task(self):
        remote_event = self.registry.remote_event('app_3', 'event_3')
        remote_event_from_backend = Event.remote_instance('app_3', 'event_3')
        self.remote_events_from_backend.append(remote_event_from_backend)
        local_task = remote_event.add_task(
            Task.local_instance('app_1.task_1', queue='new_queue'))
        remote_event_from_backend.add_task(
            Task.local_instance('app_1.task_1', queue='old_queue'))

        backend = self.backend_cls(self.registry)
        backend.sync_remote_events()

        self.assertEqual(0, len(self.created_events))
        self.assertEqual(0, len(self.created_tasks))
        self.assertEqual(0, len(self.deleted_events))
        self.assertEqual(0, len(self.removed_tasks))
        self.assertEqual(1, len(self.updated_tasks))
        updated_event, updated_task = self.updated_tasks[0]
        self.assertEqual(remote_event, updated_event)
        self.assertEqual(local_task, updated_task)
        self.assertEqual(local_task.queue, updated_task.queue)
Example #7
0
    def test_sync_local_events_create_event(self):
        local_event = self.registry.create_local_event('app_1', 'event_1')
        local_event.add_task(Task.local_instance('task_1', use_routes=False))

        backend = self.backend_cls(self.registry)
        backend.sync_local_events()

        self.assertEqual(1, len(self.created_events))
        created_event = self.created_events[0]
        self.assertEqual(local_event, created_event)
        self.assertEqual(0, len(self.created_tasks))
        self.assertEqual(0, len(self.deleted_events))
        self.assertEqual(0, len(self.removed_tasks))
        self.assertEqual(0, len(self.updated_tasks))
Example #8
0
    def test_create_events(self):
        events = [
            Event.local_instance('app_1', 'event', app=app),
            Event.local_instance('app_2', 'event', app=app),
        ]
        tasks = [
            Task.local_instance('task_1', app=app),
            Task.local_instance('task_2', app=app)
        ]
        for event in events:
            for task in tasks:
                event.add_task(task)

        backend = DjangoDBBackend(registry)
        backend.create_events(events)

        event_objs = models.Event.objects.order_by('app_name')
        self.assertEqual(2, event_objs.count())
        for event_obj, event in zip(event_objs, events):
            self.assertEqual(event_obj.app_name, event.app_name)
            self.assertEqual(event_obj.event_name, event.event_name)
            self.assertEqual(event_obj, event.backend_obj)
            task_objs = event_obj.tasks.all().order_by('name')
            self.assertEqual(0, task_objs.count())
Example #9
0
    def test_sync_remote_events_create_task(self):
        remote_event = self.registry.remote_event('app_3', 'event_3')
        self.remote_events_from_backend.append(remote_event)
        local_task = remote_event.add_task(
            Task.local_instance('task_1', use_routes=False))

        backend = self.backend_cls(self.registry)
        backend.sync_remote_events()

        self.assertEqual(0, len(self.created_events))
        self.assertEqual(1, len(self.created_tasks))
        event, created_task = self.created_tasks[0]
        self.assertEqual(remote_event, event)
        self.assertEqual(local_task, created_task)
        self.assertEqual(0, len(self.deleted_events))
        self.assertEqual(0, len(self.removed_tasks))
        self.assertEqual(0, len(self.updated_tasks))
Example #10
0
    def _convert_backend_events_to_events(self, backend_events):
        events = []
        for backend_event in backend_events:
            event = Event.local_instance(backend_event.app_name,
                                         backend_event.event_name)
            event.backend_obj = backend_event

            for backend_task in backend_event.tasks.all():
                task = Task.local_instance(backend_task.name,
                                           queue=backend_task.queue,
                                           use_routes=False)
                task.backend_obj = backend_task

                event.add_task(task)

            events.append(event)

        return events
Example #11
0
    def test_broadcast_sync(self, mock_signature):
        broadcaster = Broadcaster(self.app, 'broadcast_queue')
        event = self.app.registry.create_local_event('app',
                                                     'event',
                                                     kwarg_keys=['a', 'b'])
        event.add_task(Task.local_instance('task', queue='task_queue'))

        broadcaster.broadcast_sync(app_name='app',
                                   event_name='event',
                                   a='a',
                                   b='b')

        self.assertEqual([event], self.update_local_event_events)
        mock_signature.assert_called_with('task',
                                          kwargs={
                                              'a': 'a',
                                              'b': 'b'
                                          },
                                          queue='task_queue')
Example #12
0
 def test_no_use_routes(self):
     self.app.routes = [lambda **kwargs: {'queue': 'route_queue'}]
     task = Task.remote_instance('task', use_routes=False, app=self.app)
     self.assertEqual(None, task.queue)
Example #13
0
 def test_use_routes_queue(self):
     self.app.routes = [lambda **kwargs: {'queue': 'route_queue'}]
     task = Task.local_instance('task', app=self.app)
     self.assertEqual('route_queue', task.queue)
Example #14
0
 def test_use_queue_args(self):
     task = Task.local_instance('task', queue='queue', app=self.app)
     self.assertEqual('queue', task.queue)
Example #15
0
 def create_task(self):
     return Task.local_instance('task', app=self.app)