コード例 #1
0
    def test_task_handler_will_create_the_correct_post_request(self) -> None:
        queue_name = 'dummy_queue'
        dummy_url = '/dummy_handler'
        correct_port = dev_mode_taskqueue_services.GOOGLE_APP_ENGINE_PORT
        correct_payload = {
            'fn_identifier':
            (taskqueue_services.FUNCTION_ID_DELETE_EXPS_FROM_USER_MODELS),
            'args': [['1', '2', '3']],
            'kwargs': {}
        }

        task_name = 'task1'
        correct_headers = {
            'X-Appengine-QueueName': queue_name,
            'X-Appengine-TaskName': task_name,
            'X-Appengine-TaskRetryCount': '0',
            'X-Appengine-TaskExecutionCount': '0',
            'X-Appengine-TaskETA': '0',
            'X-AppEngine-Fake-Is-Admin': '1',
            'method': 'POST'
        }

        # In the type annotation below, we have used Dict[str, Any] for JSON.
        # This is because this function mocks requests.post function where the
        # type of JSON has been defined Any, hence using Dict[str, Any] here.
        # https://github.com/python/typeshed/blob/5e0fc4607323a4657b587bf70e3c26becf1c88d0/stubs/requests/requests/api.pyi#L78
        def mock_post(url: str, json: Dict[str, Any], headers: Dict[str, str],
                      timeout: int) -> None:
            self.assertEqual(
                url, 'http://localhost:%s%s' % (correct_port, dummy_url))
            self.assertEqual(json, correct_payload)
            self.assertEqual(headers, correct_headers)
            self.assertEqual(timeout, feconf.DEFAULT_TASKQUEUE_TIMEOUT_SECONDS)

        swap_post = self.swap(requests, 'post', mock_post)
        with swap_post:
            # I have to test _task_handler by calling it because I cannot
            # surround this task handler in a context manager reliably. The
            # task_handler is called by a queue thread that is instantiated by
            # the Cloud Tasks Emulator which has a non-determistic execution
            # time. Creating a task will execute correctly but the program will
            # exit the context before actually calling _task_handler().
            dev_mode_taskqueue_services._task_handler(  # pylint: disable=protected-access
                dummy_url,
                correct_payload,
                queue_name,
                task_name=task_name)
コード例 #2
0
    def test_task_handler_will_create_the_correct_post_request(self):
        queue_name = 'dummy_queue'
        dummy_url = '/dummy_handler'
        correct_port = dev_mode_taskqueue_services.GOOGLE_APP_ENGINE_PORT
        correct_payload = {
            'fn_identifier':
            (taskqueue_services.FUNCTION_ID_DELETE_EXPS_FROM_USER_MODELS),
            'args': [['1', '2', '3']],
            'kwargs': {}
        }

        task_name = 'task1'
        correct_headers = {
            'X-Appengine-QueueName': queue_name,
            'X-Appengine-TaskName': task_name,
            'X-Appengine-TaskRetryCount': '0',
            'X-Appengine-TaskExecutionCount': '0',
            'X-Appengine-TaskETA': '0',
            'X-AppEngine-Fake-Is-Admin': '1',
            'method': 'POST'
        }

        def mock_post(url, json, headers, timeout):
            self.assertEqual(
                url, 'http://localhost:%s%s' % (correct_port, dummy_url))
            self.assertEqual(json, correct_payload)
            self.assertEqual(headers, correct_headers)
            self.assertEqual(timeout, feconf.DEFAULT_TASKQUEUE_TIMEOUT_SECONDS)

        swap_post = self.swap(requests, 'post', mock_post)
        with swap_post:
            # I have to test _task_handler by calling it because I cannot
            # surround this task handler in a context manager reliably. The
            # task_handler is called by a queue thread that is instantiated by
            # the Cloud Tasks Emulator which has a non-determistic execution
            # time. Creating a task will execute correctly but the program will
            # exit the context before actually calling _task_handler().
            dev_mode_taskqueue_services._task_handler(  # pylint: disable=protected-access
                dummy_url,
                correct_payload,
                queue_name,
                task_name=task_name)