Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing keep-alive #406

Merged
merged 8 commits into from
Jun 11, 2015
Merged
Prev Previous commit
Added fiunctional keep-alive tests against aiohttp.web server
  • Loading branch information
gabtremblay committed Jun 10, 2015
commit 5bd081b8765a3e311c43e2187b7cc69792879120
121 changes: 121 additions & 0 deletions tests/test_client_functional_newstyle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""Http client functional tests against aiohttp.web server"""

import asyncio
import socket
import unittest

import aiohttp
from aiohttp import client, web, log


class TestHttpClientFunctionalNewStyle(unittest.TestCase):

def setUp(self):
self.handler = None
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)

def tearDown(self):
if self.handler:
self.loop.run_until_complete(self.handler.finish_connections())
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=None):
app = web.Application(loop=self.loop)
if handler:
app.router.add_route(method, path, handler)

port = self.find_unused_port()
self.handler = app.make_handler(
debug=True, keep_alive_on=False,
access_log=log.access_logger)
srv = yield from self.loop.create_server(
self.handler, '127.0.0.1', port)
url = "http://127.0.0.1:{}".format(port) + path
self.addCleanup(srv.close)
return app, srv, url

def test_keepalive_two_requests_sucess(self):
@asyncio.coroutine
def handler(request):
body = yield from request.read()
self.assertEqual(b'', body)
return web.Response(body=b'OK')

@asyncio.coroutine
def go():
_, srv, url = yield from self.create_server('GET', '/', handler)
connector = aiohttp.TCPConnector(loop=self.loop)
r = yield from client.request('GET', url,
connector=connector, loop=self.loop)
yield from r.read()

r2 = yield from client.request('GET', url,
connector=connector, loop=self.loop)
yield from r2.read()
self.assertEqual(1, len(connector._conns))
connector.close()

self.loop.run_until_complete(go())

def test_keepalive_response_released(self):
@asyncio.coroutine
def handler(request):
body = yield from request.read()
self.assertEqual(b'', body)
return web.Response(body=b'OK')

@asyncio.coroutine
def go():
_, srv, url = yield from self.create_server('GET', '/', handler)
connector = aiohttp.TCPConnector(loop=self.loop)
r = yield from client.request('GET', url,
connector=connector, loop=self.loop)
yield from r.read()
r.release()

r2 = yield from client.request('GET', url,
connector=connector, loop=self.loop)
yield from r2.read()
r2.release()
self.assertEqual(1, len(connector._conns))
connector.close()

self.loop.run_until_complete(go())

def test_keepalive_server_force_close_connection(self):
@asyncio.coroutine
def handler(request):
body = yield from request.read()
self.assertEqual(b'', body)
response = web.Response(body=b'OK')
response.force_close()
return response

@asyncio.coroutine
def go():
_, srv, url = yield from self.create_server('GET', '/', handler)

connector = aiohttp.TCPConnector(loop=self.loop)

r = yield from client.request('GET', url,
connector=connector, loop=self.loop)
yield from r.read()
self.assertEqual(0, len(connector._conns))

r2 = yield from client.request('GET', url,
connector=connector, loop=self.loop)
yield from r2.read()

self.assertEqual(0, len(connector._conns))
connector.close()

self.loop.run_until_complete(go())