def test_restore_in_progress_form_submitted_kills_old_jobs(self): """If the user submits a form somehow while a job is running, the job should be terminated """ last_sync_token = '0a72d5a3c2ec53e85c1af27ee5717e0d' device_id = 'RSMCHBA8PJNQIGMONN2JZT6E' async_restore_task_id_cache = AsyncRestoreTaskIdCache( domain=self.domain, user_id=self.user.user_id, sync_log_id=last_sync_token, device_id=device_id, ) restore_payload_path_cache = RestorePayloadPathCache( domain=self.domain, user_id=self.user.user_id, device_id=device_id, sync_log_id=last_sync_token, ) async_restore_task_id = '0edecc20d89d6f4a09f2e992c0c24b5f' initial_sync_path = 'path/to/payload' restore_config = self._restore_config(async=True) # pretend we have a task running async_restore_task_id_cache.set_value(async_restore_task_id) restore_payload_path_cache.set_value(initial_sync_path) def submit_form(user_id, device_id, last_sync_token): form = """ <data xmlns="http://openrosa.org/formdesigner/blah"> <meta> <userID>{user_id}</userID> <deviceID>{device_id}</deviceID> </meta> </data> """ submit_form_locally( form.format(user_id=user_id, device_id=device_id), self.domain, last_sync_token=last_sync_token, ) with mock.patch( 'corehq.form_processor.submission_post.revoke_celery_task' ) as revoke: # with a different user in the same domain, task doesn't get killed submit_form(user_id="other_user", device_id='OTHERDEVICEID', last_sync_token='othersynctoken') self.assertFalse(revoke.called) self.assertEqual(async_restore_task_id_cache.get_value(), async_restore_task_id) self.assertEqual(restore_payload_path_cache.get_value(), initial_sync_path) # task gets killed when the user submits a form submit_form(user_id=self.user.user_id, device_id=device_id, last_sync_token=last_sync_token) revoke.assert_called_with(async_restore_task_id) self.assertIsNone(async_restore_task_id_cache.get_value()) self.assertIsNone(restore_payload_path_cache.get_value())
def test_restore_in_progress_form_submitted_kills_old_jobs(self): """If the user submits a form somehow while a job is running, the job should be terminated """ last_sync_token = '0a72d5a3c2ec53e85c1af27ee5717e0d' device_id = 'RSMCHBA8PJNQIGMONN2JZT6E' async_restore_task_id_cache = AsyncRestoreTaskIdCache( domain=self.domain, user_id=self.user.user_id, sync_log_id=last_sync_token, device_id=device_id, ) restore_payload_path_cache = RestorePayloadPathCache( domain=self.domain, user_id=self.user.user_id, device_id=device_id, sync_log_id=last_sync_token, ) async_restore_task_id = '0edecc20d89d6f4a09f2e992c0c24b5f' initial_sync_path = 'path/to/payload' restore_config = self._restore_config(async=True) # pretend we have a task running async_restore_task_id_cache.set_value(async_restore_task_id) restore_payload_path_cache.set_value(initial_sync_path) def submit_form(user_id, device_id, last_sync_token): form = """ <data xmlns="http://openrosa.org/formdesigner/blah"> <meta> <userID>{user_id}</userID> <deviceID>{device_id}</deviceID> </meta> </data> """ submit_form_locally( form.format(user_id=user_id, device_id=device_id), self.domain, last_sync_token=last_sync_token, ) with mock.patch('corehq.form_processor.submission_post.revoke_celery_task') as revoke: # with a different user in the same domain, task doesn't get killed submit_form(user_id="other_user", device_id='OTHERDEVICEID', last_sync_token='othersynctoken') self.assertFalse(revoke.called) self.assertEqual(async_restore_task_id_cache.get_value(), async_restore_task_id) self.assertEqual(restore_payload_path_cache.get_value(), initial_sync_path) # task gets killed when the user submits a form submit_form(user_id=self.user.user_id, device_id=device_id, last_sync_token=last_sync_token) revoke.assert_called_with(async_restore_task_id) self.assertIsNone(async_restore_task_id_cache.get_value()) self.assertIsNone(restore_payload_path_cache.get_value())
def test_completed_task_deletes_cache(self): async_restore_task_id_cache = AsyncRestoreTaskIdCache( domain=self.domain, user_id=self.user.user_id, sync_log_id=None, device_id=None, ) restore_config = self._restore_config(async=True) async_restore_task_id_cache.set_value('im going to be deleted by the next command') restore_config.timing_context.start() restore_config.timing_context("wait_for_task_to_start").start() get_async_restore_payload.delay(restore_config) self.assertTrue(restore_config.timing_context.is_finished()) self.assertIsNone(async_restore_task_id_cache.get_value())