summaryrefslogtreecommitdiffhomepage
path: root/test/unit
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/applications/websockets.py20
-rw-r--r--test/unit/http.py51
-rw-r--r--test/unit/main.py175
3 files changed, 140 insertions, 106 deletions
diff --git a/test/unit/applications/websockets.py b/test/unit/applications/websockets.py
index ef16f433..fc15e8e4 100644
--- a/test/unit/applications/websockets.py
+++ b/test/unit/applications/websockets.py
@@ -52,7 +52,11 @@ class TestApplicationWebsocket(TestApplicationProto):
)
resp = ''
- while select.select([sock], [], [], 30)[0]:
+ while True:
+ rlist = select.select([sock], [], [], 60)[0]
+ if not rlist:
+ self.fail('Can\'t read response from server.')
+
resp += sock.recv(4096).decode()
if (
@@ -70,10 +74,18 @@ class TestApplicationWebsocket(TestApplicationProto):
def serialize_close(self, code=1000, reason=''):
return struct.pack('!H', code) + reason.encode('utf-8')
- def frame_read(self, sock, read_timeout=30):
+ def frame_read(self, sock, read_timeout=60):
def recv_bytes(sock, bytes):
data = b''
- while select.select([sock], [], [], read_timeout)[0]:
+ while True:
+ rlist = select.select([sock], [], [], read_timeout)[0]
+ if not rlist:
+ # For all current cases if the "read_timeout" was changed
+ # than test do not expect to get a response from server.
+ if read_timeout == 60:
+ self.fail('Can\'t read response from server.')
+ break
+
data += sock.recv(bytes - len(data))
if len(data) == bytes:
@@ -221,7 +233,7 @@ class TestApplicationWebsocket(TestApplicationProto):
op_code = self.OP_CONT
pos = end
- def message_read(self, sock, read_timeout=10):
+ def message_read(self, sock, read_timeout=60):
frame = self.frame_read(sock, read_timeout=read_timeout)
while not frame['fin']:
diff --git a/test/unit/http.py b/test/unit/http.py
index 47fb48f1..13384dc8 100644
--- a/test/unit/http.py
+++ b/test/unit/http.py
@@ -17,11 +17,6 @@ class TestHTTP(TestUnit):
port = 7080 if 'port' not in kwargs else kwargs['port']
url = '/' if 'url' not in kwargs else kwargs['url']
http = 'HTTP/1.0' if 'http_10' in kwargs else 'HTTP/1.1'
- read_buffer_size = (
- 4096
- if 'read_buffer_size' not in kwargs
- else kwargs['read_buffer_size']
- )
headers = (
{'Host': 'localhost', 'Connection': 'close'}
@@ -60,7 +55,7 @@ class TestHTTP(TestUnit):
sock.connect(connect_args)
except ConnectionRefusedError:
sock.close()
- return None
+ self.fail('Client can\'t connect to the server.')
else:
sock = kwargs['sock']
@@ -101,12 +96,15 @@ class TestHTTP(TestUnit):
resp = ''
if 'no_recv' not in kwargs:
- read_timeout = (
- 30 if 'read_timeout' not in kwargs else kwargs['read_timeout']
- )
- resp = self.recvall(
- sock, read_timeout=read_timeout, buff_size=read_buffer_size
- ).decode(encoding)
+ recvall_kwargs = {}
+
+ if 'read_timeout' in kwargs:
+ recvall_kwargs['read_timeout'] = kwargs['read_timeout']
+
+ if 'read_buffer_size' in kwargs:
+ recvall_kwargs['buff_size'] = kwargs['read_buffer_size']
+
+ resp = self.recvall(sock, **recvall_kwargs).decode(encoding)
self.log_in(resp)
@@ -174,9 +172,26 @@ class TestHTTP(TestUnit):
def put(self, **kwargs):
return self.http('PUT', **kwargs)
- def recvall(self, sock, read_timeout=30, buff_size=4096):
+ def recvall(self, sock, **kwargs):
+ timeout_default = 60
+
+ timeout = (
+ timeout_default
+ if 'read_timeout' not in kwargs
+ else kwargs['read_timeout']
+ )
+ buff_size = 4096 if 'buff_size' not in kwargs else kwargs['buff_size']
+
data = b''
- while select.select([sock], [], [], read_timeout)[0]:
+ while True:
+ rlist = select.select([sock], [], [], timeout)[0]
+ if not rlist:
+ # For all current cases if the "read_timeout" was changed
+ # than test do not expect to get a response from server.
+ if timeout == timeout_default:
+ self.fail('Can\'t read response from server.')
+ break
+
try:
part = sock.recv(buff_size)
except:
@@ -264,12 +279,8 @@ class TestHTTP(TestUnit):
def _parse_json(self, resp):
headers = resp['headers']
- self.assertIn('Content-Type', headers, 'Content-Type header set')
- self.assertEqual(
- headers['Content-Type'],
- 'application/json',
- 'Content-Type header is application/json',
- )
+ self.assertIn('Content-Type', headers)
+ self.assertEqual(headers['Content-Type'], 'application/json')
resp['body'] = json.loads(resp['body'])
diff --git a/test/unit/main.py b/test/unit/main.py
index 69234dcc..4507f71a 100644
--- a/test/unit/main.py
+++ b/test/unit/main.py
@@ -4,6 +4,7 @@ import sys
import stat
import time
import fcntl
+import atexit
import shutil
import signal
import argparse
@@ -151,48 +152,6 @@ class TestUnit(unittest.TestCase):
def setUp(self):
self._run()
- def tearDown(self):
- self.stop()
-
- # detect errors and failures for current test
-
- def list2reason(exc_list):
- if exc_list and exc_list[-1][0] is self:
- return exc_list[-1][1]
-
- if hasattr(self, '_outcome'):
- result = self.defaultTestResult()
- self._feedErrorsToResult(result, self._outcome.errors)
- else:
- result = getattr(
- self, '_outcomeForDoCleanups', self._resultForDoCleanups
- )
-
- success = not list2reason(result.errors) and not list2reason(
- result.failures
- )
-
- # check unit.log for alerts
-
- unit_log = self.testdir + '/unit.log'
-
- with open(unit_log, 'r', encoding='utf-8', errors='ignore') as f:
- self._check_alerts(f.read())
-
- # remove unit.log
-
- if not TestUnit.save_log and success:
- shutil.rmtree(self.testdir)
-
- else:
- self._print_log()
-
- def stop(self):
- if self._started:
- self._stop()
-
- self.stop_processes()
-
def _run(self):
build_dir = self.pardir + '/build'
self.unitd = build_dir + '/unitd'
@@ -224,62 +183,81 @@ class TestUnit(unittest.TestCase):
stderr=log,
)
+ atexit.register(self.stop)
+
if not self.waitforfiles(self.testdir + '/control.unit.sock'):
exit("Could not start unit")
- self._started = True
-
self.skip_alerts = [
r'read signalfd\(4\) failed',
- r'last message send failed',
r'sendmsg.+failed',
r'recvmsg.+failed',
]
self.skip_sanitizer = False
- def _stop(self):
- with self._p as p:
- p.send_signal(signal.SIGQUIT)
+ def tearDown(self):
+ stop_errs = self.stop()
- try:
- retcode = p.wait(15)
- if retcode:
- self.fail(
- "Child process terminated with code " + str(retcode)
- )
- except:
- self.fail("Could not terminate unit")
- p.kill()
+ # detect errors and failures for current test
- self._started = False
+ def list2reason(exc_list):
+ if exc_list and exc_list[-1][0] is self:
+ return exc_list[-1][1]
- def _check_alerts(self, log):
- found = False
+ if hasattr(self, '_outcome'):
+ result = self.defaultTestResult()
+ self._feedErrorsToResult(result, self._outcome.errors)
+ else:
+ result = getattr(
+ self, '_outcomeForDoCleanups', self._resultForDoCleanups
+ )
- alerts = re.findall('.+\[alert\].+', log)
+ success = not list2reason(result.errors) and not list2reason(
+ result.failures
+ )
- if alerts:
- print('All alerts/sanitizer errors found in log:')
- [print(alert) for alert in alerts]
- found = True
+ # check unit.log for alerts
- if self.skip_alerts:
- for skip in self.skip_alerts:
- alerts = [al for al in alerts if re.search(skip, al) is None]
+ unit_log = self.testdir + '/unit.log'
- if alerts:
- self._print_log(log)
- self.assertFalse(alerts, 'alert(s)')
+ with open(unit_log, 'r', encoding='utf-8', errors='ignore') as f:
+ self._check_alerts(f.read())
- if not self.skip_sanitizer:
- sanitizer_errors = re.findall('.+Sanitizer.+', log)
+ # remove unit.log
- if sanitizer_errors:
- self._print_log(log)
- self.assertFalse(sanitizer_errors, 'sanitizer error(s)')
+ if not TestUnit.save_log and success:
+ shutil.rmtree(self.testdir)
- if found:
- print('skipped.')
+ else:
+ self._print_log()
+
+ self.assertListEqual(stop_errs, [None, None], 'stop errors')
+
+ def stop(self):
+ errors = []
+
+ errors.append(self._stop())
+
+ errors.append(self.stop_processes())
+
+ atexit.unregister(self.stop)
+
+ return errors
+
+ def _stop(self):
+ if self._p.poll() is not None:
+ return
+
+ with self._p as p:
+ p.send_signal(signal.SIGQUIT)
+
+ try:
+ retcode = p.wait(15)
+ if retcode:
+ return 'Child process terminated with code ' + str(retcode)
+ except:
+ p.kill()
+ return 'Could not terminate unit'
def run_process(self, target, *args):
if not hasattr(self, '_processes'):
@@ -294,12 +272,17 @@ class TestUnit(unittest.TestCase):
if not hasattr(self, '_processes'):
return
+ fail = False
for process in self._processes:
- process.terminate()
- process.join(timeout=5)
-
if process.is_alive():
- self.fail('Fail to stop process')
+ process.terminate()
+ process.join(timeout=15)
+
+ if process.is_alive():
+ fail = True
+
+ if fail:
+ return 'Fail to stop process'
def waitforfiles(self, *files):
for i in range(50):
@@ -329,6 +312,34 @@ class TestUnit(unittest.TestCase):
for f in files:
os.chmod(os.path.join(root, f), 0o777)
+ def _check_alerts(self, log):
+ found = False
+
+ alerts = re.findall('.+\[alert\].+', log)
+
+ if alerts:
+ print('All alerts/sanitizer errors found in log:')
+ [print(alert) for alert in alerts]
+ found = True
+
+ if self.skip_alerts:
+ for skip in self.skip_alerts:
+ alerts = [al for al in alerts if re.search(skip, al) is None]
+
+ if alerts:
+ self._print_log(log)
+ self.assertFalse(alerts, 'alert(s)')
+
+ if not self.skip_sanitizer:
+ sanitizer_errors = re.findall('.+Sanitizer.+', log)
+
+ if sanitizer_errors:
+ self._print_log(log)
+ self.assertFalse(sanitizer_errors, 'sanitizer error(s)')
+
+ if found:
+ print('skipped.')
+
@staticmethod
def _parse_args():
parser = argparse.ArgumentParser(add_help=False)