From faf4ad54785fa418b3c218ab0d2d2016c55d169a Mon Sep 17 00:00:00 2001 From: Andrey Zelenkov Date: Tue, 8 Oct 2019 00:28:40 +0300 Subject: Tests: increased subprocess timeout on Unit exit. Also changed order of subprocess status checks. _terminate_process() method removed. --- test/unit/main.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'test/unit') diff --git a/test/unit/main.py b/test/unit/main.py index 873f1815..f5727726 100644 --- a/test/unit/main.py +++ b/test/unit/main.py @@ -240,24 +240,24 @@ class TestUnit(unittest.TestCase): break time.sleep(0.1) - if os.path.exists(self.testdir + '/unit.pid'): - exit("Could not terminate unit") + self._p.join(timeout=5) - self._started = False + if self._p.is_alive(): + self._p.terminate() + self._p.join(timeout=5) - self._p.join(timeout=1) - self._terminate_process(self._p) + if self._p.is_alive(): + self.fail("Could not terminate process " + str(self._p.pid)) - def _terminate_process(self, process): - if process.is_alive(): - process.terminate() - process.join(timeout=5) + if os.path.exists(self.testdir + '/unit.pid'): + self.fail("Could not terminate unit") - if process.is_alive(): - exit("Could not terminate process " + process.pid) + self._started = False - if process.exitcode: - exit("Child process terminated with code " + str(process.exitcode)) + if self._p.exitcode: + self.fail( + "Child process terminated with code " + str(self._p.exitcode) + ) def _check_alerts(self, log): found = False -- cgit From 47436e9be5f41898ba873064fb358d0fe2decd06 Mon Sep 17 00:00:00 2001 From: Andrey Zelenkov Date: Wed, 23 Oct 2019 16:26:06 +0300 Subject: Tests: improved handshake for websocket tests. --- test/unit/applications/websockets.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'test/unit') diff --git a/test/unit/applications/websockets.py b/test/unit/applications/websockets.py index 50ff2797..5f78498c 100644 --- a/test/unit/applications/websockets.py +++ b/test/unit/applications/websockets.py @@ -1,3 +1,4 @@ +import re import random import base64 import struct @@ -32,11 +33,7 @@ class TestApplicationWebsocket(TestApplicationProto): def upgrade(self): key = self.key() - - if self.preinit: - self.get() - - resp, sock = self.get( + _, sock = self.get( headers={ 'Host': 'localhost', 'Upgrade': 'websocket', @@ -45,10 +42,21 @@ class TestApplicationWebsocket(TestApplicationProto): 'Sec-WebSocket-Protocol': 'chat', 'Sec-WebSocket-Version': 13, }, - read_timeout=1, + no_recv=True, start=True, ) + resp = '' + while select.select([sock], [], [], 30)[0]: + resp += sock.recv(4096).decode() + + if ( + re.search('101 Switching Protocols', resp) + and resp[-4:] == '\r\n\r\n' + ): + resp = self._resp_to_dict(resp) + break + return (resp, sock, key) def apply_mask(self, data, mask): -- cgit From ed3298a3c68bb257b43425c92b07e224c7f46be3 Mon Sep 17 00:00:00 2001 From: Andrey Zelenkov Date: Fri, 8 Nov 2019 14:04:32 +0300 Subject: Tests: fixed operator in http.py. --- test/unit/http.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/unit') diff --git a/test/unit/http.py b/test/unit/http.py index 82a6bd6a..1a68ca3b 100644 --- a/test/unit/http.py +++ b/test/unit/http.py @@ -63,7 +63,7 @@ class TestHTTP(TestUnit): if 'raw' not in kwargs: req = ' '.join([start_str, url, http]) + crlf - if body is not b'': + if body != b'': if isinstance(body, str): body = body.encode() -- cgit From 5452ee458d2c764569213266362fb636114adbc2 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Wed, 13 Nov 2019 18:11:24 +0300 Subject: Tests: fixed websocket tests. --- test/unit/applications/websockets.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'test/unit') diff --git a/test/unit/applications/websockets.py b/test/unit/applications/websockets.py index 5f78498c..ef16f433 100644 --- a/test/unit/applications/websockets.py +++ b/test/unit/applications/websockets.py @@ -31,17 +31,22 @@ class TestApplicationWebsocket(TestApplicationProto): sha1 = hashlib.sha1((key + GUID).encode()).digest() return base64.b64encode(sha1).decode() - def upgrade(self): - key = self.key() - _, sock = self.get( - headers={ + def upgrade(self, headers=None): + key = None + + if headers is None: + key = self.key() + headers = { 'Host': 'localhost', 'Upgrade': 'websocket', 'Connection': 'Upgrade', 'Sec-WebSocket-Key': key, 'Sec-WebSocket-Protocol': 'chat', 'Sec-WebSocket-Version': 13, - }, + } + + _, sock = self.get( + headers=headers, no_recv=True, start=True, ) -- cgit From b5e3e22a46df6700415583002a15cc15eaac8514 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Thu, 14 Nov 2019 17:14:55 +0300 Subject: Tests: waitforsocket() introduced. --- test/unit/http.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'test/unit') diff --git a/test/unit/http.py b/test/unit/http.py index 1a68ca3b..c7e3e36d 100644 --- a/test/unit/http.py +++ b/test/unit/http.py @@ -1,4 +1,5 @@ import re +import time import socket import select from unit.main import TestUnit @@ -178,3 +179,20 @@ class TestHTTP(TestUnit): headers[m.group(1)] = [headers[m.group(1)], m.group(2)] return {'status': int(status), 'headers': headers, 'body': body} + + def waitforsocket(self, port): + ret = False + + for i in range(50): + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect(('127.0.0.1', port)) + ret = True + break + except: + sock.close() + time.sleep(0.1) + + sock.close() + + self.assertTrue(ret, 'socket connected') -- cgit From efb461c0e1dcd15577a6a072668990facc5533f6 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Thu, 14 Nov 2019 17:15:20 +0300 Subject: Tests: run_process() and stop_processes() introduced. --- test/unit/main.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'test/unit') diff --git a/test/unit/main.py b/test/unit/main.py index f5727726..094fdb0e 100644 --- a/test/unit/main.py +++ b/test/unit/main.py @@ -185,6 +185,8 @@ class TestUnit(unittest.TestCase): if self._started: self._stop() + self.stop_processes() + def _run(self): self.unitd = self.pardir + '/build/unitd' @@ -287,6 +289,26 @@ class TestUnit(unittest.TestCase): if found: print('skipped.') + def run_process(self, target): + if not hasattr(self, '_processes'): + self._processes = [] + + process = Process(target=target) + process.start() + + self._processes.append(process) + + def stop_processes(self): + if not hasattr(self, '_processes'): + return + + for process in self._processes: + process.terminate() + process.join(timeout=5) + + if process.is_alive(): + self.fail('Fail to stop process') + def waitforfiles(self, *files): for i in range(50): wait = False -- cgit