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)
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)
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)
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)
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'])
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'}))
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')
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')
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))
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'])
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)
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'))
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)