Merge pull request #94 from zaibon/test_port_management

don't manually assign port during test
This commit is contained in:
Robert Zajac 2018-12-20 08:53:26 -05:00 committed by GitHub
commit 390f2fa9f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 31 deletions

View File

@ -7,8 +7,8 @@ from peer.peerinfo import info_from_p2p_addr
@pytest.mark.asyncio
async def test_simple_messages():
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8001"])
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8000"])
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
async def stream_handler(stream):
while True:
@ -41,8 +41,8 @@ async def test_simple_messages():
@pytest.mark.asyncio
async def test_double_response():
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8002"])
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8003"])
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
async def stream_handler(stream):
while True:
@ -80,12 +80,13 @@ async def test_double_response():
# Success, terminate pending tasks.
return
@pytest.mark.asyncio
async def test_multiple_streams():
# Node A should be able to open a stream with node B and then vice versa.
# Stream IDs should be generated uniquely so that the stream state is not overwritten
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8004"])
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8005"])
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
async def stream_handler_a(stream):
while True:
@ -128,10 +129,11 @@ async def test_multiple_streams():
# Success, terminate pending tasks.
return
@pytest.mark.asyncio
async def test_host_connect():
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8001/"])
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/8000/"])
node_a = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
node_b = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
assert not node_a.get_peerstore().peers()

View File

@ -11,14 +11,14 @@ from protocol_muxer.multiselect_client import MultiselectClientError
# TODO: modify tests so that those async issues don't occur
# when using the same ports across tests
async def perform_simple_test(expected_selected_protocol, \
protocols_for_client, protocols_with_handlers, \
node_a_port, node_b_port):
transport_opt_a = ["/ip4/127.0.0.1/tcp/" + str(node_a_port)]
transport_opt_b = ["/ip4/127.0.0.1/tcp/" + str(node_b_port)]
node_a = await new_node(\
async def perform_simple_test(expected_selected_protocol,
protocols_for_client, protocols_with_handlers):
transport_opt_a = ["/ip4/127.0.0.1/tcp/0"]
transport_opt_b = ["/ip4/127.0.0.1/tcp/0"]
node_a = await new_node(
transport_opt=transport_opt_a)
node_b = await new_node(\
node_b = await new_node(
transport_opt=transport_opt_b)
async def stream_handler(stream):
@ -51,38 +51,43 @@ async def perform_simple_test(expected_selected_protocol, \
# Success, terminate pending tasks.
return
@pytest.mark.asyncio
async def test_single_protocol_succeeds():
expected_selected_protocol = "/echo/1.0.0"
await perform_simple_test(expected_selected_protocol, \
["/echo/1.0.0"], ["/echo/1.0.0"], 8050, 8051)
await perform_simple_test(expected_selected_protocol,
["/echo/1.0.0"], ["/echo/1.0.0"])
@pytest.mark.asyncio
async def test_single_protocol_fails():
with pytest.raises(MultiselectClientError):
await perform_simple_test("", ["/echo/1.0.0"], \
["/potato/1.0.0"], 8052, 8053)
await perform_simple_test("", ["/echo/1.0.0"],
["/potato/1.0.0"])
@pytest.mark.asyncio
async def test_multiple_protocol_first_is_valid_succeeds():
expected_selected_protocol = "/echo/1.0.0"
protocols_for_client = ["/echo/1.0.0", "/potato/1.0.0"]
protocols_for_listener = ["/foo/1.0.0", "/echo/1.0.0"]
await perform_simple_test(expected_selected_protocol, protocols_for_client, \
protocols_for_listener, 8054, 8055)
await perform_simple_test(expected_selected_protocol, protocols_for_client,
protocols_for_listener)
@pytest.mark.asyncio
async def test_multiple_protocol_second_is_valid_succeeds():
expected_selected_protocol = "/foo/1.0.0"
protocols_for_client = ["/rock/1.0.0", "/foo/1.0.0"]
protocols_for_listener = ["/foo/1.0.0", "/echo/1.0.0"]
await perform_simple_test(expected_selected_protocol, protocols_for_client, \
protocols_for_listener, 8056, 8057)
await perform_simple_test(expected_selected_protocol, protocols_for_client,
protocols_for_listener)
@pytest.mark.asyncio
async def test_multiple_protocol_fails():
protocols_for_client = ["/rock/1.0.0", "/foo/1.0.0", "/bar/1.0.0"]
protocols_for_listener = ["/aspyn/1.0.0", "/rob/1.0.0", "/zx/1.0.0", "/alex/1.0.0"]
with pytest.raises(MultiselectClientError):
await perform_simple_test("", protocols_for_client, \
protocols_for_listener, 8058, 8059)
await perform_simple_test("", protocols_for_client,
protocols_for_listener)

View File

@ -0,0 +1,20 @@
import asyncio
import pytest
from transport.tcp.tcp import _multiaddr_from_socket
@pytest.mark.asyncio
async def test_multiaddr_from_socket():
def handler(r, w):
pass
server = await asyncio.start_server(handler, '127.0.0.1', 8000)
assert str(_multiaddr_from_socket(server.sockets[0])) == '/ip4/127.0.0.1/tcp/8000'
server = await asyncio.start_server(handler, '127.0.0.1', 0)
addr = _multiaddr_from_socket(server.sockets[0])
assert addr.value_for_protocol('ip4') == '127.0.0.1'
port = addr.value_for_protocol('tcp')
assert int(port) > 0

View File

@ -1,7 +1,11 @@
import asyncio
from transport.transport_interface import ITransport
from transport.listener_interface import IListener
import multiaddr
from network.connection.raw_connection import RawConnection
from transport.listener_interface import IListener
from transport.transport_interface import ITransport
class TCP(ITransport):
@ -22,14 +26,15 @@ class TCP(ITransport):
:return: return True if successful
"""
_multiaddr = multiaddr
_multiaddr = _multiaddr.decapsulate('/ipfs')
self.multiaddrs.append(_multiaddr)
coroutine = asyncio.start_server(self.handler,
_multiaddr.value_for_protocol('ip4'),
_multiaddr.value_for_protocol('tcp'))
_multiaddr.value_for_protocol('ip4'),
_multiaddr.value_for_protocol('tcp'))
self.server = await coroutine
socket = self.server.sockets[0]
self.multiaddrs.append(_multiaddr_from_socket(socket))
return True
def get_addrs(self):
@ -80,3 +85,7 @@ class TCP(ITransport):
:return: a listener object that implements listener_interface.py
"""
return self.Listener(handler_function)
def _multiaddr_from_socket(socket):
return multiaddr.Multiaddr("/ip4/%s/tcp/%s" % socket.getsockname())