Skip to content
Snippets Groups Projects
Commit d7065db0 authored by ale's avatar ale
Browse files

Add a test for the structured error handling

parent 05e0e58b
No related branches found
No related tags found
1 merge request!1Refactor rpc library
......@@ -36,7 +36,6 @@ def generate_ca():
.add_extension(x509.BasicConstraints(ca=True, path_length=None),
critical=True)\
.sign(private_key=private_key, algorithm=hashes.SHA256())
logging.info('generated CA cert (serial=%s)', certificate.serial_number)
return certificate, private_key
......@@ -60,8 +59,6 @@ def generate_cert(ca_cert, ca_key, cn):
.add_extension(x509.BasicConstraints(ca=False, path_length=None),
critical=True)\
.sign(private_key=ca_key, algorithm=hashes.SHA256())
logging.info('generated cert for %s (serial=%s)',
cn, certificate.serial_number)
return certificate, private_key
......@@ -198,8 +195,6 @@ class RPCTestCase(TestCase):
def _save(name, data):
with open(os.path.join(self.tmpdir, name), 'wb') as f:
f.write(data)
logging.info('wrote cert to %s:\n%s',
os.path.join(self.tmpdir, name), data)
_save('ca.pem', ca_cert.public_bytes(serialization.Encoding.PEM))
_save('client.pem', client_cert.public_bytes(serialization.Encoding.PEM))
......@@ -257,7 +252,7 @@ class RPCTestCase(TestCase):
num_requests=DEFAULT_NUM_REQUESTS):
# Assert that the requests have a 'decent' distribution over
# the available backends.
threshold = num_requests / (2 * self.NUM_SERVERS)
threshold = num_requests / (4 * self.NUM_SERVERS)
for key in range(self.NUM_SERVERS):
self.assertGreater(
self.request_counters.get(key), threshold,
......
from ai_web_common.rpc.test import RPCTestCase, BASE_FREE_PORT
from ai_web_common.rpc.core import StatusError
class TestRPC(RPCTestCase):
......@@ -42,3 +43,23 @@ class TestOverload(RPCTestCase):
num_requests=5)
self.assertEqual(0, errors)
self.assertEqual(5, self.request_counters.get(0))
class TestErrors(RPCTestCase):
NUM_SERVERS = 1
SERVER_STATUS = 400
SERVER_RESPONSE = {
'field1': 'error1',
'field2': 'error2',
}
def test_structured_error(self):
client = self.client()
with self.assertRaises(StatusError) as exc:
client.test_method(
test_arg='42',
override_target_list=self.targets())
self.assertEqual(self.SERVER_RESPONSE, exc.exception.errors,
'StatusError.error does not match expectation')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment