Browse Source

fixed same thread issues. Dont know, whether it is the right way, but it works

Dimitri Korsch 3 years ago
parent
commit
86ca2f2b93
4 changed files with 25 additions and 19 deletions
  1. 12 10
      pycs/__init__.py
  2. 1 1
      pycs/frontend/WebServer.py
  3. 11 4
      tests/base.py
  4. 1 4
      tests/client/pipeline_tests.py

+ 12 - 10
pycs/__init__.py

@@ -1,17 +1,18 @@
 import json
-import sys
 import os
+import sys
 # pylint: disable=wrong-import-position,wrong-import-order
 import eventlet.tpool
 eventlet.tpool.set_num_threads(2)
 
-from pathlib import Path
 from munch import munchify
+from pathlib import Path
 
 from flask import Flask
 from flask_migrate import Migrate
 from flask_sqlalchemy import SQLAlchemy
 from sqlalchemy import event
+from sqlalchemy import pool
 from sqlalchemy.engine import Engine
 
 print('- Loading settings')
@@ -23,14 +24,9 @@ with open('settings.json') as file:
 if not os.path.exists(settings.projects_folder):
     os.mkdir(settings.projects_folder)
 
-app = Flask(__name__)
-
-if "unittest" in sys.modules:
-    # creates an in-memory DB
-    DB_FILE = ""
-else:
-    DB_FILE = Path.cwd() / settings.database
+DB_FILE = Path.cwd() / settings.database
 
+app = Flask(__name__)
 app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{DB_FILE}"
 app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
 
@@ -42,5 +38,11 @@ def set_sqlite_pragma(dbapi_connection, connection_record):
     cursor.execute("PRAGMA foreign_keys=ON")
     cursor.close()
 
-db = SQLAlchemy(app)
+db = SQLAlchemy(app, engine_options=dict(
+    poolclass=pool.SingletonThreadPool,
+    connect_args=dict(
+        check_same_thread=False
+        )
+    )
+)
 migrate = Migrate(app, db)

+ 1 - 1
pycs/frontend/WebServer.py

@@ -117,7 +117,6 @@ class WebServer:
         # create notification manager
         self.jobs = JobRunner()
         self.pipelines = PipelineCache(self.jobs, settings.get("pipeline_cache_time"))
-        self.pipelines.start()
         self.notifications = NotificationManager(self.__sio)
 
         self.jobs.on_create(self.notifications.create_job)
@@ -321,4 +320,5 @@ class WebServer:
 
     def run(self):
         """ start web server """
+        self.pipelines.start()
         eventlet.wsgi.server(eventlet.listen((self.host, self.port)), self.wsgi_app)

+ 11 - 4
tests/base.py

@@ -5,6 +5,7 @@ import eventlet
 import typing as T
 
 from unittest import mock
+from pathlib import Path
 
 from pycs import app
 from pycs import db
@@ -30,16 +31,18 @@ class BaseTestCase(unittest.TestCase):
     _sleep_time = 0.2
     server = None
 
+    DB_FILE = Path.cwd() / "test.sqlite"
+
     @classmethod
     def setUpClass(cls, discovery: bool = False):
         global server
-        PipelineCache.CLOSE_TIMER = 2
         app.config["TESTING"] = True
         app.config["WTF_CSRF_ENABLED"] = False
         app.config["DEBUG"] = False
-        app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///"
+        app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{cls.DB_FILE}"
 
         if server is None:
+            settings["pipeline_cache_time"] = 2
             server = WebServer(app, settings, discovery)
 
         if cls.server is None:
@@ -52,7 +55,7 @@ class BaseTestCase(unittest.TestCase):
         cls.server.pipelines.start()
 
 
-    def wait_for_bg_jobs(self):
+    def wait_for_bg_jobs(self, raise_errors=True):
 
         # wait for JobRunner jobs to finish
         while True:
@@ -63,6 +66,10 @@ class BaseTestCase(unittest.TestCase):
                     ready = False
                     break
 
+                if raise_errors:
+                    self.assertTrue(job.exception is None,
+                        job.exception)
+
             if ready:
                 break
 
@@ -95,7 +102,7 @@ class BaseTestCase(unittest.TestCase):
         pass
 
     def tearDown(self):
-        self.wait_for_bg_jobs()
+        self.wait_for_bg_jobs(raise_errors=False)
 
         self.context.pop()
 

+ 1 - 4
tests/client/pipeline_tests.py

@@ -1,6 +1,3 @@
-
-import eventlet
-import tempfile
 import uuid
 
 from flask import url_for
@@ -55,7 +52,7 @@ class PipelineTests(BaseTestCase):
 
 
     def tearDown(self):
-        self.wait_for_bg_jobs()
+        self.wait_for_bg_jobs(raise_errors=False)
         self.project.delete()
         super().tearDown()