diff options
author | drebs <drebs@leap.se> | 2017-01-26 19:18:15 -0200 |
---|---|---|
committer | Kali Kaneko <kali@leap.se> | 2017-02-09 17:41:45 +0100 |
commit | 47858d88ca4ca10ac363c71550b1bafe50f8f4ce (patch) | |
tree | b5bffe1b526e55c3279498851bd1c2628d83291b | |
parent | 47c357213b4e39c6ced818de7eeb401e52bf92b9 (diff) |
[refactor] allow passing threadpool pool for server sync resource
Conflicts:
server/src/leap/soledad/server/_resource.py
testing/tests/server/test__resource.py
-rw-r--r-- | server/pkg/soledad-server | 2 | ||||
-rw-r--r-- | server/src/leap/soledad/server/_resource.py | 5 | ||||
-rw-r--r-- | server/src/leap/soledad/server/_wsgi.py | 13 | ||||
-rw-r--r-- | server/src/leap/soledad/server/auth.py | 13 | ||||
-rw-r--r-- | server/src/leap/soledad/server/entrypoint.py | 11 | ||||
-rw-r--r-- | testing/tests/server/test_auth.py | 5 |
6 files changed, 33 insertions, 16 deletions
diff --git a/server/pkg/soledad-server b/server/pkg/soledad-server index 92560fa8..450f2277 100644 --- a/server/pkg/soledad-server +++ b/server/pkg/soledad-server @@ -11,7 +11,7 @@ PATH=/sbin:/bin:/usr/sbin:/usr/bin PIDFILE=/var/run/soledad.pid -RESOURCE_CLASS=leap.soledad.server.session.SoledadSession +RESOURCE_CLASS=leap.soledad.server.entrypoint.SoledadEntrypoint HTTPS_PORT=2424 CONFDIR=/etc/soledad CERT_PATH="${CONFDIR}/soledad-server.pem" diff --git a/server/src/leap/soledad/server/_resource.py b/server/src/leap/soledad/server/_resource.py index 89e252d6..4070d786 100644 --- a/server/src/leap/soledad/server/_resource.py +++ b/server/src/leap/soledad/server/_resource.py @@ -19,7 +19,7 @@ A twisted resource that serves the Soledad Server. """ from twisted.web.resource import Resource -from ._wsgi import sync_resource +from ._wsgi import get_sync_resource __all__ = ['SoledadResource'] @@ -31,7 +31,8 @@ class SoledadResource(Resource): for the Soledad Server. """ - def __init__(self): + def __init__(self, sync_pool=None): + sync_resource = get_sync_resource(sync_pool) self.children = {'': sync_resource} def getChild(self, path, request): diff --git a/server/src/leap/soledad/server/_wsgi.py b/server/src/leap/soledad/server/_wsgi.py index 13c8d13b..3e30d560 100644 --- a/server/src/leap/soledad/server/_wsgi.py +++ b/server/src/leap/soledad/server/_wsgi.py @@ -33,7 +33,7 @@ from leap.soledad.common.couch.state import CouchServerState from leap.soledad.common.log import getLogger -__all__ = ['init_couch_state', 'sync_resource'] +__all__ = ['init_couch_state', 'get_sync_resource'] _config = None @@ -76,8 +76,9 @@ def init_couch_state(_app): reactor.stop() -# setup a wsgi resource with its own threadpool -pool = threadpool.ThreadPool() -reactor.callWhenRunning(pool.start) -reactor.addSystemEventTrigger('after', 'shutdown', pool.stop) -sync_resource = WSGIResource(reactor, pool, wsgi_application) +def get_sync_resource(pool=None): + if not pool: + pool = threadpool.ThreadPool() + reactor.callWhenRunning(pool.start) + reactor.addSystemEventTrigger('after', 'shutdown', pool.stop) + return WSGIResource(reactor, pool, wsgi_application) diff --git a/server/src/leap/soledad/server/auth.py b/server/src/leap/soledad/server/auth.py index e616a94f..a5d90c46 100644 --- a/server/src/leap/soledad/server/auth.py +++ b/server/src/leap/soledad/server/auth.py @@ -42,9 +42,13 @@ from ._wsgi import get_config @implementer(IRealm) class SoledadRealm(object): + def __init__(self, sync_pool=None): + self._sync_pool = sync_pool + def requestAvatar(self, avatarId, mind, *interfaces): if IResource in interfaces: - return (IResource, SoledadResource(), lambda: None) + resource = SoledadResource(sync_pool=self._sync_pool) + return (IResource, resource, lambda: None) raise NotImplementedError() @@ -127,5 +131,10 @@ class TokenCredentialFactory(object): raise error.LoginFailed('Invalid credentials') -get_portal = lambda: Portal(SoledadRealm(), [TokenChecker()]) +def get_portal(sync_pool=None): + realm = SoledadRealm(sync_pool=sync_pool) + checker = TokenChecker() + return Portal(realm, [checker]) + + credentialFactory = TokenCredentialFactory() diff --git a/server/src/leap/soledad/server/entrypoint.py b/server/src/leap/soledad/server/entrypoint.py index df2b8934..7501a447 100644 --- a/server/src/leap/soledad/server/entrypoint.py +++ b/server/src/leap/soledad/server/entrypoint.py @@ -20,7 +20,7 @@ The entrypoint for Soledad server. from twisted.internet import reactor from .config import load_configuration -from ._resource import SoledadResource +from ._session import SoledadSession from ._wsgi import init_couch_state @@ -28,10 +28,13 @@ from ._wsgi import init_couch_state conf = load_configuration('/etc/soledad/soledad-server.conf') -class SoledadEntrypoint(SoledadResource): +class SoledadEntrypoint(SoledadSession): - def __init__(self): - SoledadResource.__init__(self, conf) + # the purpose of the entrypoint is to avoid trying to load the + # configuration file during tests. This class will be more useful when we + # add the blobs feature toggle. For now, the whole entrypoint + + pass # see the comments in application.py recarding why couch state has to be diff --git a/testing/tests/server/test_auth.py b/testing/tests/server/test_auth.py index 8ef35177..f7fe0f25 100644 --- a/testing/tests/server/test_auth.py +++ b/testing/tests/server/test_auth.py @@ -23,6 +23,7 @@ from contextlib import contextmanager from twisted.cred.credentials import UsernamePassword from twisted.cred.error import UnauthorizedLogin +from twisted.internet import reactor from twisted.internet.defer import inlineCallbacks from twisted.trial import unittest from twisted.web.resource import IResource @@ -37,7 +38,9 @@ from leap.soledad.server._resource import SoledadResource class SoledadRealmTestCase(unittest.TestCase): def test_returned_resource(self): - realm = SoledadRealm() + # we have to pass a pool to the realm , otherwise tests will hang + pool = reactor.getThreadPool() + realm = SoledadRealm(sync_pool=pool) iface, avatar, logout = realm.requestAvatar('any', None, IResource) self.assertIsInstance(avatar, SoledadResource) self.assertIsNone(logout()) |