def response(uuid): status = htq.status(uuid) if not status: abort(404) # Block until ready while True: # Poll status until it is complete if status not in {htq.QUEUED, htq.PENDING}: break time.sleep(0.1) status = htq.status(uuid) rp = htq.response(uuid) or {} resp = make_response(json.dumps(rp), 200) resp.headers['Content-Type'] = 'application/json' resp.headers['Link'] = build_link_header({ url_for('response', uuid=uuid, _external=True): { 'rel': 'self', }, url_for('request', uuid=uuid, _external=True): { 'rel': 'request', }, }) return resp
def test_purge(self): htq.send(url) uuid = htq.pop() htq.receive(uuid) htq.purge(uuid) resp = htq.response(uuid) self.assertIsNone(resp)
def test_cancel_complete(self): htq.send(url) uuid = htq.pop() htq.receive(uuid) # Cancel will change the state and delete the response htq.cancel(uuid) req = htq.request(uuid) resp = htq.response(uuid) self.assertEqual(req['status'], htq.CANCELED) self.assertIsNone(resp)
def test_cancel_queued(self): htq.send(url) uuid = htq.pop() # Cancel while queued htq.cancel(uuid) # Receive has not effect htq.receive(uuid) req = htq.request(uuid) resp = htq.response(uuid) self.assertEqual(req['status'], htq.CANCELED) self.assertIsNone(resp)
def test_send_receive(self): # Send (queue) a request htq.send(url) self.assertEqual(htq.size(), 1) # Pop off UUID for receiving uuid = htq.pop() self.assertEqual(htq.size(), 0) # Actually send request/get response htq.receive(uuid) # Check states req = htq.request(uuid) resp = htq.response(uuid) self.assertEqual(req['status'], htq.SUCCESS) self.assertEqual(resp['code'], 200) self.assertEqual(resp['data'], '{"ok": 1}') self.assertEqual(resp['headers'], { 'Content-Type': 'application/json', })