Skip to content

Commit

Permalink
Fix flaky test by flushing the progress bar output upon uninstall.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 674041785
  • Loading branch information
daiyip authored and langfun authors committed Sep 12, 2024
1 parent d9ddc38 commit 789c020
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion langfun/core/concurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ def refresh(cls) -> None:
# Process uninstall requests.
if cls._uninstall_requests:
for bar_id in cls._uninstall_requests:
cls._progress_bars.pop(bar_id, None)
bar = cls._progress_bars.pop(bar_id, None)
if bar is not None:
bar.close()
cls._uninstall_requests.clear()


Expand Down Expand Up @@ -765,6 +767,10 @@ def update(self, delta):
def refresh(self) -> None:
"""Refresh progress bar."""

@abc.abstractmethod
def close(self) -> None:
"""Close progress bar."""


class _TqdmProgressControl(_ProgressControl):
"""Tqdm-based progress control."""
Expand All @@ -791,6 +797,9 @@ def refresh(self):
self._tqdm.colour = self.color
self._tqdm.refresh()

def close(self):
self._tqdm.close()


class _ConsoleProgressControl(_ProgressControl):
"""Simple progress control by printing the status to the console."""
Expand Down Expand Up @@ -824,6 +833,9 @@ def refresh(self):
s.write(f' : {status}')
sys.stderr.write(s.getvalue() + '\n')

def close(self):
sys.stderr.flush()


class _NoopProgressControl(_ProgressControl):
"""No-op progress control."""
Expand All @@ -834,6 +846,9 @@ def update(self, delta: int) -> None:
def refresh(self) -> None:
pass

def close(self) -> None:
pass


def _progress_control(
total: int,
Expand Down
2 changes: 1 addition & 1 deletion langfun/core/concurrent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,8 @@ def fun(x):
(3, pg.MISSING_VALUE),
],
)
self.assertIn('100%', string_io.getvalue())
concurrent.ProgressBar.uninstall(bar_id)
self.assertIn('100%', string_io.getvalue())


class ExecutorPoolTest(unittest.TestCase):
Expand Down

0 comments on commit 789c020

Please sign in to comment.