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)
def test_create_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_obj = models.Task.objects.create(name='task_0') event_obj.tasks.add(current_task_obj) backend = DjangoDBBackend(registry) event = Event.local_instance('app_1', 'event', app=app) event.backend_obj = event_obj tasks = [ Task.local_instance('task_1', app=app), Task.local_instance('task_2', queue='queue_2', app=app) ] backend.create_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 = event_objs.first().tasks.order_by('name') self.assertEqual(3, task_objs.count()) for task_obj, task in zip(task_objs[1:], tasks): self.assertEqual(task_obj.name, task.name) self.assertEqual(task_obj.queue, task.queue) self.assertEqual(task_obj, task.backend_obj)
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')
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)
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))
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())
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))
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
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')
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)
def test_use_queue_args(self): task = Task.local_instance('task', queue='queue', app=self.app) self.assertEqual('queue', task.queue)
def create_task(self): return Task.local_instance('task', app=self.app)