-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b39ef7
commit 01a8eac
Showing
2 changed files
with
76 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,77 @@ | ||
from ward import test | ||
import pyawaitable | ||
import ctypes as ct | ||
import ctypes | ||
from ctypes import pythonapi | ||
import pytest | ||
import asyncio | ||
|
||
get_pointer = pythonapi.PyCapsule_GetPointer | ||
get_pointer.argtypes = (ct.py_object, ct.c_void_p) | ||
get_pointer.restype = ct.c_void_p | ||
get_pointer.argtypes = (ctypes.py_object, ctypes.c_void_p) | ||
get_pointer.restype = ctypes.c_void_p | ||
|
||
@test("awaitable creation") | ||
async def _(): | ||
ptr = get_pointer(pyawaitable._api, None) | ||
awaitcallback = ctypes.CFUNCTYPE(ctypes.py_object, ctypes.py_object) | ||
awaitcallback_err = awaitcallback | ||
|
||
# Initialize API array | ||
ptr = get_pointer(pyawaitable._api, None) | ||
api = (ctypes.c_void_p * 10).from_address(ptr) | ||
|
||
# Types | ||
AwaitableType = ctypes.cast(api[0], ctypes.py_object).value | ||
AwaitableGenWrapperType = ctypes.cast(api[1], ctypes.py_object).value | ||
|
||
# API Functions | ||
awaitable_new = ctypes.cast(api[2], ctypes.CFUNCTYPE(ctypes.py_object)) | ||
awaitable_await = ctypes.cast( | ||
api[3], | ||
ctypes.CFUNCTYPE( | ||
ctypes.c_int, | ||
ctypes.py_object, | ||
ctypes.py_object, | ||
awaitcallback, | ||
awaitcallback_err, | ||
), | ||
) | ||
|
||
|
||
def test_api_types(): | ||
assert AwaitableType is pyawaitable._awaitable | ||
assert AwaitableGenWrapperType is pyawaitable._genwrapper | ||
|
||
|
||
@pytest.mark.limit_leaks("5 KB") | ||
@pytest.mark.asyncio | ||
async def test_new(): | ||
assert isinstance(awaitable_new(), pyawaitable._awaitable) | ||
await asyncio.create_task(awaitable_new()) | ||
await awaitable_new() | ||
|
||
|
||
@pytest.mark.limit_leaks("5 KB") | ||
@pytest.mark.asyncio | ||
async def test_await(): | ||
event = asyncio.Event() | ||
|
||
async def coro(): | ||
event.set() | ||
|
||
awaitable = awaitable_new() | ||
awaitable_await(awaitable, coro(), awaitcallback(0), awaitcallback_err(0)) | ||
await awaitable | ||
assert event.is_set() | ||
|
||
|
||
@pytest.mark.limit_leaks("5 KB") | ||
@pytest.mark.asyncio | ||
async def test_await_cb(): | ||
awaitable = awaitable_new() | ||
|
||
async def coro(value: int): | ||
return value * 2 | ||
|
||
@awaitcallback | ||
def cb(awaitable_inner: AwaitableType, result: int) -> int: | ||
assert awaitable_inner is awaitable | ||
assert result == 42 | ||
return 0 | ||
|
||
awaitable_await(awaitable, coro(21), cb, awaitcallback_err(0)) |