This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
/
test_atcp_server.py
65 lines (52 loc) · 2.17 KB
/
test_atcp_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Async TCP Server tests."""
import asyncio
import errno
import logging
from kytos.core.atcp_server import (KytosServer, KytosServerProtocol,
exception_handler)
# Using "nettest" TCP port as a way to avoid conflict with a running
# Kytos server on 6653.
TEST_ADDRESS = ('127.0.0.1', 4138)
class TestKytosServer:
"""Test if a Kytos Server will go up and receive connections."""
def setup(self):
"""Start new asyncio loop and a test TCP server."""
# pylint: disable=attribute-defined-outside-init
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
self.server = KytosServer(TEST_ADDRESS, KytosServerProtocol,
None, 'openflow', loop=self.loop)
self.server.serve_forever()
def test_connection_to_server(self):
"""Test if we really can connect to TEST_ADDRESS."""
@asyncio.coroutine
def wait_and_go():
"""Wait a little for the server to go up, then connect."""
yield from asyncio.sleep(0.01, loop=self.loop)
# reader, writer = ...
_ = yield from asyncio.open_connection(
*TEST_ADDRESS, loop=self.loop)
self.loop.run_until_complete(wait_and_go())
def test_exception_handler_oserror(self, caplog):
"""Test the TCP Server Exception Handler.
1. create mock OSError/TimeoutError instances
2. call exception_handler with them
3. ensure log is OK
"""
caplog.set_level(logging.INFO)
exception = TimeoutError()
context = {"exception": exception,
"transport": "unit_tests"}
exception_handler(self.loop, context)
exception2 = OSError(errno.EBADF, "Bad file descriptor")
context2 = {"exception": exception2,
"transport": "unit_tests"}
exception_handler(self.loop, context2)
assert caplog.record_tuples == [
("kytos.core.atcp_server",
logging.INFO,
"Socket timeout: 'unit_tests'"),
("kytos.core.atcp_server",
logging.INFO,
"Socket closed: 'unit_tests'"),
]