def test_exception(self): buffer = parsers.DataBuffer(loop=self.loop) self.assertIsNone(buffer.exception()) exc = ValueError() buffer.set_exception(exc) self.assertIs(buffer.exception(), exc)
def test_read_exception(self): buffer = parsers.DataBuffer(loop=self.loop) buffer.feed_data(object()) buffer.set_exception(ValueError()) self.assertRaises( ValueError, self.loop.run_until_complete, buffer.read())
def test_exception_waiter(self): stream = parsers.StreamBuffer() stream._parser = parsers.lines_parser() buf = stream._parser_buffer = parsers.DataBuffer(loop=self.loop) exc = ValueError() stream.set_exception(exc) self.assertIs(buf.exception(), exc)
def test_read_eof(self): buffer = parsers.DataBuffer(loop=self.loop) read_task = tasks.Task(buffer.read(), loop=self.loop) def cb(): buffer.feed_eof() self.loop.call_soon(cb) data = self.loop.run_until_complete(read_task) self.assertIsNone(data)
def test_read_until_eof(self): item = object() buffer = parsers.DataBuffer(loop=self.loop) buffer.feed_data(item) buffer.feed_eof() data = self.loop.run_until_complete(buffer.read()) self.assertIs(data, item) data = self.loop.run_until_complete(buffer.read()) self.assertIsNone(data)
def test_read(self): item = object() buffer = parsers.DataBuffer(loop=self.loop) read_task = tasks.Task(buffer.read(), loop=self.loop) def cb(): buffer.feed_data(item) self.loop.call_soon(cb) data = self.loop.run_until_complete(read_task) self.assertIs(item, data)
def test_exception_waiter(self): buffer = parsers.DataBuffer(loop=self.loop) @tasks.coroutine def set_err(): buffer.set_exception(ValueError()) t1 = tasks.Task(buffer.read(), loop=self.loop) t2 = tasks.Task(set_err(), loop=self.loop) self.loop.run_until_complete(tasks.wait([t1, t2], loop=self.loop)) self.assertRaises(ValueError, t1.result)
def test_chunks_parser(self): out = parsers.DataBuffer(loop=self.loop) buf = self._make_one() p = parsers.chunks_parser(5) next(p) p.send((out, buf)) for d in (b'line1', b'lin', b'e2d', b'ata'): p.send(d) self.assertEqual( [bytearray(b'line1'), bytearray(b'line2')], list(out._buffer)) try: p.throw(parsers.EofStream()) except parsers.EofStream: pass self.assertEqual(bytes(buf), b'data')
def test_feed_eof(self): buffer = parsers.DataBuffer(loop=self.loop) buffer.feed_eof() self.assertTrue(buffer._eof)
def test_feed_data(self): buffer = parsers.DataBuffer(loop=self.loop) item = object() buffer.feed_data(item) self.assertEqual([item], list(buffer._buffer))