]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
pytests: import test_long_lived (test2)
authorTomas Krizek <tomas.krizek@nic.cz>
Wed, 14 Nov 2018 11:46:32 +0000 (12:46 +0100)
committerTomas Krizek <tomas.krizek@nic.cz>
Tue, 4 Dec 2018 16:13:42 +0000 (17:13 +0100)
tests/pytests/kresd.py
tests/pytests/test_conn_mgmt.py
tests/pytests/test_tcp_prefix.py
tests/pytests/utils.py

index 0ef25661e918088a5fec540b0dd35ef2c77ab5d8..0f9e174c655a7f0052d5d438b707ff71f325bf74 100644 (file)
@@ -41,13 +41,6 @@ def make_ssl_context():
     return context
 
 
-def ping_alive(sock):
-    buff, msgid = utils.get_msgbuff()
-    sock.sendall(buff)
-    answer = utils.receive_parse_answer(sock)
-    return answer.id == msgid
-
-
 class Kresd(ContextDecorator):
     def __init__(self, workdir, port, tls_port, ip=None, ip6=None):
         if ip is None and ip6 is None:
@@ -110,11 +103,11 @@ class Kresd(ContextDecorator):
     def all_ports_alive(self):
         alive = True
         if self.ip:
-            alive &= ping_alive(self.ip_tcp_socket())
-            alive &= ping_alive(self.ip_tls_socket())
+            alive &= utils.ping_alive(self.ip_tcp_socket())
+            alive &= utils.ping_alive(self.ip_tls_socket())
         if self.ip6:
-            alive &= ping_alive(self.ip6_tcp_socket())
-            alive &= ping_alive(self.ip6_tls_socket())
+            alive &= utils.ping_alive(self.ip6_tcp_socket())
+            alive &= utils.ping_alive(self.ip6_tls_socket())
         return alive
 
     def _wait_for_tcp_port(self, delay=0.1, max_attempts=20):
@@ -127,7 +120,7 @@ class Kresd(ContextDecorator):
                 time.sleep(delay)
                 continue
             else:
-                return ping_alive(sock)
+                return utils.ping_alive(sock)
             finally:
                 sock.close()
         raise RuntimeError("Kresd didn't start in time")
index d7c335dca500ec38125ba06e7b87f6f9c30129db..2f884be5495fe9cf16361b613b251488b48961f0 100644 (file)
@@ -1,5 +1,7 @@
 """TCP Connection Management tests"""
 
+import time
+
 import utils
 
 
@@ -34,3 +36,18 @@ def test_pipelining(kresd_sock):
 
     msg_answer = utils.receive_parse_answer(kresd_sock)
     assert msg_answer.id == msgid1
+
+
+def test_long_lived(kresd_sock):
+    """
+    Test establishes a TCP connection a sends several queries over it. They are sent
+    seqeuntially, each with a delay, which in total exceeds maximum timeout.
+
+    Expected: kresd must not close the connection
+    """
+    utils.ping_alive(kresd_sock)
+    end_time = time.time() + utils.MAX_TIMEOUT
+
+    while time.time() < end_time:
+        time.sleep(3)
+        utils.ping_alive(kresd_sock)
index d611320934212d4ebc2563650d0b70105733d39a..3b1e33e69c919c49aebfba8f36141b7c9abdd23e 100644 (file)
@@ -21,11 +21,6 @@ import pytest
 import utils
 
 
-# default net.tcp_in_idle is 10s, TCP_DEFER_ACCEPT 3s, some extra for
-# Python handling / edge cases
-MAX_TIMEOUT = 16
-
-
 def send_incorrect_repeatedly(sock, buff, delay=1):
     """Utility function to keep sending the buffer until MAX_TIMEOUT is reached.
 
@@ -34,7 +29,7 @@ def send_incorrect_repeatedly(sock, buff, delay=1):
 
     If the connection remains open, test is failed.
     """
-    end_time = time.time() + MAX_TIMEOUT
+    end_time = time.time() + utils.MAX_TIMEOUT
 
     with pytest.raises(BrokenPipeError, message="kresd didn't close connection"):
         while time.time() < end_time:
index 9f365d8e4c1b79da2765a8206f0b9784c035ac5b..55cc819c2dcbd1ee888b863edfdee968012de568 100644 (file)
@@ -5,6 +5,11 @@ import dns
 import dns.message
 
 
+# default net.tcp_in_idle is 10s, TCP_DEFER_ACCEPT 3s, some extra for
+# Python handling / edge cases
+MAX_TIMEOUT = 16
+
+
 def receive_answer(sock):
     answer_total_len = 0
     data = sock.recv(2)
@@ -67,3 +72,10 @@ def get_garbage(length):
 def get_prefixed_garbage(length):
     data = get_garbage(length)
     return prepare_buffer(data)
+
+
+def ping_alive(sock):
+    buff, msgid = get_msgbuff()
+    sock.sendall(buff)
+    answer = receive_parse_answer(sock)
+    return answer.id == msgid