1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Create network tests

This commit is contained in:
Steffo 2019-03-22 11:22:21 +01:00
parent efd18fc3a7
commit 1b00ea8ae6
7 changed files with 75 additions and 5 deletions

2
README.md Normal file
View file

@ -0,0 +1,2 @@
# `royalnet`

View file

@ -1,2 +1,3 @@
python-telegram-bot>=11.1.0 python-telegram-bot>=11.1.0
websockets>=7.0 websockets>=7.0
pytest>=4.3.1

View file

@ -0,0 +1,3 @@
from .telegram import TelegramBot
__all__ = ["TelegramBot"]

View file

@ -6,6 +6,10 @@ from ..utils import asyncify, Call, Command
from ..network import RoyalnetLink, Message from ..network import RoyalnetLink, Message
async def todo(message: Message):
pass
class TelegramBot: class TelegramBot:
def __init__(self, def __init__(self,
api_key: str, api_key: str,
@ -17,7 +21,7 @@ class TelegramBot:
self.should_run: bool = False self.should_run: bool = False
self.offset: int = -100 self.offset: int = -100
self.missing_command = missing_command self.missing_command = missing_command
self.network: RoyalnetLink = RoyalnetLink(master_server_uri, master_server_secret, "telegram", null) self.network: RoyalnetLink = RoyalnetLink(master_server_uri, master_server_secret, "telegram", todo)
# Generate commands # Generate commands
self.commands = {} self.commands = {}
for command in commands: for command in commands:

View file

@ -68,7 +68,7 @@ class RoyalnetLink:
self.request_handler = request_handler self.request_handler = request_handler
self._pending_requests: typing.Dict[typing.Optional[Message]] = {} self._pending_requests: typing.Dict[typing.Optional[Message]] = {}
self._connect_event: asyncio.Event = asyncio.Event() self._connect_event: asyncio.Event = asyncio.Event()
self._identify_event: asyncio.Event = asyncio.Event() self.identify_event: asyncio.Event = asyncio.Event()
async def connect(self): async def connect(self):
log.info(f"Connecting to {self.master_uri}...") log.info(f"Connecting to {self.master_uri}...")
@ -83,7 +83,7 @@ class RoyalnetLink:
except websockets.ConnectionClosed: except websockets.ConnectionClosed:
self.websocket = None self.websocket = None
self._connect_event.clear() self._connect_event.clear()
self._identify_event.clear() self.identify_event.clear()
log.info(f"Connection to {self.master_uri} was closed.") log.info(f"Connection to {self.master_uri} was closed.")
# What to do now? Let's just reraise. # What to do now? Let's just reraise.
raise raise
@ -100,7 +100,7 @@ class RoyalnetLink:
response = response_package.data response = response_package.data
if isinstance(response, ErrorMessage): if isinstance(response, ErrorMessage):
raise NetworkError(response, "Server returned error while identifying self") raise NetworkError(response, "Server returned error while identifying self")
self._identify_event.set() self.identify_event.set()
log.info(f"Identified successfully!") log.info(f"Identified successfully!")
@requires_identification @requires_identification
@ -128,7 +128,7 @@ class RoyalnetLink:
while True: while True:
if self.websocket is None: if self.websocket is None:
await self.connect() await self.connect()
if not self._identify_event.is_set(): if not self.identify_event.is_set():
await self.identify() await self.identify()
package: Package = await self.receive() package: Package = await self.receive()
# Package is a response # Package is a response

27
setup.py Normal file
View file

@ -0,0 +1,27 @@
import setuptools
with open("README.md", "r") as f:
long_description = f.read()
setuptools.setup(
name="royalnet",
version="5.0a1",
author="Stefano Pigozzi",
author_email="ste.pigozzi@gmail.com",
description="The great bot network of the Royal Games community",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/Steffo99/royalnet",
packages=setuptools.find_packages(),
install_requires=[
"python-telegram-bot>=11.1.0",
"websockets>=7.0"
],
python_requires=">=3.7",
classifiers=[
"Development Status :: 3 - Alpha",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3.7",
"Topic :: Internet"
]
)

33
tests/test_network.py Normal file
View file

@ -0,0 +1,33 @@
import pytest
import asyncio
from royalnet.network import RoyalnetLink, RoyalnetServer
from royalnet.network import Message
async def echo(message: Message):
return message
def test_connection():
loop = asyncio.SelectorEventLoop()
server = RoyalnetServer("localhost", 1234, "testing")
link = RoyalnetLink("ws://localhost:1234", "testing", "testing", echo)
loop.create_task(server.run())
loop.run_until_complete(link.run())
assert link.websocket is not None
assert link.identify_event.is_set()
assert len(server.identified_clients) == 1
assert server.identified_clients[0].link_type == "testing"
def test_request():
loop = asyncio.SelectorEventLoop()
server = RoyalnetServer("localhost", 1234, "testing")
link1 = RoyalnetLink("ws://localhost:1234", "testing", "testing1", echo)
link2 = RoyalnetLink("ws://localhost:1234", "testing", "testing2", echo)
loop.create_task(server.run())
loop.create_task(link1.run())
loop.create_task(link2.run())
message = Message()
response = loop.run_until_complete(link1.request(message, "testing2"))
assert message is response