Skip to content

Commit

Permalink
Ensure that select returns True after end of file.
Browse files Browse the repository at this point in the history
This is because the next read will immediately return b''.
  • Loading branch information
s-ball committed Nov 12, 2020
1 parent ff176dd commit 3808d71
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.6.0: Ensure that select returns True after end of file.

0.5.3: Fix TROVE classifiers

0.5.2: Improve README file
Expand Down
7 changes: 6 additions & 1 deletion non_blocking_io_wrapper/non_blocking_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,17 @@ def __del__(self):
super().__del__()

def select(self, timeout=None):
"""Waits for data to become available.
"""Waits for data to become available, or more exactly for the
next read not to return None. Specifically, it returns True after
end of file has been reached, because the next read will
immediately return b''.
If timeout is given and is not None it is the maximum time in
seconds to wait.
Returns True if it succeeded or False on timeout.
"""
if self.ended:
return True
self.available.wait(timeout)
with self.lock:
return 0 != len(self.data)
9 changes: 9 additions & 0 deletions test/test_non_blocking_io_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ def test_select_ok(self):
end = time.time()
self.assertEqual(b'a', self.fd.read())
self.assertTrue(.05 < end-beg < .15)

def test_select_eof(self):
self.fd.drain()
self.assertEqual(b'ab\ncd', self.fd.read())
beg = time.time()
self.assertTrue(self.fd.select(.1))
end = time.time()
self.assertTrue(end-beg < .05)
self.assertEqual(b'', self.fd.read())

0 comments on commit 3808d71

Please sign in to comment.