class LineStream:
        """
        A class to represent the lines of kernel output.
-       Provides a peek()/pop() interface over an iterator of
+       Provides a lazy peek()/pop() interface over an iterator of
        (line#, text).
        """
        _lines: Iterator[Tuple[int, str]]
        _next: Tuple[int, str]
+       _need_next: bool
        _done: bool
 
        def __init__(self, lines: Iterator[Tuple[int, str]]):
                """Creates a new LineStream that wraps the given iterator."""
                self._lines = lines
                self._done = False
+               self._need_next = True
                self._next = (0, '')
-               self._get_next()
 
        def _get_next(self) -> None:
-               """Advances the LineSteam to the next line."""
+               """Advances the LineSteam to the next line, if necessary."""
+               if not self._need_next:
+                       return
                try:
                        self._next = next(self._lines)
                except StopIteration:
                        self._done = True
+               finally:
+                       self._need_next = False
 
        def peek(self) -> str:
                """Returns the current line, without advancing the LineStream.
                """
+               self._get_next()
                return self._next[1]
 
        def pop(self) -> str:
                """Returns the current line and advances the LineStream to
                the next line.
                """
-               n = self._next
-               self._get_next()
-               return n[1]
+               s = self.peek()
+               if self._done:
+                       raise ValueError(f'LineStream: going past EOF, last line was {s}')
+               self._need_next = True
+               return s
 
        def __bool__(self) -> bool:
                """Returns True if stream has more lines."""
+               self._get_next()
                return not self._done
 
        # Only used by kunit_tool_test.py.
 
        def line_number(self) -> int:
                """Returns the line number of the current line."""
+               self._get_next()
                return self._next[0]
 
 # Parsing helper methods:
 
 
 import itertools
 import json
+import os
 import signal
 import subprocess
-import os
+from typing import Iterable
 
 import kunit_config
 import kunit_parser
                                result.status)
                        self.assertEqual('kunit-resource-test', result.test.subtests[0].name)
 
+def line_stream_from_strs(strs: Iterable[str]) -> kunit_parser.LineStream:
+       return kunit_parser.LineStream(enumerate(strs, start=1))
+
+class LineStreamTest(unittest.TestCase):
+
+       def test_basic(self):
+               stream = line_stream_from_strs(['hello', 'world'])
+
+               self.assertTrue(stream, msg='Should be more input')
+               self.assertEqual(stream.line_number(), 1)
+               self.assertEqual(stream.peek(), 'hello')
+               self.assertEqual(stream.pop(), 'hello')
+
+               self.assertTrue(stream, msg='Should be more input')
+               self.assertEqual(stream.line_number(), 2)
+               self.assertEqual(stream.peek(), 'world')
+               self.assertEqual(stream.pop(), 'world')
+
+               self.assertFalse(stream, msg='Should be no more input')
+               with self.assertRaisesRegex(ValueError, 'LineStream: going past EOF'):
+                       stream.pop()
+
+       def test_is_lazy(self):
+               called_times = 0
+               def generator():
+                       nonlocal called_times
+                       for i in range(1,5):
+                               called_times += 1
+                               yield called_times, str(called_times)
+
+               stream = kunit_parser.LineStream(generator())
+               self.assertEqual(called_times, 0)
+
+               self.assertEqual(stream.pop(), '1')
+               self.assertEqual(called_times, 1)
+
+               self.assertEqual(stream.pop(), '2')
+               self.assertEqual(called_times, 2)
+
 class LinuxSourceTreeTest(unittest.TestCase):
 
        def setUp(self):