refactor: removes deprecated calls to runtime.xqueue from code and tests
* Uses the XQueueService object from the ModuleSystem in the LoncapaSystem instead of the dict of xqueue-related values. * Adds a StubXQueueService for testing.
This commit is contained in:
committed by
Piotr Surowiec
parent
1974bacadd
commit
31827bc3b1
@@ -994,9 +994,9 @@ class MatlabInput(CodeInput):
|
||||
response = data['submission']
|
||||
|
||||
# construct xqueue headers
|
||||
qinterface = self.capa_system.xqueue['interface']
|
||||
qinterface = self.capa_system.xqueue.interface
|
||||
qtime = datetime.utcnow().strftime(xqueue_interface.dateformat)
|
||||
callback_url = self.capa_system.xqueue['construct_callback']('ungraded_response')
|
||||
callback_url = self.capa_system.xqueue.construct_callback('ungraded_response')
|
||||
anonymous_student_id = self.capa_system.anonymous_student_id
|
||||
# TODO: Why is this using self.capa_system.seed when we have self.seed???
|
||||
queuekey = xqueue_interface.make_hashkey(str(self.capa_system.seed) + qtime +
|
||||
|
||||
@@ -2586,14 +2586,14 @@ class CodeResponse(LoncapaResponse):
|
||||
"""
|
||||
Grade student code using an external queueing server, called 'xqueue'.
|
||||
|
||||
Expects 'xqueue' dict in LoncapaSystem with the following keys that are
|
||||
Expects 'xqueue' dict in LoncapaSystem with the following properties that are
|
||||
needed by CodeResponse::
|
||||
|
||||
capa_system.xqueue = {
|
||||
'interface': XQueueInterface object.
|
||||
'construct_callback': Per-StudentModule callback URL constructor,
|
||||
capa_system.xqueue = object with properties:
|
||||
interface: XQueueInterface object.
|
||||
construct_callback: Per-StudentModule callback URL constructor,
|
||||
defaults to using 'score_update' as the correct dispatch (function).
|
||||
'default_queuename': Default queue name to submit request (string).
|
||||
default_queuename: Default queue name to submit request (string).
|
||||
}
|
||||
|
||||
External requests are only submitted for student submission grading, not
|
||||
@@ -2623,7 +2623,7 @@ class CodeResponse(LoncapaResponse):
|
||||
|
||||
# We do not support xqueue within Studio.
|
||||
if self.capa_system.xqueue is not None:
|
||||
default_queuename = self.capa_system.xqueue['default_queuename']
|
||||
default_queuename = self.capa_system.xqueue.default_queuename
|
||||
else:
|
||||
default_queuename = None
|
||||
self.queue_name = xml.get('queuename', default_queuename)
|
||||
@@ -2684,7 +2684,7 @@ class CodeResponse(LoncapaResponse):
|
||||
# Prepare xqueue request
|
||||
#------------------------------------------------------------
|
||||
|
||||
qinterface = self.capa_system.xqueue['interface']
|
||||
qinterface = self.capa_system.xqueue.interface
|
||||
qtime = datetime.strftime(datetime.now(UTC), xqueue_interface.dateformat)
|
||||
|
||||
anonymous_student_id = self.capa_system.anonymous_student_id
|
||||
@@ -2693,7 +2693,7 @@ class CodeResponse(LoncapaResponse):
|
||||
queuekey = xqueue_interface.make_hashkey(
|
||||
str(self.capa_system.seed) + qtime + anonymous_student_id + self.answer_id
|
||||
)
|
||||
callback_url = self.capa_system.xqueue['construct_callback']()
|
||||
callback_url = self.capa_system.xqueue.construct_callback()
|
||||
xheader = xqueue_interface.make_xheader(
|
||||
lms_callback_url=callback_url,
|
||||
lms_key=queuekey,
|
||||
|
||||
@@ -44,12 +44,19 @@ def tst_render_template(template, context): # pylint: disable=unused-argument
|
||||
return '<div>{0}</div>'.format(saxutils.escape(repr(context)))
|
||||
|
||||
|
||||
def calledback_url(dispatch='score_update'):
|
||||
"""A callback url method to use in tests."""
|
||||
return dispatch
|
||||
class StubXQueueService:
|
||||
"""
|
||||
Stubs out the XQueueService for Capa problem tests.
|
||||
"""
|
||||
def __init__(self):
|
||||
self.interface = MagicMock()
|
||||
self.interface.send_to_queue.return_value = (0, 'Success!')
|
||||
self.default_queuename = 'testqueue'
|
||||
self.waittime = 10
|
||||
|
||||
xqueue_interface = MagicMock() # pylint: disable=invalid-name
|
||||
xqueue_interface.send_to_queue.return_value = (0, 'Success!')
|
||||
def construct_callback(self, dispatch='score_update'):
|
||||
"""A callback url method to use in tests."""
|
||||
return dispatch
|
||||
|
||||
|
||||
def test_capa_system(render_template=None):
|
||||
@@ -72,12 +79,7 @@ def test_capa_system(render_template=None):
|
||||
seed=0,
|
||||
STATIC_URL='/dummy-static/',
|
||||
STATUS_CLASS=Status,
|
||||
xqueue={
|
||||
'interface': xqueue_interface,
|
||||
'construct_callback': calledback_url,
|
||||
'default_queuename': 'testqueue',
|
||||
'waittime': 10
|
||||
},
|
||||
xqueue=StubXQueueService(),
|
||||
)
|
||||
return the_system
|
||||
|
||||
|
||||
@@ -643,9 +643,7 @@ class MatlabTest(unittest.TestCase):
|
||||
def test_plot_data(self):
|
||||
data = {'submission': 'x = 1234;'}
|
||||
response = self.the_input.handle_ajax("plot", data)
|
||||
|
||||
test_capa_system().xqueue['interface'].send_to_queue.assert_called_with(header=ANY, body=ANY)
|
||||
|
||||
self.the_input.capa_system.xqueue.interface.send_to_queue.assert_called_with(header=ANY, body=ANY)
|
||||
assert response['success']
|
||||
assert self.the_input.input_state['queuekey'] is not None
|
||||
assert self.the_input.input_state['queuestate'] == 'queued'
|
||||
@@ -653,7 +651,7 @@ class MatlabTest(unittest.TestCase):
|
||||
def test_plot_data_failure(self):
|
||||
data = {'submission': 'x = 1234;'}
|
||||
error_message = 'Error message!'
|
||||
test_capa_system().xqueue['interface'].send_to_queue.return_value = (1, error_message)
|
||||
self.the_input.capa_system.xqueue.interface.send_to_queue.return_value = (1, error_message)
|
||||
response = self.the_input.handle_ajax("plot", data)
|
||||
assert not response['success']
|
||||
assert response['message'] == error_message
|
||||
@@ -740,7 +738,7 @@ class MatlabTest(unittest.TestCase):
|
||||
data = {'submission': 'x = 1234;'}
|
||||
response = the_input.handle_ajax("plot", data) # lint-amnesty, pylint: disable=unused-variable
|
||||
|
||||
body = system.xqueue['interface'].send_to_queue.call_args[1]['body']
|
||||
body = system.xqueue.interface.send_to_queue.call_args[1]['body']
|
||||
payload = json.loads(body)
|
||||
assert 'test_api_key' == payload['token']
|
||||
assert '2' == payload['endpoint_version']
|
||||
|
||||
@@ -827,7 +827,7 @@ class ProblemBlock(
|
||||
render_template=self.runtime.service(self, 'mako').render_template,
|
||||
seed=seed, # Why do we do this if we have self.seed?
|
||||
STATIC_URL=self.runtime.STATIC_URL,
|
||||
xqueue=self.runtime.xqueue,
|
||||
xqueue=self.runtime.service(self, 'xqueue'),
|
||||
matlab_api_key=self.matlab_api_key
|
||||
)
|
||||
|
||||
@@ -1746,7 +1746,8 @@ class ProblemBlock(
|
||||
if self.lcp.is_queued():
|
||||
prev_submit_time = self.lcp.get_recentmost_queuetime()
|
||||
|
||||
waittime_between_requests = self.runtime.xqueue['waittime']
|
||||
xqueue_service = self.runtime.service(self, 'xqueue')
|
||||
waittime_between_requests = xqueue_service.waittime if xqueue_service else 0
|
||||
if (current_time - prev_submit_time).total_seconds() < waittime_between_requests:
|
||||
msg = _("You must wait at least {wait} seconds between submissions.").format(
|
||||
wait=waittime_between_requests)
|
||||
|
||||
Reference in New Issue
Block a user