def test_readlines_eof_and_error(self): stream = Stream(None) stream.buffer.feed(b'foo\nbar\n') stream.buffer.feed_eof() stream.buffer.feed_error(RuntimeError) self.assertEqual(stream.readlines(), [b'foo\n', b'bar\n']) self.assertEqual(stream.readlines(), [])
def test_read_eof_and_error(self): stream = Stream(None) stream.buffer.feed(b'foo') stream.buffer.feed_eof() stream.buffer.feed_error(RuntimeError) self.assertEqual(stream.read(), b'foo') self.assertEqual(stream.read(), b'')
def test_readline(self): stream = Stream(None) stream.buffer.feed(b'foo\n') self.assertEqual(stream.readline(), b'foo\n') stream.buffer.feed(b'bar\nbaz\n') self.assertEqual(stream.readline(), b'bar\n') self.assertEqual(stream.readline(), b'baz\n')
def test_read(self): stream = Stream(None) stream.buffer.feed(b'foo') self.assertEqual(stream.read(3), b'foo') stream.buffer.feed(b'foo bar') self.assertEqual(stream.read(3), b'foo') self.assertEqual(stream.read(4), b' bar')
def test_read_incr(self): stream = Stream(None) buf = b'foobar' for i in range(len(buf)): stream.buffer.feed(buf[i:i + 1]) stream.buffer.feed_eof() self.assertEqual(stream.read(), b'foobar')
def test_readline_incr(self): stream = Stream(None) buf = b'foo\nbar\n' for i in range(len(buf)): stream.buffer.feed(buf[i:i + 1]) self.assertEqual(stream.readline(), b'foo\n') self.assertEqual(stream.readline(), b'bar\n')
def test_read_incr(self): stream = Stream(None) buf = b'foobar' for i in range(len(buf)): stream.buffer.feed(buf[i:i+1]) stream.buffer.feed_eof() self.assertEqual(stream.read(), b'foobar')
def test_write(self): transport = MockTransport() stream = Stream(transport) wrapped = stream.wrap('utf-8') wrapped.write(u'foo') wrapped.flush() self.assertEqual(transport.buffer.getvalue(), b'foo')
def test_readline_incr(self): stream = Stream(None) buf = b'foo\nbar\n' for i in range(len(buf)): stream.buffer.feed(buf[i:i+1]) self.assertEqual(stream.readline(), b'foo\n') self.assertEqual(stream.readline(), b'bar\n')
def test_partial_decode_at_eof(self): stream = Stream(None) wrapped = stream.wrap('utf-8') # \u20ac is the euro sign in case you wondered.. buf = u'20 \u20ac'.encode('utf-8') stream.buffer.feed(buf[:-1]) stream.buffer.feed_eof() self.assertRaises(UnicodeDecodeError, wrapped.read, 4)
def test_partial_decode_wait(self): stream = Stream(None) wrapped = stream.wrap('utf-8') buf = u'20 \u20ac'.encode('utf-8') stream.buffer.feed(buf[:-1]) def write_last_byte(): gruvi.sleep(0.01) stream.buffer.feed(buf[-1:]) gruvi.spawn(write_last_byte) self.assertEqual(wrapped.read(4), u'20 \u20ac')
def test_read_wait(self): stream = Stream(None) stream.buffer.feed(b'foo') def write_more(): gruvi.sleep(0.01) stream.buffer.feed(b'bar') gruvi.sleep(0.01) stream.buffer.feed_eof() gruvi.spawn(write_more) self.assertEqual(stream.read(), b'foobar') self.assertEqual(stream.read(), b'')
def test_readlines_wait_eof(self): stream = Stream(None) stream.buffer.feed(b'foo\n') def write_more(): gruvi.sleep(0.01) stream.buffer.feed(b'bar\n') gruvi.sleep(0.01) stream.buffer.feed_eof() gruvi.spawn(write_more) self.assertEqual(stream.readlines(), [b'foo\n', b'bar\n']) self.assertEqual(stream.readlines(), [])
def test_read_wait_error(self): stream = Stream(None) stream.buffer.feed(b'foo') def write_more(): gruvi.sleep(0.01) stream.buffer.feed(b'bar') gruvi.sleep(0.01) stream.buffer.feed_error(RuntimeError) gruvi.spawn(write_more) self.assertEqual(stream.read(), b'foobar') self.assertRaises(RuntimeError, stream.read)
def test_iter_eof(self): stream = Stream(None) stream.buffer.feed(b'foo\nbar\n') stream.buffer.feed_eof() it = iter(stream) self.assertEqual(six.next(it), b'foo\n') self.assertEqual(six.next(it), b'bar\n') self.assertRaises(StopIteration, six.next, it)
def test_iter_error(self): stream = Stream(None) stream.buffer.feed(b'foo\nbar\n') stream.buffer.feed_error(RuntimeError) it = iter(stream) self.assertEqual(six.next(it), b'foo\n') self.assertEqual(six.next(it), b'bar\n') self.assertRaises(RuntimeError, six.next, it)
def test_partial_decode_at_eof(self): stream = Stream(None) wrapped = TextIOWrapper(stream, 'utf-8') # \u20ac is the euro sign in case you wondered.. buf = u'20 \u20ac'.encode('utf-8') stream.buffer.feed(buf[:-1]) stream.buffer.feed_eof() self.assertRaises(UnicodeDecodeError, wrapped.read, 4)
def test_partial_decode_wait(self): stream = Stream(None) wrapped = TextIOWrapper(stream, 'utf-8') buf = u'20 \u20ac'.encode('utf-8') stream.buffer.feed(buf[:-1]) def write_last(): gruvi.sleep(0.01) stream.buffer.feed(buf[-1:]) gruvi.spawn(write_last) self.assertEqual(wrapped.read(4), u'20 \u20ac')
def test_iter_wait_error(self): stream = Stream(None) stream.buffer.feed(b'foo\n') def write_more(): gruvi.sleep(0.01) stream.buffer.feed(b'bar\n') gruvi.sleep(0.01) stream.buffer.feed_error(RuntimeError) gruvi.spawn(write_more) it = iter(stream) self.assertEqual(six.next(it), b'foo\n') self.assertEqual(six.next(it), b'bar\n') self.assertRaises(RuntimeError, six.next, it)
def test_write_through(self): transport = MockTransport() stream = Stream(transport) stream.write(b'foo') self.assertEqual(transport.buffer.getvalue(), b'foo')
def test_simple(self): stream = Stream(None) wrapped = stream.wrap('utf-8') stream.buffer.feed(b'foo') self.assertEqual(wrapped.read(3), 'foo')
def test_read_eof(self): stream = Stream(None) wrapped = stream.wrap('utf-8') stream.buffer.feed(b'foo') stream.buffer.feed_eof() self.assertEqual(wrapped.read(), 'foo')
def test_read_eof(self): stream = Stream(None) stream.buffer.feed(b'foo') stream.buffer.feed_eof() self.assertEqual(stream.read(), b'foo') self.assertEqual(stream.read(), b'')
def test_writelines(self): transport = MockTransport() stream = Stream(transport) wrapped = stream.wrap('utf-8') wrapped.writelines([u'foo', u'bar']) self.assertEqual(transport.buffer.getvalue(), b'foobar')
def test_readline_limit(self): stream = Stream(None) stream.buffer.feed(b'foobar\n') self.assertEqual(stream.readline(3), b'foo') self.assertEqual(stream.readline(), b'bar\n')
def test_readline_error(self): stream = Stream(None) stream.buffer.feed(b'foo') stream.buffer.feed_error(RuntimeError) self.assertEqual(stream.readline(), b'foo') self.assertRaises(RuntimeError, stream.readline)
def test_readlines_limit(self): stream = Stream(None) stream.buffer.feed(b'foo\nbar\n') self.assertEqual(stream.readlines(4), [b'foo\n', b'bar\n'])
def test_read_eof(self): stream = Stream(None) wrapped = TextIOWrapper(stream, 'utf-8') stream.buffer.feed(b'foo') stream.buffer.feed_eof() self.assertEqual(wrapped.read(), 'foo')
def test_writelines(self): transport = MockTransport() stream = Stream(transport) stream.writelines([b'foo', b'bar']) self.assertEqual(transport.buffer.getvalue(), b'foobar')