]> average.org Git - loctrkd.git/blobdiff - test/common.py
test: opencellid downloader
[loctrkd.git] / test / common.py
index 38c59d588e72c87e1683cb2c4a3dc9d3853ae392..b9f768c8f55d84573a9149f5de248f955039d32b 100644 (file)
@@ -1,7 +1,8 @@
 """ Common housekeeping for tests that rely on daemons """
 
 from configparser import ConfigParser, SectionProxy
 """ Common housekeeping for tests that rely on daemons """
 
 from configparser import ConfigParser, SectionProxy
-from contextlib import closing
+from contextlib import closing, ExitStack
+from http.server import HTTPServer, SimpleHTTPRequestHandler
 from importlib import import_module
 from multiprocessing import Process
 from os import kill, unlink
 from importlib import import_module
 from multiprocessing import Process
 from os import kill, unlink
@@ -15,22 +16,24 @@ from socket import (
     socket,
     SocketType,
 )
     socket,
     SocketType,
 )
+from sys import exit
 from tempfile import mkstemp
 from time import sleep
 from typing import Optional
 from unittest import TestCase
 
 from tempfile import mkstemp
 from time import sleep
 from typing import Optional
 from unittest import TestCase
 
+NUMPORTS = 3
+
 
 class TestWithServers(TestCase):
 
 class TestWithServers(TestCase):
-    def setUp(self, *args: str) -> None:
-        with closing(socket(AF_INET6, SOCK_DGRAM)) as sock1, closing(
-            socket(AF_INET6, SOCK_DGRAM)
-        ) as sock2:
-            freeports = []
-            for sock in sock1, sock2:
-                sock.bind(("", 0))
-                sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
-                freeports.append(sock.getsockname()[1])
+    def setUp(self, *args: str, httpd: bool = False) -> None:
+        freeports = []
+        with ExitStack() as stack:
+            for _ in range(NUMPORTS):
+                sk = stack.enter_context(closing(socket(AF_INET6, SOCK_DGRAM)))
+                sk.bind(("", 0))
+                sk.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
+                freeports.append(sk.getsockname()[1])
         _, self.tmpfilebase = mkstemp()
         self.conf = ConfigParser()
         self.conf["collector"] = {
         _, self.tmpfilebase = mkstemp()
         self.conf = ConfigParser()
         self.conf["collector"] = {
@@ -43,6 +46,7 @@ class TestWithServers(TestCase):
         }
         self.conf["opencellid"] = {
             "dbfn": self.tmpfilebase + ".opencellid.sqlite",
         }
         self.conf["opencellid"] = {
             "dbfn": self.tmpfilebase + ".opencellid.sqlite",
+            "downloadurl": f"http://localhost:{freeports[2]}/test/262.csv.gz",
         }
         self.conf["lookaside"] = {
             "backend": "opencellid",
         }
         self.conf["lookaside"] = {
             "backend": "opencellid",
@@ -60,6 +64,19 @@ class TestWithServers(TestCase):
             p = Process(target=cls.runserver, args=(self.conf,), kwargs=kwargs)
             p.start()
             self.children.append((srvname, p))
             p = Process(target=cls.runserver, args=(self.conf,), kwargs=kwargs)
             p.start()
             self.children.append((srvname, p))
+        if httpd:
+            server = HTTPServer(("", freeports[2]), SimpleHTTPRequestHandler)
+
+            def run(server):
+                try:
+                    server.serve_forever()
+                except KeyboardInterrupt:
+                    # TODO: this still leaves unclosed socket in the server
+                    server.shutdown()
+
+            p = Process(target=run, args=(server,))
+            p.start()
+            self.children.append(("httpd", p))
         sleep(1)
 
     def tearDown(self) -> None:
         sleep(1)
 
     def tearDown(self) -> None: