Skip to content

Commit

Permalink
test: init_func
Browse files Browse the repository at this point in the history
  • Loading branch information
phi-friday committed Sep 13, 2024
1 parent 2068409 commit 1850ad0
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions tests/test_executor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import time
import uuid
from collections.abc import Awaitable
from itertools import product
from typing import Any
Expand All @@ -15,6 +16,18 @@
pytestmark = pytest.mark.anyio


def sample_func(*args: Any, **kwargs: Any) -> tuple[tuple[Any, ...], dict[str, Any]]:
return args, kwargs


async def sample_async_func(
*args: Any, **kwargs: Any
) -> tuple[tuple[Any, ...], dict[str, Any]]:
await anyio.sleep(0.1)

return sample_func(*args, **kwargs)


class SomeAwaitable:
def __init__(self, size: int = 5, value: Any = None) -> None:
self.size = max(size, 5)
Expand Down Expand Up @@ -178,13 +191,27 @@ def awaitable_func() -> Awaitable[Any]:
assert result == expect


def sample_func(*args: Any, **kwargs: Any) -> tuple[tuple[Any, ...], dict[str, Any]]:
return args, kwargs
def test_init_func():
key, value = str(uuid.uuid4()), str(uuid.uuid4())

def sample_init(key: str, value: str) -> None:
import os

async def sample_async_func(
*args: Any, **kwargs: Any
) -> tuple[tuple[Any, ...], dict[str, Any]]:
await anyio.sleep(0.1)
os.environ[key.upper()] = value

return sample_func(*args, **kwargs)
def get_init_if_exist(key: str) -> str:
import os

return os.environ.get(key.upper(), "")

executor = TimeoutExecutor(1)
executor.set_initializer(sample_init, key, value=value)
assert executor.initializer is not None
assert executor.initializer.function is sample_init
assert executor.initializer.args == (key,)
assert executor.initializer.kwargs == {"value": value}

async_result = executor.apply(get_init_if_exist, key)
result = async_result.result()
assert isinstance(result, str)
assert result == value

0 comments on commit 1850ad0

Please sign in to comment.