def test_subsequent_syncs_when_job_complete(self): # First sync, return a timout. Ensure that the async_task_id gets set async_restore_task_id_cache = AsyncRestoreTaskIdCache( domain=self.domain, user_id=self.user.user_id, sync_log_id=None, device_id=None, ) with mock.patch('casexml.apps.phone.restore.get_async_restore_payload') as task: delay = mock.MagicMock() delay.id = 'random_task_id' delay.get = mock.MagicMock(side_effect=TimeoutError()) # task not finished task.delay.return_value = delay restore_config = self._restore_config(async=True) initial_payload = restore_config.get_payload() self.assertIsNotNone(async_restore_task_id_cache.get_value()) self.assertIsInstance(initial_payload, AsyncRestoreResponse) # new synclog should not have been created self.assertIsNone(restore_config.restore_state.current_sync_log) # Second sync, don't timeout (can't use AsyncResult in tests, so mock # the return value). restore_response = mock.MagicMock(return_value=RestoreResponse(None)) with mock.patch.object(AsyncResult, 'get', restore_response) as get_result: with mock.patch.object(AsyncResult, 'status', ASYNC_RESTORE_SENT): subsequent_restore = self._restore_config(async=True) self.assertIsNotNone(async_restore_task_id_cache.get_value()) subsequent_restore.get_payload() # if the task actually ran, the cache should now not have the task id, # however, the task is not run in this test. See `test_completed_task_deletes_cache` # self.assertIsNone(restore_config.cache.get(cache_id)) get_result.assert_called_with(timeout=1)
def _invalidate_async_restore_task_id_cache(self, xform, device_id): async_restore_task_id_cache = AsyncRestoreTaskIdCache( domain=self.domain, user_id=xform.user_id, sync_log_id=self.last_sync_token, device_id=device_id, ) task_id = async_restore_task_id_cache.get_value() if task_id is not None: revoke_celery_task(task_id) async_restore_task_id_cache.invalidate()
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())
def async_restore_task_id_cache(self): return AsyncRestoreTaskIdCache( domain=self.domain, user_id=self.restore_user.user_id, sync_log_id=self.sync_log._id if self.sync_log else '', device_id=self.params.device_id, )
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())