test_request_tracker.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import pytest
  2. from aphrodite.common.outputs import RequestOutput
  3. from aphrodite.engine.async_aphrodite import RequestTracker
  4. @pytest.mark.asyncio
  5. async def test_request_tracker():
  6. tracker = RequestTracker()
  7. stream_1 = tracker.add_request("1")
  8. assert tracker.new_requests_event.is_set()
  9. await tracker.wait_for_new_requests()
  10. new, aborted = tracker.get_new_and_aborted_requests()
  11. assert not tracker.new_requests_event.is_set()
  12. assert len(new) == 1
  13. assert new[0]["request_id"] == "1"
  14. assert not aborted
  15. assert not stream_1.finished
  16. stream_2 = tracker.add_request("2")
  17. stream_3 = tracker.add_request("3")
  18. assert tracker.new_requests_event.is_set()
  19. await tracker.wait_for_new_requests()
  20. new, aborted = tracker.get_new_and_aborted_requests()
  21. assert not tracker.new_requests_event.is_set()
  22. assert len(new) == 2
  23. assert new[0]["request_id"] == "2"
  24. assert new[1]["request_id"] == "3"
  25. assert not aborted
  26. assert not stream_2.finished
  27. assert not stream_3.finished
  28. # request_ids must be unique
  29. with pytest.raises(KeyError):
  30. tracker.add_request("1")
  31. assert not tracker.new_requests_event.is_set()
  32. tracker.abort_request("1")
  33. new, aborted = tracker.get_new_and_aborted_requests()
  34. assert len(aborted) == 1
  35. assert "1" in aborted
  36. assert not new
  37. assert stream_1.finished
  38. stream_4 = tracker.add_request("4")
  39. tracker.abort_request("4")
  40. assert tracker.new_requests_event.is_set()
  41. await tracker.wait_for_new_requests()
  42. new, aborted = tracker.get_new_and_aborted_requests()
  43. # aborted new requests will cancel each other out -
  44. # there's no need for them to propagate into the
  45. # engine
  46. assert not aborted
  47. assert not new
  48. assert stream_4.finished
  49. stream_5 = tracker.add_request("5")
  50. assert tracker.new_requests_event.is_set()
  51. tracker.process_request_output(
  52. RequestOutput("2", "output", [], [], [], finished=True))
  53. await tracker.wait_for_new_requests()
  54. new, aborted = tracker.get_new_and_aborted_requests()
  55. assert not tracker.new_requests_event.is_set()
  56. assert not aborted
  57. assert len(new) == 1
  58. assert new[0]["request_id"] == "5"
  59. assert stream_2.finished
  60. assert not stream_5.finished