Example #1
0
 def test_read(self):
     reader = StreamReader()
     reader.feed(b'foo')
     self.assertEqual(reader.read(3), b'foo')
     reader.feed(b'foo bar')
     self.assertEqual(reader.read(3), b'foo')
     self.assertEqual(reader.read(4), b' bar')
Example #2
0
 def test_readline(self):
     reader = StreamReader()
     reader.feed(b'foo\n')
     self.assertEqual(reader.readline(), b'foo\n')
     reader.feed(b'bar\nbaz\n')
     self.assertEqual(reader.readline(), b'bar\n')
     self.assertEqual(reader.readline(), b'baz\n')
Example #3
0
 def test_read_incr(self):
     reader = StreamReader()
     buf = b'foobar'
     for i in range(len(buf)):
         reader.feed(buf[i:i + 1])
     reader.feed_eof()
     self.assertEqual(reader.read(), b'foobar')
Example #4
0
 def test_read_eof(self):
     reader = StreamReader()
     reader.feed(b'foo')
     reader.feed_eof()
     self.assertEqual(reader.read(), b'foo')
     self.assertEqual(reader.read(), b'')
     reader.feed_error(RuntimeError)
Example #5
0
 def test_readline_incr(self):
     reader = StreamReader()
     buf = b'foo\nbar\n'
     for i in range(len(buf)):
         reader.feed(buf[i:i + 1])
     self.assertEqual(reader.readline(), b'foo\n')
     self.assertEqual(reader.readline(), b'bar\n')
Example #6
0
 def test_iter_eof(self):
     reader = StreamReader()
     reader.feed(b'foo\nbar\n')
     reader.feed_eof()
     it = iter(reader)
     self.assertEqual(six.next(it), b'foo\n')
     self.assertEqual(six.next(it), b'bar\n')
     self.assertRaises(StopIteration, six.next, it)
Example #7
0
 def test_partial_decode_at_eof(self):
     reader = StreamReader()
     wrapped = TextIOWrapper(reader, 'utf-8')
     # \u20ac is the euro sign in case you wondered..
     buf = u'20 \u20ac'.encode('utf-8')
     reader.feed(buf[:-1])
     reader.feed_eof()
     self.assertRaises(UnicodeDecodeError, wrapped.read, 4)
Example #8
0
 def test_iter_error(self):
     reader = StreamReader()
     reader.feed(b'foo\nbar\n')
     reader.feed_error(RuntimeError)
     it = iter(reader)
     self.assertEqual(six.next(it), b'foo\n')
     self.assertEqual(six.next(it), b'bar\n')
     self.assertRaises(RuntimeError, six.next, it)
Example #9
0
    def test_partial_decode_wait(self):
        reader = StreamReader()
        wrapped = TextIOWrapper(reader, 'utf-8')
        buf = u'20 \u20ac'.encode('utf-8')
        reader.feed(buf[:-1])

        def write_last():
            gruvi.sleep(0.01)
            reader.feed(buf[-1:])

        gruvi.spawn(write_last)
        self.assertEqual(wrapped.read(4), u'20 \u20ac')
Example #10
0
    def test_readlines_wait_eof(self):
        reader = StreamReader()
        reader.feed(b'foo\n')

        def write_more():
            gruvi.sleep(0.01)
            reader.feed(b'bar\n')
            gruvi.sleep(0.01)
            reader.feed_eof()

        gruvi.spawn(write_more)
        self.assertEqual(reader.readlines(), [b'foo\n', b'bar\n'])
        self.assertEqual(reader.readlines(), [])
Example #11
0
    def test_read_wait_error(self):
        reader = StreamReader()
        reader.feed(b'foo')

        def write_more():
            gruvi.sleep(0.01)
            reader.feed(b'bar')
            gruvi.sleep(0.01)
            reader.feed_error(RuntimeError)

        gruvi.spawn(write_more)
        self.assertEqual(reader.read(), b'foobar')
        self.assertRaises(RuntimeError, reader.read)
Example #12
0
    def test_read_wait(self):
        reader = StreamReader()
        reader.feed(b'foo')

        def write_more():
            gruvi.sleep(0.01)
            reader.feed(b'bar')
            gruvi.sleep(0.01)
            reader.feed_eof()

        gruvi.spawn(write_more)
        self.assertEqual(reader.read(), b'foobar')
        self.assertEqual(reader.read(), b'')
Example #13
0
    def test_iter_wait_error(self):
        reader = StreamReader()
        reader.feed(b'foo\n')

        def write_more():
            gruvi.sleep(0.01)
            reader.feed(b'bar\n')
            gruvi.sleep(0.01)
            reader.feed_error(RuntimeError)

        gruvi.spawn(write_more)
        it = iter(reader)
        self.assertEqual(six.next(it), b'foo\n')
        self.assertEqual(six.next(it), b'bar\n')
        self.assertRaises(RuntimeError, six.next, it)
Example #14
0
    def test_iter_wait_eof(self):
        reader = StreamReader()
        reader.feed(b'foo\n')

        def write_more():
            gruvi.sleep(0.01)
            reader.feed(b'bar\n')
            gruvi.sleep(0.01)
            reader.feed_eof()

        gruvi.spawn(write_more)
        it = iter(reader)
        self.assertEqual(six.next(it), b'foo\n')
        self.assertEqual(six.next(it), b'bar\n')
        self.assertRaises(StopIteration, six.next, it)
Example #15
0
 def test_readline_limit(self):
     reader = StreamReader()
     reader.feed(b'foobar\n')
     self.assertEqual(reader.readline(3), b'foo')
     self.assertEqual(reader.readline(), b'bar\n')
Example #16
0
 def test_readline_error(self):
     reader = StreamReader()
     reader.feed(b'foo')
     reader.feed_error(RuntimeError)
     self.assertEqual(reader.readline(), b'foo')
     self.assertRaises(RuntimeError, reader.readline)
Example #17
0
 def test_readlines_limit(self):
     reader = StreamReader()
     reader.feed(b'foo\nbar\n')
     self.assertEqual(reader.readlines(4), [b'foo\n', b'bar\n'])
Example #18
0
 def test_readlines_eof(self):
     reader = StreamReader()
     reader.feed(b'foo\nbar\n')
     reader.feed_eof()
     self.assertEqual(reader.readlines(), [b'foo\n', b'bar\n'])
     self.assertEqual(reader.readlines(), [])
Example #19
0
 def test_readlines_error(self):
     reader = StreamReader()
     reader.feed(b'foo\nbar\n')
     reader.feed_error(RuntimeError)
     self.assertEqual(reader.readlines(), [b'foo\n', b'bar\n'])
     self.assertRaises(RuntimeError, reader.readlines)
Example #20
0
 def test_read_eof(self):
     reader = StreamReader()
     wrapped = TextIOWrapper(reader, 'utf-8')
     reader.feed(b'foo')
     reader.feed_eof()
     self.assertEqual(wrapped.read(), 'foo')
Example #21
0
 def test_simple(self):
     reader = StreamReader()
     wrapped = TextIOWrapper(reader, 'utf-8')
     reader.feed(b'foo')
     self.assertEqual(wrapped.read(3), 'foo')
Example #22
0
 def test_readline_eof(self):
     reader = StreamReader()
     reader.feed(b'foo')
     reader.feed_eof()
     self.assertEqual(reader.readline(), b'foo')
     self.assertEqual(reader.readline(), b'')