vomebook commited on
Commit
012bf23
·
1 Parent(s): b379539

Assert OCR shutdown waits for task cleanup

Browse files
Files changed (1) hide show
  1. tests/test_async_proxy.py +24 -3
tests/test_async_proxy.py CHANGED
@@ -844,6 +844,8 @@ class AsyncProxyTests(unittest.IsolatedAsyncioTestCase):
844
 
845
  async def test_bucket_shutdown_cancels_and_drains_owned_ocr_tasks(self):
846
  started = [asyncio.Event(), asyncio.Event()]
 
 
847
  finished = []
848
 
849
  async def held(index):
@@ -851,8 +853,13 @@ class AsyncProxyTests(unittest.IsolatedAsyncioTestCase):
851
  try:
852
  await asyncio.Event().wait()
853
  finally:
 
 
854
  finished.append(index)
855
 
 
 
 
856
  tasks = [asyncio.create_task(held(index)) for index in range(2)]
857
  state = app.app.state
858
  state.reader_ocr_inflight = {'synthetic-1': tasks[0], 'synthetic-2': tasks[1]}
@@ -860,9 +867,21 @@ class AsyncProxyTests(unittest.IsolatedAsyncioTestCase):
860
  state.reader_bucket_cache_bytes = 3
861
  state.reader_ocr_cache['synthetic'] = ('digest', {}, 7)
862
  state.reader_ocr_cache_bytes = 7
 
 
863
  try:
864
  await asyncio.wait_for(asyncio.gather(*(event.wait() for event in started)), 1)
865
- await asyncio.wait_for(app.shutdown_reader_bucket_state(app.app), 1)
 
 
 
 
 
 
 
 
 
 
866
  self.assertCountEqual(finished, [0, 1])
867
  self.assertTrue(all(task.cancelled() for task in tasks))
868
  self.assertEqual(state.reader_ocr_inflight, {})
@@ -871,10 +890,12 @@ class AsyncProxyTests(unittest.IsolatedAsyncioTestCase):
871
  self.assertEqual(state.reader_bucket_cache, {})
872
  self.assertEqual(state.reader_bucket_cache_bytes, 0)
873
  finally:
874
- for task in tasks:
 
 
875
  if not task.done():
876
  task.cancel()
877
- await asyncio.gather(*tasks, return_exceptions=True)
878
 
879
  async def test_reader_content_routes_both_v2_bucket_origins_to_proxy(self):
880
  request = SimpleNamespace(method="GET", headers={"Range": "bytes=0-1"})
 
844
 
845
  async def test_bucket_shutdown_cancels_and_drains_owned_ocr_tasks(self):
846
  started = [asyncio.Event(), asyncio.Event()]
847
+ cleaning = [asyncio.Event(), asyncio.Event()]
848
+ release = asyncio.Event()
849
  finished = []
850
 
851
  async def held(index):
 
853
  try:
854
  await asyncio.Event().wait()
855
  finally:
856
+ cleaning[index].set()
857
+ await release.wait()
858
  finished.append(index)
859
 
860
+ async def await_cleanups():
861
+ await asyncio.gather(*(event.wait() for event in cleaning))
862
+
863
  tasks = [asyncio.create_task(held(index)) for index in range(2)]
864
  state = app.app.state
865
  state.reader_ocr_inflight = {'synthetic-1': tasks[0], 'synthetic-2': tasks[1]}
 
867
  state.reader_bucket_cache_bytes = 3
868
  state.reader_ocr_cache['synthetic'] = ('digest', {}, 7)
869
  state.reader_ocr_cache_bytes = 7
870
+ shutdown = None
871
+ cleanups = None
872
  try:
873
  await asyncio.wait_for(asyncio.gather(*(event.wait() for event in started)), 1)
874
+ shutdown = asyncio.create_task(app.shutdown_reader_bucket_state(app.app))
875
+ cleanups = asyncio.create_task(await_cleanups())
876
+ await asyncio.wait({shutdown, cleanups}, timeout=1, return_when=asyncio.FIRST_COMPLETED)
877
+ self.assertTrue(all(event.is_set() for event in cleaning), 'shutdown must cancel each OCR task')
878
+ self.assertFalse(shutdown.done(), 'shutdown must await OCR task cleanup')
879
+ self.assertEqual(state.reader_ocr_inflight, {})
880
+ release.set()
881
+ try:
882
+ await asyncio.wait_for(shutdown, 1)
883
+ except asyncio.TimeoutError:
884
+ self.fail('shutdown must finish after OCR task cleanup')
885
  self.assertCountEqual(finished, [0, 1])
886
  self.assertTrue(all(task.cancelled() for task in tasks))
887
  self.assertEqual(state.reader_ocr_inflight, {})
 
890
  self.assertEqual(state.reader_bucket_cache, {})
891
  self.assertEqual(state.reader_bucket_cache_bytes, 0)
892
  finally:
893
+ release.set()
894
+ owned = tasks + [task for task in (shutdown, cleanups) if task is not None]
895
+ for task in owned:
896
  if not task.done():
897
  task.cancel()
898
+ await asyncio.gather(*owned, return_exceptions=True)
899
 
900
  async def test_reader_content_routes_both_v2_bucket_origins_to_proxy(self):
901
  request = SimpleNamespace(method="GET", headers={"Range": "bytes=0-1"})