Ejemplo n.º 1
0
 def test_coroutine1(self):
     d1 = Deferred()
     a = maybe_async(c_summation(d1))
     d1.callback(1)
     yield a
     self.assertEqual(a.result, 3)
     self.assertEqual(d1.result, 1)
Ejemplo n.º 2
0
 def test_coroutine1(self):
     d1 = Deferred()
     a = maybe_async(c_summation(d1))
     d1.callback(1)
     yield a
     self.assertEqual(a.result, 3)
     self.assertEqual(d1.result, 1)
Ejemplo n.º 3
0
 def test_fail_deferred1(self):
     a = Deferred()
     d1 = Deferred().add_callback(lambda r: a.callback(r+2))\
                    .add_errback(a.callback)
     d1.callback('bla')
     self.assertIsInstance(a.result.error, TypeError)
     self.assertIsInstance(d1.result.error, TypeError)
     mute_failure(self, a.result)
Ejemplo n.º 4
0
 def test_json_with_async_string2(self):
     d = Deferred()
     astr = wsgi.AsyncString(d)
     response = wsgi.Json({'bla': astr})
     self.assertEqual(len(response.children), 1)
     result = response.render()
     self.assertIsInstance(result, Deferred)
     d.callback('ciao')
     result = yield result
     self.assertEqual(result, json.dumps({'bla': 'ciao'}))
Ejemplo n.º 5
0
 def testSimple(self):
     d = Deferred()
     self.assertFalse(d.called)
     self.assertFalse(d.running)
     self.assertEqual(str(d), 'Deferred')
     d.callback('ciao')
     self.assertTrue(d.called)
     self.assertTrue(' (called)' in str(d))
     self.assertEqual(d.result, 'ciao')
     self.assertRaises(AlreadyCalledError, d.callback, 'bla')
Ejemplo n.º 6
0
 def test_json_with_async_string2(self):
     d = Deferred()
     astr = wsgi.AsyncString(d)
     response = wsgi.Json({'bla': astr})
     self.assertEqual(len(response.children), 1)
     result = response.render()
     self.assertIsInstance(result, Deferred)
     d.callback('ciao')
     result = yield result
     self.assertEqual(result, json.dumps({'bla': 'ciao'}))
Ejemplo n.º 7
0
 def test_fail_coroutine1(self):
     d1 = Deferred()
     a = maybe_async(c_summation(d1))
     d1.callback('bla')
     try:
         yield a
     except TypeError:
         pass
     else:
         raise TypeError
     self.assertEqual(d1.result, 'bla')
Ejemplo n.º 8
0
 def test_fail_coroutine1(self):
     d1 = Deferred()
     a = maybe_async(c_summation(d1))
     d1.callback('bla')
     try:
         yield a
     except TypeError:
         pass
     else:
         raise TypeError
     self.assertEqual(d1.result, 'bla')
Ejemplo n.º 9
0
 def testDeferredCallback(self):
     d = Deferred()
     d.add_callback(lambda r : Cbk(r))
     self.assertFalse(d.called)
     result = d.callback('ciao')
     self.assertTrue(d.called)
     self.assertEqual(d.paused,1)
     self.assertTrue(is_async(result))
     self.assertEqual(len(result._callbacks),1)
     self.assertFalse(result.called)
     result.set_result('luca')
     self.assertTrue(result.called)
     self.assertEqual(result.result,('ciao','luca'))
     self.assertEqual(d.paused,0)
Ejemplo n.º 10
0
 def test_then1(self):
     a = Deferred()
     d1 = Deferred()
     d2 = d1.then().add_callback(lambda r: a.callback(r + 2))
     d1.callback(1)
     self.assertEqual(a.result, 3)
     self.assertEqual(d1.result, 1)
     self.assertEqual(d2.result, 3)
Ejemplo n.º 11
0
 def get_callback(self, task_id):
     with self.lock:
         callbacks = self.callbacks
         when_done = callbacks.get(task_id)
         if not when_done:
             # No deferred, create one and check again
             callbacks[task_id] = when_done = Deferred()
         return when_done
Ejemplo n.º 12
0
 def test_fail_then1(self):
     a = Deferred()
     d1 = Deferred()
     d2 = d1.then().add_callback(lambda r: a.callback(r+2))\
                   .add_errback(a.callback)
     d1.callback('bla')
     self.assertIsInstance(a.result.error, TypeError)
     self.assertEqual(d1.result, 'bla')
     self.assertIsInstance(d2.result.error, TypeError)
     mute_failure(self, a.result)
Ejemplo n.º 13
0
    def unsubscribe(self, *channels):
        '''Un-subscribe from a list of ``channels`` or ``channel patterns``.

        It returns an asynchronous component which results in the number of
        channels this handler is subscribed to.
        '''
        d = Deferred()
        loop = self.connection_pool.get_event_loop()
        loop.call_soon_threadsafe(self._unsubscribe, channels, d)
        return d
Ejemplo n.º 14
0
    def close(self):
        '''Stop listening for messages.

        :meth:`unsubscribe` from all :attr:`channels` and :attr:`patterns`
        and close the subscriber connection with redis.
        '''
        d = Deferred()
        loop = self.connection_pool.get_event_loop()
        loop.call_soon_threadsafe(self._close, d)
        return d
Ejemplo n.º 15
0
 def _close(self, future):
     if self._connection:
         d = Deferred()
         exc_info = None
         try:
             yield self._unsubscribe((), d)
             yield d
         except Exception:
             exc_info = sys.exc_info()
         self._connection.close()
         self._reset()
         future.callback(exc_info)
Ejemplo n.º 16
0
 def testDeferredErrorbackInGenerator(self):
     d = Deferred()
     ioloop = IOLoop()
     # Add a callback which returns a deferred
     rd = Cbk()
     d.add_callback(lambda r : rd.add(r))
     d.add_callback(lambda r : r + ('second',))
     def _gen():
         yield d
     a = make_async(_gen())
     result = d.callback('ciao')
     self.assertTrue(d.called)
     self.assertEqual(d.paused, 1)
     # The generator has added its consume callback
     self.assertEqual(len(d._callbacks), 2)
     self.assertEqual(len(rd._callbacks), 1)
     #
     self.assertEqual(rd.r, ('ciao',))
     self.assertFalse(a.called)
     #
     # set Error back
     rd.set_error()
     self.assertFalse(d.paused)
     self.assertTrue(a.called)
     self.assertTrue(is_failure(d.result))
Ejemplo n.º 17
0
 def test_then1(self):
     a = Deferred()
     d1 = Deferred()
     d2 = d1.then().add_callback(lambda r: a.callback(r+2))
     d1.callback(1)
     self.assertEqual(a.result, 3)
     self.assertEqual(d1.result, 1)
     self.assertEqual(d2.result, 3)
Ejemplo n.º 18
0
 def _execute(self, command, *args):
     if not self._connection:
         client = self.client()
         pool = client.connection_pool
         consumer = yield pool.request(client,
                                       'ping', (),
                                       release_connection=False).on_finished
         self.parser = consumer._request.parser
         connection = consumer.connection
         connection.set_consumer(None)
         connection.set_consumer(self)
         self.fire_event('pre_request')
     d = Deferred()
     yield self._connection_msg(command, args, d)
Ejemplo n.º 19
0
    def subscribe(self, *channels):
        '''Subscribe to a list of ``channels`` or ``channel patterns``.

        It returns an asynchronous component which results in the number of
        channels this handler is subscribed to.

        If this is the first time the method is called by this handler,
        than the :class:`PubSub` starts listening for messages which
        are fired via the ``on_message`` event.
        '''
        d = Deferred()
        loop = self.connection_pool.get_event_loop()
        loop.call_soon_threadsafe(self._subscribe, channels, d)
        return d
Ejemplo n.º 20
0
 def testUpdate(self):
     d1 = Deferred()
     d2 = Deferred()
     d = MultiDeferred()
     d.update((d1,d2)).lock()
     d1.callback('first')
     d2.callback('second')
     self.assertTrue(d.called)
     self.assertEqual(d.result,['first','second'])
Ejemplo n.º 21
0
 def testMulti(self):
     d = MultiDeferred()
     d1 = Deferred()
     d2 = Deferred()
     d.append(d1)
     d.append(d2)
     d.append('bla')
     self.assertRaises(RuntimeError, d._finish)
     d.lock()
     self.assertRaises(RuntimeError, d._finish)
     self.assertRaises(RuntimeError, d.lock)
     self.assertRaises(RuntimeError, d.append, d1)
     self.assertFalse(d.called)
     d2.callback('first')
     self.assertFalse(d.called)
     d1.callback('second')
     self.assertTrue(d.called)
     self.assertEqual(d.result,['second', 'first', 'bla'])
Ejemplo n.º 22
0
 def testDeferredCallbackInGenerator(self):
     d = Deferred()
     ioloop = IOLoop()
     # Add a callback which returns a deferred
     rd = Cbk()
     d.add_callback(lambda r : rd.add(r))
     d.add_callback(lambda r : r + ('second',))
     def _gen():
         yield d
     a = make_async(_gen())
     result = d.callback('ciao')
     self.assertTrue(d.called)
     self.assertEqual(d.paused,1)
     self.assertEqual(len(d._callbacks), 2)
     self.assertEqual(len(rd._callbacks), 1)
     #
     self.assertEqual(rd.r, ('ciao',))
     self.assertFalse(a.called)
     #
     # set callback
     rd.set_result('luca')
     self.assertTrue(a.called)
     self.assertFalse(d.paused)
     self.assertEqual(d.result,('ciao','luca','second'))
Ejemplo n.º 23
0
class StreamReader:
    _expect_sent = None
    _waiting = None

    def __init__(self, headers, parser, transport=None):
        self.headers = headers
        self.parser = parser
        self.transport = transport
        self.buffer = b''
        self.on_message_complete = Deferred()

    def __repr__(self):
        return repr(self.transport)

    __str__ = __repr__

    def done(self):
        '''``True`` when the full HTTP message has been read.
        '''
        return self.on_message_complete.done()

    def protocol(self):
        version = self.parser.get_version()
        return "HTTP/%s" % ".".join(('%s' % v for v in version))

    def waiting_expect(self):
        '''``True`` when the client is waiting for 100 Continue.
        '''
        if self._expect_sent is None:
            if (not self.parser.is_message_complete()
                    and self.headers.has('expect', '100-continue')):
                return True
            self._expect_sent = ''
        return False

    def recv(self):
        '''Read bytes in the buffer.
        '''
        if self.waiting_expect():
            if self.parser.get_version() < (1, 1):
                raise HttpException(status=417)
            else:
                msg = '%s 100 Continue\r\n\r\n' % self.protocol()
                self._expect_sent = msg
                self.transport.write(msg.encode(DEFAULT_CHARSET))
        return self.parser.recv_body()

    def read(self, maxbuf=None):
        '''Return bytes in the buffer.

        If the stream is not yet ready, return a :class:`pulsar.Deferred`
        which results in the bytes read.
        '''
        if not self._waiting:
            body = self.recv()
            if self.done():
                return self._getvalue(body, maxbuf)
            else:
                self._waiting = self.on_message_complete.then()
                return self._waiting.add_callback(
                    lambda r: self._getvalue(body, maxbuf))
        else:
            return self._waiting

    def fail(self):
        if self.waiting_expect():
            raise HttpException(status=417)

    ##    INTERNALS
    def _getvalue(self, body, maxbuf):
        if self.buffer:
            body = self.buffer + body
        body = body + self.recv()
        if maxbuf and len(body) > maxbuf:
            body, self.buffer = body[:maxbuf], body[maxbuf:]
        return body

    def data_processed(self, protocol, data=None):
        '''Callback by the protocol when new body data is received.'''
        if self.parser.is_message_complete():
            self.on_message_complete.callback(None)
Ejemplo n.º 24
0
 def __init__(self, headers, parser, transport=None):
     self.headers = headers
     self.parser = parser
     self.transport = transport
     self.buffer = b''
     self.on_message_complete = Deferred()
Ejemplo n.º 25
0
class StreamReader:
    _expect_sent = None
    _waiting = None

    def __init__(self, headers, parser, transport=None):
        self.headers = headers
        self.parser = parser
        self.transport = transport
        self.buffer = b''
        self.on_message_complete = Deferred()

    def __repr__(self):
        return repr(self.transport)
    __str__ = __repr__

    def done(self):
        '''``True`` when the full HTTP message has been read.
        '''
        return self.on_message_complete.done()

    def protocol(self):
        version = self.parser.get_version()
        return "HTTP/%s" % ".".join(('%s' % v for v in version))

    def waiting_expect(self):
        '''``True`` when the client is waiting for 100 Continue.
        '''
        if self._expect_sent is None:
            if (not self.parser.is_message_complete() and
                    self.headers.has('expect', '100-continue')):
                return True
            self._expect_sent = ''
        return False

    def recv(self):
        '''Read bytes in the buffer.
        '''
        if self.waiting_expect():
            if self.parser.get_version() < (1, 1):
                raise HttpException(status=417)
            else:
                msg = '%s 100 Continue\r\n\r\n' % self.protocol()
                self._expect_sent = msg
                self.transport.write(msg.encode(DEFAULT_CHARSET))
        return self.parser.recv_body()

    def read(self, maxbuf=None):
        '''Return bytes in the buffer.

        If the stream is not yet ready, return a :class:`pulsar.Deferred`
        which results in the bytes read.
        '''
        if not self._waiting:
            body = self.recv()
            if self.done():
                return self._getvalue(body, maxbuf)
            else:
                self._waiting = self.on_message_complete.then()
                return self._waiting.add_callback(
                    lambda r: self._getvalue(body, maxbuf))
        else:
            return self._waiting

    def fail(self):
        if self.waiting_expect():
            raise HttpException(status=417)

    ##    INTERNALS
    def _getvalue(self, body, maxbuf):
        if self.buffer:
            body = self.buffer + body
        body = body + self.recv()
        if maxbuf and len(body) > maxbuf:
            body, self.buffer = body[:maxbuf], body[maxbuf:]
        return body

    def data_processed(self, protocol, data=None):
        '''Callback by the protocol when new body data is received.'''
        if self.parser.is_message_complete():
            self.on_message_complete.callback(None)
Ejemplo n.º 26
0
 def __init__(self, *args, **kwargs):
     Deferred.__init__(self)
     connection.RedisRequest.__init__(self, *args, **kwargs)
Ejemplo n.º 27
0
def async_func(loop, value):
    p = Deferred()
    loop.call_later(0.01, p.callback, value)
    return p
Ejemplo n.º 28
0
 def __init__(self, headers, parser, transport=None):
     self.headers = headers
     self.parser = parser
     self.transport = transport
     self.buffer = b''
     self.on_message_complete = Deferred()
Ejemplo n.º 29
0
 def test_coroutine(self):
     loop = new_event_loop(iothreadloop=False)
     d = Deferred()
     loop.call_soon(main, d, loop, 1)
     loop.run_until_complete(d)
     self.assertEqual(d.result, 9)