From 86ed99a7d78e4690d103f4334dc0eef03c47e402 Mon Sep 17 00:00:00 2001 From: Duda Dornelles Date: Thu, 22 Jan 2015 15:50:36 -0200 Subject: #224 fixing tests and app for twisted migration --- service/pixelated/config/app_factory.py | 1 + service/pixelated/resources/__init__.py | 4 +-- .../pixelated/resources/attachments_resource.py | 8 +++-- service/pixelated/resources/contacts_resource.py | 3 +- service/pixelated/resources/mails_resource.py | 1 + service/pixelated/resources/root_resource.py | 2 +- service/test/functional/features/environment.py | 4 +-- .../test/support/integration/app_test_client.py | 42 +++++++++++++++------- service/test/support/test_helper.py | 4 +++ .../unit/resources/test_sync_info_controller.py | 5 ++- 10 files changed, 49 insertions(+), 25 deletions(-) diff --git a/service/pixelated/config/app_factory.py b/service/pixelated/config/app_factory.py index 9a80640b..86816b87 100644 --- a/service/pixelated/config/app_factory.py +++ b/service/pixelated/config/app_factory.py @@ -116,6 +116,7 @@ def create_app(app, args): listen_with_ssl(app, args) else: listen_without_ssl(app, args) + reactor.suggestThreadPoolSize(20) reactor.callWhenRunning(lambda: init_app(app, args.home)) reactor.run() diff --git a/service/pixelated/resources/__init__.py b/service/pixelated/resources/__init__.py index 9e9c154e..92a4462f 100644 --- a/service/pixelated/resources/__init__.py +++ b/service/pixelated/resources/__init__.py @@ -19,8 +19,8 @@ def respond_json(entity, request, status_code=200): json_response = json.dumps(entity) request.responseHeaders.addRawHeader(b"content-type", b"application/json") request.code = status_code - request.write(json_response) - request.finish() + return json_response + # request.finish() def respond_json_deferred(entity, request, status_code=200): diff --git a/service/pixelated/resources/attachments_resource.py b/service/pixelated/resources/attachments_resource.py index 0ab214b9..e0ba1bd1 100644 --- a/service/pixelated/resources/attachments_resource.py +++ b/service/pixelated/resources/attachments_resource.py @@ -19,10 +19,14 @@ import io import re from twisted.protocols.basic import FileSender from twisted.python.log import err +from twisted.web import server from twisted.web.resource import Resource class AttachmentResource(Resource): + + isLeaf = True + def __init__(self, attachment_id, querier): Resource.__init__(self) self.attachment_id = attachment_id @@ -44,7 +48,7 @@ class AttachmentResource(Resource): d.addErrback(err).addCallback(cb_finished) - return d + return server.NOT_DONE_YET def _extract_mimetype(self, content_type): match = re.compile('([A-Za-z-]+\/[A-Za-z-]+)').search(content_type) @@ -53,8 +57,6 @@ class AttachmentResource(Resource): class AttachmentsResource(Resource): - isLeaf = True - def __init__(self, querier): Resource.__init__(self) self.querier = querier diff --git a/service/pixelated/resources/contacts_resource.py b/service/pixelated/resources/contacts_resource.py index 94468a63..5ec39761 100644 --- a/service/pixelated/resources/contacts_resource.py +++ b/service/pixelated/resources/contacts_resource.py @@ -16,6 +16,7 @@ from pixelated.resources import respond_json_deferred from twisted.internet.threads import deferToThread +from twisted.web import server from twisted.web.resource import Resource @@ -32,4 +33,4 @@ class ContactsResource(Resource): d = deferToThread(lambda: self._search_engine.contacts(query)) d.addCallback(lambda tags: respond_json_deferred(tags, request)) - return d + return server.NOT_DONE_YET diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py index ccd03ad6..a6eb0fe0 100644 --- a/service/pixelated/resources/mails_resource.py +++ b/service/pixelated/resources/mails_resource.py @@ -122,4 +122,5 @@ class MailsResource(Resource): else: pixelated_mail = self._draft_service.create_draft(_mail) self._search_engine.index_mail(pixelated_mail) + return respond_json({'ident': pixelated_mail.ident}, request) diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py index f230415d..7768472c 100644 --- a/service/pixelated/resources/root_resource.py +++ b/service/pixelated/resources/root_resource.py @@ -23,7 +23,7 @@ class RootResource(Resource): def initialize(self, querier, search_engine, mail_service, draft_service): self.putChild('assets', File(self._static_folder)) - self.putChild('attachments', AttachmentsResource(querier)) + self.putChild('attachment', AttachmentsResource(querier)) self.putChild('contacts', ContactsResource(search_engine)) self.putChild('features', FeaturesResource()) self.putChild('sync_info', SyncInfoResource()) diff --git a/service/test/functional/features/environment.py b/service/test/functional/features/environment.py index e4c4fa0c..5e93c840 100644 --- a/service/test/functional/features/environment.py +++ b/service/test/functional/features/environment.py @@ -19,14 +19,14 @@ from test.support.dispatcher.proxy import Proxy from test.support.integration import AppTestClient from selenium import webdriver -from pixelated.resources.features_resource import FeaturesController +from pixelated.resources.features_resource import FeaturesResource def before_all(context): logging.disable('INFO') client = AppTestClient() proxy = Proxy(proxy_port='8889', app_port='4567') - FeaturesController.DISABLED_FEATURES.append('autoRefresh') + FeaturesResource.DISABLED_FEATURES.append('autoRefresh') context.client = client context.call_to_terminate_proxy = proxy.run_on_a_thread() context.call_to_terminate = client.run_on_a_thread(logfile='/tmp/behave-tests.log') diff --git a/service/test/support/integration/app_test_client.py b/service/test/support/integration/app_test_client.py index 67bc1124..860b9c40 100644 --- a/service/test/support/integration/app_test_client.py +++ b/service/test/support/integration/app_test_client.py @@ -33,10 +33,16 @@ from pixelated.adapter.model.mail import PixelatedMail from pixelated.adapter.search import SearchEngine from test.support.integration.model import MailBuilder from test.support.test_helper import request_mock +from twisted.internet import reactor +from twisted.internet.defer import Deferred from twisted.web.resource import getChildForRequest +from twisted.web.server import Site class AppTestClient: + INDEX_KEY = '\xde3?\x87\xff\xd9\xd3\x14\xf0\xa7>\x1f%C{\x16.\\\xae\x8c\x13\xa7\xfb\x04\xd4]+\x8d_\xed\xd1\x8d\x0bI' \ + '\x8a\x0e\xa4tm\xab\xbf\xb4\xa5\x99\x00d\xd5w\x9f\x18\xbc\x1d\xd4_W\xd2\xb6\xe8H\x83\x1b\xd8\x9d\xad' + def __init__(self, soledad_test_folder='/tmp/soledad-test/test'): self.soledad = initialize_soledad(tempdir=soledad_test_folder) @@ -50,7 +56,7 @@ class AppTestClient: self.app = pixelated.runserver.app self.soledad_querier = SoledadQuerier(self.soledad) - self.soledad_querier.get_index_masterkey = lambda: '\xde3?\x87\xff\xd9\xd3\x14\xf0\xa7>\x1f%C{\x16.\\\xae\x8c\x13\xa7\xfb\x04\xd4]+\x8d_\xed\xd1\x8d\x0bI\x8a\x0e\xa4tm\xab\xbf\xb4\xa5\x99\x00d\xd5w\x9f\x18\xbc\x1d\xd4_W\xd2\xb6\xe8H\x83\x1b\xd8\x9d\xad' + self.soledad_querier.get_index_masterkey = lambda: self.INDEX_KEY self.account = SoledadBackedAccount('test', self.soledad, MagicMock()) self.mailboxes = Mailboxes(self.account, self.soledad_querier) @@ -67,27 +73,37 @@ class AppTestClient: self.app.resource.initialize(self.soledad_querier, self.search_engine, self.mail_service, self.draft_service) def _render(self, request, as_json=True): + def get_str(_str): + return json.loads(_str) if as_json else _str + def get_request_written_data(_=None): - written_data = None - if len(request.written): - written_data = request.written[0] + written_data = request.getWrittenData() if written_data: - return json.loads(written_data) if as_json else written_data + return get_str(written_data) resource = getChildForRequest(self.app.resource, request) - result = resource.render(request) + if isinstance(result, basestring): + return get_str(result), request + if request.finished: - return get_request_written_data(), request + d = Deferred() + d.addCallback(get_request_written_data) + return d, request else: - return request.notifyFinish().addCallback(lambda _: request).addCallback(get_request_written_data), request - - def run_on_a_thread(self, logfile='/tmp/app_test_client.log', port=4567, host='localhost'): - worker = lambda: self.app.run(host=host, port=port, logFile=open(logfile, 'w')) - process = multiprocessing.Process(target=worker) + d = request.notifyFinish() + d.addCallback(lambda _: request) + d.addCallback(get_request_written_data) + return d, request + + def run_on_a_thread(self, logfile='/tmp/app_test_client.log', port=4567, host='0.0.0.0'): + def _start(): + reactor.listenTCP(port, Site(self.app.resource), interface=host) + reactor.run() + process = multiprocessing.Process(target=_start) process.start() - time.sleep(1) # just let it start + time.sleep(1) return lambda: process.terminate() def get(self, path, get_args, as_json=True): diff --git a/service/test/support/test_helper.py b/service/test/support/test_helper.py index d2387d0c..54685008 100644 --- a/service/test/support/test_helper.py +++ b/service/test/support/test_helper.py @@ -91,6 +91,10 @@ class PixRequestMock(DummyRequest): self.content = None self.code = None + def getWrittenData(self): + if len(self.written): + return self.written[0] + def request_mock(path='', method='GET', body='', headers={}): dummy = PixRequestMock(path.split('/')) diff --git a/service/test/unit/resources/test_sync_info_controller.py b/service/test/unit/resources/test_sync_info_controller.py index c24c7181..a91dd386 100644 --- a/service/test/unit/resources/test_sync_info_controller.py +++ b/service/test/unit/resources/test_sync_info_controller.py @@ -21,7 +21,7 @@ from pixelated.resources.sync_info_resource import SyncInfoResource from mockito import * -class SyncInfoControllerTest(unittest.TestCase): +class SyncInfoResourceTest(unittest.TestCase): def setUp(self): self.dummy_request = request_mock() @@ -33,8 +33,7 @@ class SyncInfoControllerTest(unittest.TestCase): self.controller.set_sync_info(soledad_sync_data) def get_sync_info(self): - self.controller.render_GET(self.dummy_request) - return json.loads(self.dummy_request.written[0]) + return json.loads(self.controller.render_GET(self.dummy_request)) def test_is_not_syncing_if_total_is_equal_to_current(self): self._set_count(total=0, current=0) -- cgit v1.2.3