Commit d117222f by uzairr

Add timeout in request to xqueue

parent 3c040b93
......@@ -15,6 +15,8 @@ XQUEUE_METRIC_NAME = 'edxapp.xqueue'
# Wait time for response from Xqueue.
XQUEUE_TIMEOUT = 35 # seconds
CONNECT_TIMEOUT = 3.05 # seconds
READ_TIMEOUT = 10 # seconds
def make_hashkey(seed):
......@@ -134,12 +136,18 @@ class XQueueInterface(object):
def _http_post(self, url, data, files=None):
try:
r = self.session.post(url, data=data, files=files)
response = self.session.post(
url, data=data, files=files, timeout=(CONNECT_TIMEOUT, READ_TIMEOUT)
)
except requests.exceptions.ConnectionError, err:
log.error(err)
return (1, 'cannot connect to server')
if r.status_code not in [200]:
return (1, 'unexpected HTTP status code [%d]' % r.status_code)
except requests.exceptions.ReadTimeout, err:
log.error(err)
return (1, 'failed to read from the server')
if response.status_code not in [200]:
return (1, 'unexpected HTTP status code [%d]' % response.status_code)
return parse_xreply(r.text)
return parse_xreply(response.text)
......@@ -8,6 +8,7 @@ Tests of the Capa XModule
import datetime
import json
import random
import requests
import os
import textwrap
import unittest
......@@ -242,6 +243,22 @@ class CapaModuleTest(unittest.TestCase):
problem = CapaFactory.create()
self.assertFalse(problem.answer_available())
@ddt.data(
(requests.exceptions.ReadTimeout, (1, 'failed to read from the server')),
(requests.exceptions.ConnectionError, (1, 'cannot connect to server')),
)
@ddt.unpack
def test_xqueue_request_exception(self, exception, result):
"""
Makes sure that platform will raise appropriate exception in case of
connect/read timeout(s) to request to xqueue
"""
xqueue_interface = XQueueInterface("http://example.com/xqueue", Mock())
with patch.object(xqueue_interface.session, 'post', side_effect=exception):
# pylint: disable = protected-access
response = xqueue_interface._http_post('http://some/fake/url', {})
self.assertEqual(response, result)
def test_showanswer_attempted(self):
problem = CapaFactory.create(showanswer='attempted')
self.assertFalse(problem.answer_available())
......
......@@ -818,7 +818,7 @@ class ProblemWithUploadedFilesTest(TestSubmittingProblems):
self.assertEqual(name, "post")
self.assertEqual(len(args), 1)
self.assertTrue(args[0].endswith("/submit/"))
self.assertItemsEqual(kwargs.keys(), ["files", "data"])
self.assertItemsEqual(kwargs.keys(), ["files", "data", "timeout"])
self.assertItemsEqual(kwargs['files'].keys(), filenames.split())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment