Tests: added additional check in tests with timeouts.

This commit is contained in:
Andrei Zeliankou
2021-05-24 22:28:23 +01:00
parent 1154ede862
commit 39c0fda24c

View File

@@ -14,8 +14,7 @@ class TestSettings(TestApplicationPython):
def test_settings_header_read_timeout(self): def test_settings_header_read_timeout(self):
self.load('empty') self.load('empty')
self.conf({'http': {'header_read_timeout': 2}}, 'settings') def req():
(resp, sock) = self.http( (resp, sock) = self.http(
b"""GET / HTTP/1.1 b"""GET / HTTP/1.1
""", """,
@@ -26,7 +25,7 @@ class TestSettings(TestApplicationPython):
time.sleep(3) time.sleep(3)
resp = self.http( return self.http(
b"""Host: localhost b"""Host: localhost
Connection: close Connection: close
@@ -35,12 +34,22 @@ Connection: close
raw=True, raw=True,
) )
assert resp['status'] == 408, 'status header read timeout' assert 'success' in self.conf(
{'http': {'header_read_timeout': 2}}, 'settings'
)
assert req()['status'] == 408, 'status header read timeout'
assert 'success' in self.conf(
{'http': {'header_read_timeout': 7}}, 'settings'
)
assert req()['status'] == 200, 'status header read timeout 2'
def test_settings_header_read_timeout_update(self): def test_settings_header_read_timeout_update(self):
self.load('empty') self.load('empty')
self.conf({'http': {'header_read_timeout': 4}}, 'settings') assert 'success' in self.conf(
{'http': {'header_read_timeout': 4}}, 'settings'
)
(resp, sock) = self.http( (resp, sock) = self.http(
b"""GET / HTTP/1.1 b"""GET / HTTP/1.1
@@ -91,8 +100,7 @@ Connection: close
def test_settings_body_read_timeout(self): def test_settings_body_read_timeout(self):
self.load('empty') self.load('empty')
self.conf({'http': {'body_read_timeout': 2}}, 'settings') def req():
(resp, sock) = self.http( (resp, sock) = self.http(
b"""POST / HTTP/1.1 b"""POST / HTTP/1.1
Host: localhost Host: localhost
@@ -108,14 +116,24 @@ Connection: close
time.sleep(3) time.sleep(3)
resp = self.http(b"""0123456789""", sock=sock, raw=True) return self.http(b"""0123456789""", sock=sock, raw=True)
assert resp['status'] == 408, 'status body read timeout' assert 'success' in self.conf(
{'http': {'body_read_timeout': 2}}, 'settings'
)
assert req()['status'] == 408, 'status body read timeout'
assert 'success' in self.conf(
{'http': {'body_read_timeout': 7}}, 'settings'
)
assert req()['status'] == 200, 'status body read timeout 2'
def test_settings_body_read_timeout_update(self): def test_settings_body_read_timeout_update(self):
self.load('empty') self.load('empty')
self.conf({'http': {'body_read_timeout': 4}}, 'settings') assert 'success' in self.conf(
{'http': {'body_read_timeout': 4}}, 'settings'
)
(resp, sock) = self.http( (resp, sock) = self.http(
b"""POST / HTTP/1.1 b"""POST / HTTP/1.1
@@ -150,22 +168,7 @@ Connection: close
def test_settings_send_timeout(self, temp_dir): def test_settings_send_timeout(self, temp_dir):
self.load('body_generate') self.load('body_generate')
sysctl_out = sysctl() def req(addr, data_len):
values = re.findall(
r'net.core.[rw]mem_(?:max|default).*?(\d+)', sysctl_out
)
values = [int(v) for v in values]
data_len = 1048576 if len(values) == 0 else 10 * max(values)
self.conf({'http': {'send_timeout': 1}}, 'settings')
addr = temp_dir + '/sock'
self.conf(
{"unix:" + addr: {'application': 'body_generate'}}, 'listeners'
)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(addr) sock.connect(addr)
@@ -189,16 +192,40 @@ Connection: close
sock.close() sock.close()
assert re.search(r'200 OK', data), 'status send timeout' return data
assert len(data) < data_len, 'data send timeout'
sysctl_out = sysctl()
values = re.findall(
r'net.core.[rw]mem_(?:max|default).*?(\d+)', sysctl_out
)
values = [int(v) for v in values]
data_len = 1048576 if len(values) == 0 else 10 * max(values)
addr = temp_dir + '/sock'
assert 'success' in self.conf(
{"unix:" + addr: {'application': 'body_generate'}}, 'listeners'
)
assert 'success' in self.conf(
{'http': {'send_timeout': 1}}, 'settings'
)
data = req(addr, data_len)
assert re.search(r'200 OK', data), 'send timeout status'
assert len(data) < data_len, 'send timeout data '
self.conf({'http': {'send_timeout': 7}}, 'settings')
data = req(addr, data_len)
assert re.search(r'200 OK', data), 'send timeout status 2'
assert len(data) > data_len, 'send timeout data 2'
def test_settings_idle_timeout(self): def test_settings_idle_timeout(self):
self.load('empty') self.load('empty')
assert self.get()['status'] == 200, 'init' def req():
self.conf({'http': {'idle_timeout': 2}}, 'settings')
(resp, sock) = self.get( (resp, sock) = self.get(
headers={'Host': 'localhost', 'Connection': 'keep-alive'}, headers={'Host': 'localhost', 'Connection': 'keep-alive'},
start=True, start=True,
@@ -207,34 +234,48 @@ Connection: close
time.sleep(3) time.sleep(3)
resp = self.get( return self.get(sock=sock)
headers={'Host': 'localhost', 'Connection': 'close'}, sock=sock
)
assert resp['status'] == 408, 'status idle timeout' assert self.get()['status'] == 200, 'init'
assert 'success' in self.conf(
{'http': {'idle_timeout': 2}}, 'settings'
)
assert req()['status'] == 408, 'status idle timeout'
assert 'success' in self.conf(
{'http': {'idle_timeout': 7}}, 'settings'
)
assert req()['status'] == 200, 'status idle timeout 2'
def test_settings_idle_timeout_2(self): def test_settings_idle_timeout_2(self):
self.load('empty') self.load('empty')
assert self.get()['status'] == 200, 'init' def req():
self.conf({'http': {'idle_timeout': 1}}, 'settings')
_, sock = self.http(b'', start=True, raw=True, no_recv=True) _, sock = self.http(b'', start=True, raw=True, no_recv=True)
time.sleep(3) time.sleep(3)
assert ( return self.get(sock=sock)
self.get(
headers={'Host': 'localhost', 'Connection': 'close'}, sock=sock assert self.get()['status'] == 200, 'init'
)['status']
== 408 assert 'success' in self.conf(
), 'status idle timeout' {'http': {'idle_timeout': 1}}, 'settings'
)
assert req()['status'] == 408, 'status idle timeout'
assert 'success' in self.conf(
{'http': {'idle_timeout': 7}}, 'settings'
)
assert req()['status'] == 200, 'status idle timeout 2'
def test_settings_max_body_size(self): def test_settings_max_body_size(self):
self.load('empty') self.load('empty')
self.conf({'http': {'max_body_size': 5}}, 'settings') assert 'success' in self.conf(
{'http': {'max_body_size': 5}}, 'settings'
)
assert self.post(body='01234')['status'] == 200, 'status size' assert self.post(body='01234')['status'] == 200, 'status size'
assert self.post(body='012345')['status'] == 413, 'status size max' assert self.post(body='012345')['status'] == 413, 'status size max'
@@ -242,7 +283,9 @@ Connection: close
def test_settings_max_body_size_large(self): def test_settings_max_body_size_large(self):
self.load('mirror') self.load('mirror')
self.conf({'http': {'max_body_size': 32 * 1024 * 1024}}, 'settings') assert 'success' in self.conf(
{'http': {'max_body_size': 32 * 1024 * 1024}}, 'settings'
)
body = '0123456789abcdef' * 4 * 64 * 1024 body = '0123456789abcdef' * 4 * 64 * 1024
resp = self.post(body=body, read_buffer_size=1024 * 1024) resp = self.post(body=body, read_buffer_size=1024 * 1024)