def test_id(self): htq.send(url, data='v1', id='foo') htq.send(url, data='v2', id='foo') # First request is canceled uuid = htq.pop() req1 = htq.request(uuid) self.assertEqual(req1['status'], htq.CANCELED) # Second is queued uuid = htq.pop() req2 = htq.request(uuid) self.assertEqual(req2['status'], htq.QUEUED)
def test_response(self): resp = app.post('/', data=json.dumps({ 'url': url, }), headers={'content-type': 'application/json'}) location = resp.location # Receive response.. htq.receive(htq.pop()) resp = app.get(location) self.assertIn('response', resp.headers['Link']) links = parse_header_links(resp.headers['Link']) response_url = links['response'] resp = app.get(response_url) self.assertEqual(resp.status_code, 200) resp = app.delete(response_url) self.assertEqual(resp.status_code, 204) resp = app.delete(response_url) self.assertEqual(resp.status_code, 404)
def test_post(self): htq.send(url, 'post', data='{"foo": 1}', headers={ 'Content-Type': 'application/json', }) uuid = htq.pop() resp = htq.receive(uuid) self.assertEqual(resp['status'], htq.SUCCESS)
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_error(self): htq.send('http://localhost:9999') uuid = htq.pop() resp = htq.receive(uuid) req = htq.request(uuid) self.assertEqual(resp['status'], htq.ERROR) self.assertEqual(req['status'], htq.ERROR)
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 run(n): responses.add(responses.GET, url=re.compile(r'http://localhost/\d+'), body='{"ok": 1}', status=200, content_type='application/json') t0 = time.time() for i in range(n): htq.send('http://localhost/' + str(i)) print('sent {} in {}'.format(n, time.time() - t0)) t0 = time.time() for i in range(n): uuid = htq.pop() htq.receive(uuid) print('received {} in {}'.format(n, time.time() - t0))
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', })
def test_status(self): htq.send(url) uuid = htq.pop() self.assertEqual(htq.status(uuid), htq.QUEUED)