]> www.average.org Git - loctrkd.git/blobdiff - test/test_fuzz.py
test: move `send_and_drain` to common module
[loctrkd.git] / test / test_fuzz.py
index d937c95a3dd216a064e2718ce38dbdf68b391b23..6be5348394c2ff666973d2dfcc3356cc146a75f6 100644 (file)
@@ -1,9 +1,10 @@
 """ Send junk to the collector """
 
 from random import Random
-from socket import getaddrinfo, socket, AF_INET6, MSG_DONTWAIT, SOCK_STREAM
+from socket import getaddrinfo, socket, AF_INET6, SOCK_STREAM
+from time import sleep
 import unittest
-from .common import TestWithServers
+from .common import send_and_drain, TestWithServers
 
 REPEAT: int = 1000000
 
@@ -23,19 +24,23 @@ class Fuzz(TestWithServers):
         self.sock.connect(skadr)
 
     def tearDown(self) -> None:
+        sleep(1)  # give collector some time
+        send_and_drain(self.sock, None)
         self.sock.close()
         print("finished fuzzing")
         super().tearDown()
 
-    def test_fuzz(self) -> None:
+    def test_stream(self) -> None:
         for _ in range(REPEAT):
             size = self.rnd.randint(1, 5000)
             buf = self.rnd.randbytes(size)
-            self.sock.send(buf)
-            try:
-                self.sock.recv(4096, MSG_DONTWAIT)
-            except BlockingIOError:
-                pass
+            send_and_drain(self.sock, buf)
+
+    def test_msgs(self) -> None:
+        for _ in range(REPEAT):
+            size = self.rnd.randint(0, 300)
+            buf = b"xx" + self.rnd.randbytes(size) + b"\r\n"
+            send_and_drain(self.sock, buf)
 
 
 if __name__ == "__main__":