Skip to content

Commit

Permalink
test: adapt to github win runner
Browse files Browse the repository at this point in the history
  • Loading branch information
leavers committed Dec 27, 2023
1 parent ec1e474 commit afa147b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
22 changes: 21 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import time
from typing import Union

from flexexecutor import AsyncPoolExecutor, ThreadPoolExecutor

AnyPoolExecutor = Union[AsyncPoolExecutor, ThreadPoolExecutor]

def alive_threads(executor: Union[AsyncPoolExecutor, ThreadPoolExecutor]):

def alive_threads(executor: AnyPoolExecutor):
return [t for t in executor._threads if t.is_alive()]


def wait_for_alive_threads(
executor: AnyPoolExecutor,
expect: int,
timeout: float,
) -> int:
t = -1
tick = time.monotonic()
while True:
t = len(alive_threads(executor))
if t == expect:
break
if time.monotonic() - tick > timeout:
break
time.sleep(0.05)
return t
5 changes: 2 additions & 3 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pytest_mock import MockerFixture

from flexexecutor import AsyncPoolExecutor
from tests.conftest import alive_threads
from tests.conftest import alive_threads, wait_for_alive_threads


async def simple_return(n: int = 1):
Expand Down Expand Up @@ -141,8 +141,7 @@ def test_finite_timeout():
assert len(alive_threads(executor)) == 1
future.result()

time.sleep(0.2)
assert len(alive_threads(executor)) == 0
assert wait_for_alive_threads(executor, 0, 0.5) == 0

executor.submit(simple_delay_return, wait=0.1)
assert len(alive_threads(executor)) == 1
Expand Down
11 changes: 4 additions & 7 deletions tests/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pytest_mock import MockerFixture

from flexexecutor import ThreadPoolExecutor
from tests.conftest import alive_threads
from tests.conftest import alive_threads, wait_for_alive_threads


def simple_delay_return(n: int = 1, wait: float = 0.1):
Expand Down Expand Up @@ -128,8 +128,7 @@ def test_worker_alive():
assert len(alive_threads(executor)) == 0
executor.submit(lambda: 1)
assert len(alive_threads(executor)) == 1
time.sleep(0.5)
assert len(alive_threads(executor)) == 0
assert wait_for_alive_threads(executor, 0, 0.5) == 0


def test_max_workers():
Expand All @@ -147,11 +146,9 @@ def test_finite_timeout():
assert len(alive_threads(executor)) == 1
future.result()

time.sleep(0.5)
assert len(alive_threads(executor)) == 0

assert wait_for_alive_threads(executor, 0, 0.5) == 0
executor.submit(lambda: time.sleep(0.1))
assert len(alive_threads(executor)) == 1
assert wait_for_alive_threads(executor, 1, 0.1) == 1

assert len(alive_threads(executor)) == 0

Expand Down

0 comments on commit afa147b

Please sign in to comment.