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()
|
||||
|
||||
|
||||
def announce_server(task=None, loop=None, **outer_kwargs):
|
||||
if task is None:
|
||||
return lambda f: announce_server(f, loop=loop, **outer_kwargs)
|
||||
# def announce_server(task=None, loop=None, **outer_kwargs):
|
||||
# if task is None:
|
||||
# return lambda f: announce_server(f, loop=loop, **outer_kwargs)
|
||||
|
||||
@wraps(task)
|
||||
def wrapper(*args, **kwargs):
|
||||
async def main(*args, **kwargs):
|
||||
if loop is not None:
|
||||
host_block_thread = loop.run_in_executor(None, task)
|
||||
else:
|
||||
host_block_thread = asyncio.to_thread(task)
|
||||
# @wraps(task)
|
||||
# async def wrapper(*args, **kwargs):
|
||||
# if not asyncio.iscoroutinefunction(task):
|
||||
# # If the decorated function is not a coroutine, wrap it in a coroutine
|
||||
# task = asyncio.coroutine(task)
|
||||
# if loop is not None:
|
||||
# host_block_thread = loop.run_in_executor(None, task)
|
||||
# else:
|
||||
# host_block_thread = asyncio.to_thread(task)
|
||||
|
||||
# Announce the server to the host
|
||||
await _announce_server(**outer_kwargs)
|
||||
# # Announce the server to the host
|
||||
# await _announce_server(**outer_kwargs)
|
||||
|
||||
# Wait for host_block to finish
|
||||
await host_block_thread
|
||||
# # Wait for host_block to finish
|
||||
# await host_block_thread
|
||||
|
||||
if loop is not None:
|
||||
return loop.create_task(main(*args, **kwargs))
|
||||
else:
|
||||
return asyncio.run(main(*args, **kwargs))
|
||||
return wrapper
|
||||
# return wrapper
|
||||
|
@ -1,6 +1,6 @@
|
||||
import asyncio
|
||||
import sys
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
if sys.version_info >= (3, 8):
|
||||
from unittest.mock import AsyncMock
|
||||
@ -9,19 +9,20 @@ else:
|
||||
|
||||
import pytest
|
||||
|
||||
from announce_server import _announce_server, announce_server
|
||||
from announce_server.decorator import _announce_server, announce_server
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_announce_server_decorator(event_loop, mocker):
|
||||
# Mock the _announce_server function to prevent actual connections
|
||||
mocker.patch("announce_server._announce_server")
|
||||
|
||||
# @patch('announce_server.decorator.announce_server', new=MagicMock())
|
||||
async def test_announce_server_decorator(mocked_announce_server, event_loop):
|
||||
# Sample function to be decorated
|
||||
async def sample_async_function():
|
||||
await asyncio.sleep(1)
|
||||
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
|
||||
decorated_function = announce_server(
|
||||
name="test_server",
|
||||
@ -33,17 +34,19 @@ async def test_announce_server_decorator(event_loop, mocker):
|
||||
)(sample_async_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
|
||||
task.cancel() # Cancel the task
|
||||
|
||||
# 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",
|
||||
ip="127.0.0.1",
|
||||
port=8000,
|
||||
host_ip="127.0.0.1",
|
||||
host_port=5000,
|
||||
loop=event_loop,
|
||||
)
|
||||
|
||||
# Check if the decorated function returns the expected result
|
||||
|
Loading…
Reference in New Issue
Block a user