Skip to content

Commit

Permalink
Handle HTTP errors for API version request and data sending
Browse files Browse the repository at this point in the history
- add testing with HTTP 400 error code

Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Mar 22, 2024
1 parent bcb0a60 commit 93101e0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
12 changes: 8 additions & 4 deletions rebench/rebenchdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ def _send_payload(payload, url):
def _get_api_version(self):
url = self._server_base_url + '/results'
req = HttpRequest(url, method='OPTIONS')
with urlopen(req) as socket:
response = socket.read()
return socket.getheader('X-ReBenchDB-Result-API-Version')
try:
with urlopen(req) as socket:
response = socket.read()
return socket.getheader('X-ReBenchDB-Result-API-Version')
except:
# some error, so no API version available
return None

def convert_data_to_json(self, data):
return json.dumps(data, separators=(',', ':'), ensure_ascii=True)
Expand Down Expand Up @@ -116,7 +120,7 @@ def _send_with_retries(self, payload_bytes, url):
+ "{ind}{ind}" + str(te) + "\n")
return False, None
except (IOError, HTTPException) as error:
if attempts > 0:
if error.status != 400 and attempts > 0:
# let's retry, the benchmark server might just time out, as usual
# but let it breath a little
self.ui.warning(
Expand Down
17 changes: 9 additions & 8 deletions rebench/tests/mock_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

class _RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_response(self.server.status_code)
self.end_headers()
self.send_header("Content-Length", 0)
self.server.get_requests += 1

def do_PUT(self):
self.send_response(200)
self.send_response(self.server.status_code)
self.send_header("Content-Length", 0)
self.end_headers()
self.server.put_requests += 1

def do_OPTIONS(self):
self.send_response(200)
self.send_response(self.server.status_code)
if self.server.api_v2:
self.send_header("X-ReBenchDB-Result-API-Version", "2.0.0")
self.send_header("Allow", "PUT")
Expand All @@ -38,19 +38,18 @@ def __init__(self, *args, **kwargs):
self.get_requests = 0
self.options_requests = 0
self.api_v2 = None

def set_api_v2(self, value):
self.api_v2 = value
self.status_code = 200


class MockHTTPServer(object):

def __init__(self, api_v2 = True):
def __init__(self, api_v2 = True, test_error_handling = False):
self._port = -1
self._server = None
self._thread = None
self._is_shutdown = False
self.api_v2 = api_v2
self._test_error_handling = test_error_handling

def get_free_port(self):
s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
Expand All @@ -63,7 +62,9 @@ def get_free_port(self):

def start(self):
self._server = HTTPServerWithCounter(('localhost', self._port), _RequestHandler)
self._server.set_api_v2(self.api_v2)
self._server.api_v2 = self.api_v2
if self._test_error_handling:
self._server.status_code = 400

self._thread = Thread(target=self._server.serve_forever)
self._thread.daemon = True
Expand Down
18 changes: 17 additions & 1 deletion rebench/tests/persistency_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_data_discarding(self):

@skipIf(git_not_available() or git_repo_not_initialized(),
"git source info not available, but needed for reporting to ReBenchDB")
def test_rebench_db(self):
def test_rebenchdb(self):
option_parser = ReBench().shell_options()
cmd_config = option_parser.parse_args(['--experiment=Test', 'persistency.conf'])

Expand All @@ -137,6 +137,22 @@ def test_rebench_db(self):
finally:
server.process_and_shutdown()

@skipIf(git_not_available() or git_repo_not_initialized(),
"git source info not available, but needed for reporting to ReBenchDB")
def test_rebenchdb_400_error(self):
option_parser = ReBench().shell_options()
cmd_config = option_parser.parse_args(['--experiment=Test', 'persistency.conf'])

server = MockHTTPServer(test_error_handling=True)

try:
self._exec_rebench_db(cmd_config, server)
server.process_and_shutdown()

self.assertEqual(1, server.get_number_of_put_requests())
finally:
server.process_and_shutdown()

def test_disabled_rebench_db(self):
option_parser = ReBench().shell_options()
cmd_config = option_parser.parse_args(['--experiment=Test', '-R', 'persistency.conf'])
Expand Down

0 comments on commit 93101e0

Please sign in to comment.