checkpoint
This commit is contained in:
parent
2e0bf0cb67
commit
c4cf873c48
37
README.md
37
README.md
@ -0,0 +1,37 @@
|
|||||||
|
# Announce Server
|
||||||
|
|
||||||
|
A Python library that announces a server to a host.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install announce-server
|
||||||
|
```
|
||||||
|
|
||||||
|
## Build
|
||||||
|
To build the package, run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install -e .[dev]
|
||||||
|
pip install -m build
|
||||||
|
```
|
||||||
|
|
||||||
|
## Test
|
||||||
|
|
||||||
|
To run the tests, install the package with the `[dev]` option:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install -e .[dev]
|
||||||
|
pytest
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```python
|
||||||
|
from announce_server.announce import announce_server
|
||||||
|
|
||||||
|
@announce_server(name="server_name", ip="server_ip", port=8000, host_ip="host_server_ip", host_port=5000)
|
||||||
|
def your_function():
|
||||||
|
pass
|
||||||
|
|
||||||
|
```
|
@ -44,26 +44,24 @@ async def _announce_server(**kwargs):
|
|||||||
await main()
|
await main()
|
||||||
|
|
||||||
|
|
||||||
def announce_server(task=None, loop=None, **outer_kwargs):
|
# def announce_server(task=None, loop=None, **outer_kwargs):
|
||||||
if task is None:
|
# if task is None:
|
||||||
return lambda f: announce_server(f, loop=loop, **outer_kwargs)
|
# return lambda f: announce_server(f, loop=loop, **outer_kwargs)
|
||||||
|
|
||||||
@wraps(task)
|
# @wraps(task)
|
||||||
def wrapper(*args, **kwargs):
|
# async def wrapper(*args, **kwargs):
|
||||||
async def main(*args, **kwargs):
|
# if not asyncio.iscoroutinefunction(task):
|
||||||
if loop is not None:
|
# # If the decorated function is not a coroutine, wrap it in a coroutine
|
||||||
host_block_thread = loop.run_in_executor(None, task)
|
# task = asyncio.coroutine(task)
|
||||||
else:
|
# if loop is not None:
|
||||||
host_block_thread = asyncio.to_thread(task)
|
# host_block_thread = loop.run_in_executor(None, task)
|
||||||
|
# else:
|
||||||
|
# host_block_thread = asyncio.to_thread(task)
|
||||||
|
|
||||||
# Announce the server to the host
|
# # Announce the server to the host
|
||||||
await _announce_server(**outer_kwargs)
|
# await _announce_server(**outer_kwargs)
|
||||||
|
|
||||||
# Wait for host_block to finish
|
# # Wait for host_block to finish
|
||||||
await host_block_thread
|
# await host_block_thread
|
||||||
|
|
||||||
if loop is not None:
|
# return wrapper
|
||||||
return loop.create_task(main(*args, **kwargs))
|
|
||||||
else:
|
|
||||||
return asyncio.run(main(*args, **kwargs))
|
|
||||||
return wrapper
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import sys
|
import sys
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch, MagicMock
|
||||||
|
|
||||||
if sys.version_info >= (3, 8):
|
if sys.version_info >= (3, 8):
|
||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
@ -9,19 +9,20 @@ else:
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from announce_server import _announce_server, announce_server
|
from announce_server.decorator import _announce_server, announce_server
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_announce_server_decorator(event_loop, mocker):
|
# @patch('announce_server.decorator.announce_server', new=MagicMock())
|
||||||
# Mock the _announce_server function to prevent actual connections
|
async def test_announce_server_decorator(mocked_announce_server, event_loop):
|
||||||
mocker.patch("announce_server._announce_server")
|
|
||||||
|
|
||||||
# Sample function to be decorated
|
# Sample function to be decorated
|
||||||
async def sample_async_function():
|
async def sample_async_function():
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
return "Hello, world!"
|
return "Hello, world!"
|
||||||
|
|
||||||
|
# Mock the _announce_server function to prevent actual connections
|
||||||
|
mocked_announce_server.return_value = lambda x: x
|
||||||
|
|
||||||
# Decorate the sample function with announce_server
|
# Decorate the sample function with announce_server
|
||||||
decorated_function = announce_server(
|
decorated_function = announce_server(
|
||||||
name="test_server",
|
name="test_server",
|
||||||
@ -33,17 +34,19 @@ async def test_announce_server_decorator(event_loop, mocker):
|
|||||||
)(sample_async_function)
|
)(sample_async_function)
|
||||||
|
|
||||||
# Run the decorated function
|
# Run the decorated function
|
||||||
task = await decorated_function()
|
coro = asyncio.to_thread(decorated_function)
|
||||||
|
task = await asyncio.gather(coro)
|
||||||
await asyncio.sleep(1.1) # Sleep slightly longer than sample_async_function
|
await asyncio.sleep(1.1) # Sleep slightly longer than sample_async_function
|
||||||
task.cancel() # Cancel the task
|
task.cancel() # Cancel the task
|
||||||
|
|
||||||
# Check if the _announce_server function was called with the correct arguments
|
# Check if the _announce_server function was called with the correct arguments
|
||||||
announce_server._announce_server.assert_called_once_with(
|
mocked_announce_server.assert_called_once_with(
|
||||||
name="test_server",
|
name="test_server",
|
||||||
ip="127.0.0.1",
|
ip="127.0.0.1",
|
||||||
port=8000,
|
port=8000,
|
||||||
host_ip="127.0.0.1",
|
host_ip="127.0.0.1",
|
||||||
host_port=5000,
|
host_port=5000,
|
||||||
|
loop=event_loop,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check if the decorated function returns the expected result
|
# Check if the decorated function returns the expected result
|
||||||
|
Loading…
Reference in New Issue
Block a user