test_util_modules.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import asyncio
  2. import pytest
  3. import hivemind
  4. from concurrent.futures import CancelledError
  5. def test_mpfuture_result():
  6. f1, f2 = hivemind.MPFuture.make_pair()
  7. f1.set_result(321)
  8. assert f2.result() == 321
  9. assert f1.result() == 321
  10. for future in [f1, f2]:
  11. with pytest.raises(RuntimeError):
  12. future.set_result(123)
  13. with pytest.raises(RuntimeError):
  14. future.set_exception(ValueError())
  15. assert future.cancel() is False
  16. assert future.done() and not future.running() and not future.cancelled()
  17. f1, f2 = hivemind.MPFuture.make_pair()
  18. with pytest.raises(TimeoutError):
  19. f1.result(timeout=1e-3)
  20. f2.set_result(['abacaba', 123])
  21. assert f1.result() == ['abacaba', 123]
  22. def test_mpfuture_exception():
  23. f1, f2 = hivemind.MPFuture.make_pair()
  24. with pytest.raises(TimeoutError):
  25. f1.exception(timeout=1e-3)
  26. f2.set_exception(NotImplementedError())
  27. for future in [f1, f2]:
  28. assert isinstance(future.exception(), NotImplementedError)
  29. with pytest.raises(NotImplementedError):
  30. future.result()
  31. assert future.cancel() is False
  32. assert future.done() and not future.running() and not future.cancelled()
  33. def test_mpfuture_cancel():
  34. f1, f2 = hivemind.MPFuture.make_pair()
  35. assert not f2.cancelled()
  36. f1.cancel()
  37. for future in [f1, f2]:
  38. with pytest.raises(CancelledError):
  39. future.result()
  40. with pytest.raises(CancelledError):
  41. future.exception()
  42. with pytest.raises(RuntimeError):
  43. future.set_result(123)
  44. with pytest.raises(RuntimeError):
  45. future.set_exception(NotImplementedError)
  46. assert future.cancelled() and future.done() and not future.running()
  47. def test_mpfuture_status():
  48. f1, f2 = hivemind.MPFuture.make_pair()
  49. assert f1.set_running_or_notify_cancel() is True
  50. for future in [f1, f2]:
  51. assert future.running() and not future.done() and not future.cancelled()
  52. with pytest.raises(RuntimeError):
  53. future.set_running_or_notify_cancel()
  54. f2.cancel()
  55. for future in [f1, f2]:
  56. assert not future.running() and future.done() and future.cancelled()
  57. assert future.set_running_or_notify_cancel() is False
  58. f1, f2 = hivemind.MPFuture.make_pair()
  59. f1.cancel()
  60. for future in [f1, f2]:
  61. assert future.set_running_or_notify_cancel() is False
  62. def test_await_mpfuture():
  63. async def _run():
  64. # await result
  65. f1, f2 = hivemind.MPFuture.make_pair()
  66. async def wait_and_assign():
  67. assert f2.set_running_or_notify_cancel() is True
  68. await asyncio.sleep(0.1)
  69. f2.set_result((123, 'ololo'))
  70. asyncio.create_task(wait_and_assign())
  71. for future in [f1, f2]:
  72. res = await future
  73. assert res == (123, 'ololo')
  74. # await cancel
  75. f1, f2 = hivemind.MPFuture.make_pair()
  76. async def wait_and_cancel():
  77. await asyncio.sleep(0.1)
  78. f1.cancel()
  79. asyncio.create_task(wait_and_cancel())
  80. for future in [f1, f2]:
  81. with pytest.raises(CancelledError):
  82. await future
  83. # await exception
  84. f1, f2 = hivemind.MPFuture.make_pair()
  85. async def wait_and_raise():
  86. await asyncio.sleep(0.1)
  87. f1.set_exception(SystemError())
  88. asyncio.create_task(wait_and_raise())
  89. for future in [f1, f2]:
  90. with pytest.raises(SystemError):
  91. await future
  92. asyncio.new_event_loop().run_until_complete(_run())