Provides decorator to announce the presence of a server to a host machine.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

import asyncio
2 years ago
import subprocess
from unittest.mock import MagicMock, patch
import pytest
2 years ago
from announce_server.decorator import _announce_server, announce_server
2 years ago
@patch("announce_server.decorator._announce_server")
def test_announce_server_decorator(mock_announce_server):
2 years ago
# Mock the _announce_server function to prevent actual connections
2 years ago
mock_announce_server.return_value = MagicMock()
2 years ago
# Decorate the sample function with announce_server
2 years ago
@announce_server(
name="test_server",
ip="127.0.0.1",
port=8000,
host_ip="127.0.0.1",
host_port=5000,
2 years ago
)
def http_server():
server = subprocess.Popen(["python3", "-m", "http.server", "13373"])
yield
server.terminate()
server.wait()
# Run the decorated function
2 years ago
http_server()
# Check if the _announce_server function was called with the correct arguments
2 years ago
mock_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,
)