Exemple #1
0
    def test_scope_error(self):
        from aiopyramid.exceptions import ScopeError
        from aiopyramid.helpers import synchronize, spawn_greenlet

        synced = synchronize(self._sample)
        self.assertRaises(ScopeError, synced, 'val')

        five = asyncio.get_event_loop().run_until_complete(
            spawn_greenlet(synced, 5), )
        self.assertEqual(five, 5)

        synced = synchronize(self._sample, strict=False)
        self.assertTrue(asyncio.iscoroutine(synced('val')))
Exemple #2
0
    def test_scope_error(self):
        from aiopyramid.exceptions import ScopeError
        from aiopyramid.helpers import synchronize, spawn_greenlet

        synced = synchronize(self._sample)
        self.assertRaises(ScopeError, synced, 'val')

        five = asyncio.get_event_loop().run_until_complete(
            spawn_greenlet(synced, 5),
        )
        self.assertEqual(five, 5)

        synced = synchronize(self._sample, strict=False)
        self.assertTrue(asyncio.iscoroutine(synced('val')))
Exemple #3
0
    def test_conversion(self):
        from aiopyramid.helpers import synchronize
        from aiopyramid.helpers import is_generator

        syncer = synchronize(strict=True)
        self.assertRaises(ConfigurationError, syncer, self._simple)
        self.assertFalse(is_generator(syncer(self._sample)))
Exemple #4
0
 def test_wrapper_in_coroutine(self, wrapped_policy, web_request):
     loop = asyncio.get_event_loop()
     loop.run_until_complete(spawn_greenlet(
         synchronize(self.yield_from_authn_policy_methods),
         wrapped_policy,
         web_request,
     ))
Exemple #5
0
 def test_wrapper_in_coroutine(self, wrapped_policy, web_request):
     loop = asyncio.get_event_loop()
     loop.run_until_complete(
         spawn_greenlet(
             synchronize(self.yield_from_authn_policy_methods),
             wrapped_policy,
             web_request,
         ))
Exemple #6
0
    def handle_request(self, message, payload):
        """ Patched from aiohttp. """
        now = time.time()

        if self.readpayload:
            wsgiinput = io.BytesIO()
            wsgiinput.write((yield from payload.read()))
            wsgiinput.seek(0)
            payload = wsgiinput
        else:
            # allow read to be called from a synchronous context
            payload.read = synchronize(payload.read)
            payload.read = spawn_greenlet_on_scope_error(payload.read)

        environ = self.create_wsgi_environ(message, payload)
        # add a reference to this for switching protocols
        environ['async.protocol'] = self

        response = self.create_wsgi_response(message)

        riter = yield from spawn_greenlet(
            self.wsgi,
            environ,
            response.start_response
        )

        resp = response.response
        try:
            for item in riter:
                if isinstance(item, asyncio.Future):
                    item = yield from item
                yield from resp.write(item)

            yield from resp.write_eof()
        finally:
            if hasattr(riter, 'close'):
                riter.close()

        if resp.keep_alive():
            self.keep_alive(True)

        self.log_access(
            message, environ, response.response, time.time() - now)
Exemple #7
0
    def handle_request(self, message, payload):
        """ Patched from aiohttp. """
        now = time.time()

        if self.readpayload:
            wsgiinput = io.BytesIO()
            wsgiinput.write((yield from payload.read()))
            wsgiinput.seek(0)
            payload = wsgiinput
        else:
            # allow read to be called from a synchronous context
            payload.read = synchronize(payload.read)
            payload.read = spawn_greenlet_on_scope_error(payload.read)

        environ = self.create_wsgi_environ(message, payload)
        # add a reference to this for switching protocols
        environ['async.protocol'] = self

        response = self.create_wsgi_response(message)

        riter = yield from spawn_greenlet(self.wsgi, environ,
                                          response.start_response)

        resp = response.response
        try:
            for item in riter:
                if isinstance(item, asyncio.Future):
                    item = yield from item
                yield from resp.write(item)

            yield from resp.write_eof()
        finally:
            if hasattr(riter, 'close'):
                riter.close()

        if resp.keep_alive():
            self.keep_alive(True)

        self.log_access(message, environ, response.response, time.time() - now)