Skip to content

Commit

Permalink
Choose an unused port in tests (#1006)
Browse files Browse the repository at this point in the history
* Choose an unused port in tests

* Remove dupe find_unused_port implementations
  • Loading branch information
mpaolini authored and asvetlov committed Jul 26, 2016
1 parent 32c0f1d commit b637251
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 39 deletions.
14 changes: 3 additions & 11 deletions tests/test_client_functional_oldstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import json
import http.cookies
import asyncio
import socket
import unittest
from unittest import mock

Expand All @@ -17,14 +16,7 @@
from aiohttp import client, helpers
from aiohttp import test_utils
from aiohttp.multipart import MultipartWriter


def find_unused_port():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 0))
port = s.getsockname()[1]
s.close()
return port
from aiohttp.test_utils import unused_port


class TestHttpClientFunctional(unittest.TestCase):
Expand Down Expand Up @@ -952,7 +944,7 @@ def connection_lost(self, exc):
@asyncio.coroutine
def go():
server = yield from self.loop.create_server(
Proto, '127.0.0.1', find_unused_port())
Proto, '127.0.0.1', unused_port())

addr = server.sockets[0].getsockname()

Expand Down Expand Up @@ -995,7 +987,7 @@ def connection_lost(self, exc):
@asyncio.coroutine
def go():
server = yield from self.loop.create_server(
Proto, '127.0.0.1', find_unused_port())
Proto, '127.0.0.1', unused_port())

addr = server.sockets[0].getsockname()

Expand Down
14 changes: 4 additions & 10 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from aiohttp import helpers
from aiohttp.client import ClientResponse, ClientRequest
from aiohttp.connector import Connection
from aiohttp.test_utils import unused_port


class TestBaseConnector(unittest.TestCase):
Expand Down Expand Up @@ -718,19 +719,12 @@ def tearDown(self):
self.loop.close()
gc.collect()

def find_unused_port(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 0))
port = s.getsockname()[1]
s.close()
return port

@asyncio.coroutine
def create_server(self, method, path, handler):
app = web.Application(loop=self.loop)
app.router.add_route(method, path, handler)

port = self.find_unused_port()
port = unused_port()
self.handler = app.make_handler(keep_alive_on=False)
srv = yield from self.loop.create_server(
self.handler, '127.0.0.1', port)
Expand Down Expand Up @@ -780,7 +774,7 @@ def handler(request):
self.create_server('get', '/', handler)
)

port = self.find_unused_port()
port = unused_port()
conn = aiohttp.TCPConnector(loop=self.loop,
local_addr=('127.0.0.1', port))

Expand Down Expand Up @@ -840,7 +834,7 @@ def test_resolver_not_called_with_address_is_ip(self):
resolver = unittest.mock.MagicMock()
connector = aiohttp.TCPConnector(resolver=resolver, loop=self.loop)

req = ClientRequest('GET', 'http://127.0.0.1:80',
req = ClientRequest('GET', 'http://127.0.0.1:{}'.format(unused_port()),
loop=self.loop,
response_class=unittest.mock.Mock())

Expand Down
11 changes: 2 additions & 9 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import json
import os
import os.path
import socket
import unittest
import zlib
from multidict import MultiDict
from aiohttp import log, web, request, FormData, ClientSession, TCPConnector
from aiohttp.file_sender import FileSender
from aiohttp.protocol import HttpVersion, HttpVersion10, HttpVersion11
from aiohttp.streams import EOF_MARKER
from aiohttp.test_utils import unused_port

from unittest import mock

Expand All @@ -35,13 +35,6 @@ def tearDown(self):
self.loop.close()
gc.collect()

def find_unused_port(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 0))
port = s.getsockname()[1]
s.close()
return port

@asyncio.coroutine
def create_server(self, method, path, handler=None, ssl_ctx=None,
logger=log.server_logger, handler_kwargs=None):
Expand All @@ -50,7 +43,7 @@ def create_server(self, method, path, handler=None, ssl_ctx=None,
if handler:
app.router.add_route(method, path, handler)

port = self.find_unused_port()
port = unused_port()
self.handler = app.make_handler(
keep_alive_on=False,
access_log=log.access_logger,
Expand Down
11 changes: 2 additions & 9 deletions tests/test_web_websocket_functional_oldstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import base64
import hashlib
import os
import socket
import unittest
from aiohttp.test_utils import unused_port

import aiohttp
from aiohttp import helpers, web, websocket
Expand All @@ -21,19 +21,12 @@ def setUp(self):
def tearDown(self):
self.loop.close()

def find_unused_port(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 0))
port = s.getsockname()[1]
s.close()
return port

@asyncio.coroutine
def create_server(self, method, path, handler):
app = web.Application(loop=self.loop)
app.router.add_route(method, path, handler)

port = self.find_unused_port()
port = unused_port()
srv = yield from self.loop.create_server(
app.make_handler(), '127.0.0.1', port)
url = "http://127.0.0.1:{}".format(port) + path
Expand Down

0 comments on commit b637251

Please sign in to comment.