Skip to content

Commit

Permalink
Move tests to the create_future() wrapper
Browse files Browse the repository at this point in the history
Apply the asyncio.Future() -> helpers.create_future() refactoring to the
tests as well.
  • Loading branch information
jchampio committed Jun 2, 2016
1 parent 1e29862 commit f2c742f
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 94 deletions.
2 changes: 1 addition & 1 deletion tests/test_client_functional_oldstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ def test_POST_STREAM_DATA(self):
with open(fname, 'rb') as f:
data = f.read()

fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)

@asyncio.coroutine
def stream():
Expand Down
5 changes: 3 additions & 2 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import pytest
import aiohttp
from aiohttp import BaseConnector
from aiohttp import helpers
from aiohttp.client_reqrep import ClientRequest, ClientResponse

import os.path
Expand Down Expand Up @@ -716,7 +717,7 @@ def test_data_file(self):
self.loop.run_until_complete(req.close())

def test_data_stream_exc(self):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)

def gen():
yield b'binary data'
Expand Down Expand Up @@ -756,7 +757,7 @@ def gen():
resp.close()

def test_data_stream_exc_chain(self):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)

def gen():
yield from fut
Expand Down
23 changes: 12 additions & 11 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest.mock

import aiohttp
from aiohttp import helpers
from aiohttp.client_reqrep import ClientResponse


Expand Down Expand Up @@ -71,7 +72,7 @@ def test_repr(self):

def test_read_and_release_connection(self):
def side_effect(*args, **kwargs):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
fut.set_result(b'payload')
return fut
content = self.response.content = unittest.mock.Mock()
Expand All @@ -83,7 +84,7 @@ def side_effect(*args, **kwargs):

def test_read_and_release_connection_with_error(self):
content = self.response.content = unittest.mock.Mock()
content.read.return_value = asyncio.Future(loop=self.loop)
content.read.return_value = helpers.create_future(self.loop)
content.read.return_value.set_exception(ValueError)

self.assertRaises(
Expand All @@ -92,7 +93,7 @@ def test_read_and_release_connection_with_error(self):
self.assertTrue(self.response._closed)

def test_release(self):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
fut.set_result(b'')
content = self.response.content = unittest.mock.Mock()
content.readany.return_value = fut
Expand All @@ -103,7 +104,7 @@ def test_release(self):
def test_read_decode_deprecated(self):
self.response._content = b'data'
self.response.json = unittest.mock.Mock()
self.response.json.return_value = asyncio.Future(loop=self.loop)
self.response.json.return_value = helpers.create_future(self.loop)
self.response.json.return_value.set_result('json')

with self.assertWarns(DeprecationWarning):
Expand All @@ -113,7 +114,7 @@ def test_read_decode_deprecated(self):

def test_text(self):
def side_effect(*args, **kwargs):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
return fut
self.response.headers = {
Expand All @@ -127,7 +128,7 @@ def side_effect(*args, **kwargs):

def test_text_custom_encoding(self):
def side_effect(*args, **kwargs):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
return fut
self.response.headers = {
Expand All @@ -144,7 +145,7 @@ def side_effect(*args, **kwargs):

def test_text_detect_encoding(self):
def side_effect(*args, **kwargs):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
return fut
self.response.headers = {'CONTENT-TYPE': 'application/json'}
Expand All @@ -158,7 +159,7 @@ def side_effect(*args, **kwargs):

def test_text_after_read(self):
def side_effect(*args, **kwargs):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
return fut
self.response.headers = {
Expand All @@ -172,7 +173,7 @@ def side_effect(*args, **kwargs):

def test_json(self):
def side_effect(*args, **kwargs):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
return fut
self.response.headers = {
Expand Down Expand Up @@ -209,7 +210,7 @@ def test_json_no_content(self, m_log):

def test_json_override_encoding(self):
def side_effect(*args, **kwargs):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
return fut
self.response.headers = {
Expand All @@ -226,7 +227,7 @@ def side_effect(*args, **kwargs):

def test_json_detect_encoding(self):
def side_effect(*args, **kwargs):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
return fut
self.response.headers = {'CONTENT-TYPE': 'application/json'}
Expand Down
21 changes: 11 additions & 10 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import aiohttp
from aiohttp import web
from aiohttp import client
from aiohttp import helpers
from aiohttp.client import ClientResponse
from aiohttp.connector import Connection

Expand Down Expand Up @@ -273,7 +274,7 @@ class Req:
key = ('host', 80, False)
conn._conns[key] = [(tr, proto, self.loop.time())]
conn._create_connection = unittest.mock.Mock()
conn._create_connection.return_value = asyncio.Future(loop=self.loop)
conn._create_connection.return_value = helpers.create_future(self.loop)
conn._create_connection.return_value.set_result((tr, proto))

connection = self.loop.run_until_complete(conn.connect(Req()))
Expand All @@ -286,7 +287,7 @@ class Req:
def test_connect_timeout(self):
conn = aiohttp.BaseConnector(loop=self.loop)
conn._create_connection = unittest.mock.Mock()
conn._create_connection.return_value = asyncio.Future(loop=self.loop)
conn._create_connection.return_value = helpers.create_future(self.loop)
conn._create_connection.return_value.set_exception(
asyncio.TimeoutError())

Expand All @@ -297,7 +298,7 @@ def test_connect_timeout(self):
def test_connect_oserr(self):
conn = aiohttp.BaseConnector(loop=self.loop)
conn._create_connection = unittest.mock.Mock()
conn._create_connection.return_value = asyncio.Future(loop=self.loop)
conn._create_connection.return_value = helpers.create_future(self.loop)
err = OSError(1, 'permission error')
conn._create_connection.return_value.set_exception(err)

Expand Down Expand Up @@ -499,8 +500,8 @@ class Req:
key = ('host', 80, False)
conn._conns[key] = [(tr, proto, self.loop.time())]
conn._create_connection = unittest.mock.Mock()
conn._create_connection.return_value = asyncio.Future(
loop=self.loop)
conn._create_connection.return_value = helpers.create_future(
self.loop)
conn._create_connection.return_value.set_result((tr, proto))

connection1 = yield from conn.connect(Req())
Expand Down Expand Up @@ -547,8 +548,8 @@ class Req:
key = ('host', 80, False)
conn._conns[key] = [(tr, proto, self.loop.time())]
conn._create_connection = unittest.mock.Mock()
conn._create_connection.return_value = asyncio.Future(
loop=self.loop)
conn._create_connection.return_value = helpers.create_future(
self.loop)
conn._create_connection.return_value.set_result((tr, proto))

connection = yield from conn.connect(Req())
Expand All @@ -569,7 +570,7 @@ def check_with_exc(err):
conn = aiohttp.BaseConnector(limit=1, loop=self.loop)
conn._create_connection = unittest.mock.Mock()
conn._create_connection.return_value = \
asyncio.Future(loop=self.loop)
helpers.create_future(self.loop)
conn._create_connection.return_value.set_exception(err)

with self.assertRaises(Exception):
Expand Down Expand Up @@ -668,8 +669,8 @@ class Req:
key = ('host', 80, False)
conn._conns[key] = [(tr, proto, self.loop.time())]
conn._create_connection = unittest.mock.Mock()
conn._create_connection.return_value = asyncio.Future(
loop=self.loop)
conn._create_connection.return_value = helpers.create_future(
self.loop)
conn._create_connection.return_value.set_result((tr, proto))

connection = yield from conn.connect(Req())
Expand Down
7 changes: 4 additions & 3 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import zlib

import aiohttp.multipart
from aiohttp import helpers
from aiohttp.helpers import parse_mimetype
from aiohttp.hdrs import (
CONTENT_DISPOSITION,
Expand Down Expand Up @@ -48,7 +49,7 @@ def tearDown(self):
self.loop.close()

def future(self, obj):
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
fut.set_result(obj)
return fut

Expand Down Expand Up @@ -175,7 +176,7 @@ def test_read_incomplete_chunk(self):
stream = Stream(b'')

def prepare(data):
f = asyncio.Future(loop=self.loop)
f = helpers.create_future(self.loop)
f.set_result(data)
return f

Expand Down Expand Up @@ -216,7 +217,7 @@ def test_read_boundary_with_incomplete_chunk(self):
stream = Stream(b'')

def prepare(data):
f = asyncio.Future(loop=self.loop)
f = helpers.create_future(self.loop)
f.set_result(data)
return f

Expand Down
5 changes: 2 additions & 3 deletions tests/test_py35/test_web_websocket_35.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import pytest

import asyncio

import aiohttp
from aiohttp import web
from aiohttp import helpers


@pytest.mark.run_loop
async def test_server_ws_async_for(loop, create_server):
closed = asyncio.Future(loop=loop)
closed = helpers.create_future(loop)

async def handler(request):
ws = web.WebSocketResponse()
Expand Down
3 changes: 2 additions & 1 deletion tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from aiohttp import server
from aiohttp import errors
from aiohttp import helpers


@pytest.yield_fixture
Expand Down Expand Up @@ -507,7 +508,7 @@ def test_keep_alive_close_existing(make_srv, loop):
srv._keep_alive_period = 15
keep_alive_handle = srv._keep_alive_handle = mock.Mock()
srv.handle_request = mock.Mock()
srv.handle_request.return_value = asyncio.Future(loop=loop)
srv.handle_request.return_value = helpers.create_future(loop)
srv.handle_request.return_value.set_result(1)

srv.data_received(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from aiohttp import streams
from aiohttp import test_utils
from aiohttp import helpers


class TestStreamReader(unittest.TestCase):
Expand All @@ -24,7 +25,7 @@ def _make_one(self, *args, **kwargs):

def test_create_waiter(self):
stream = self._make_one()
stream._waiter = asyncio.Future(loop=self.loop)
stream._waiter = helpers.create_future(self.loop)
self.assertRaises(RuntimeError, stream._create_waiter, 'test')

@mock.patch('aiohttp.streams.asyncio')
Expand Down
6 changes: 3 additions & 3 deletions tests/test_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time

import pytest
from aiohttp.helpers import ensure_future, Timeout
from aiohttp.helpers import create_future, ensure_future, Timeout


@pytest.mark.run_loop
Expand Down Expand Up @@ -95,7 +95,7 @@ def long_running_task():

@pytest.mark.run_loop
def test_for_race_conditions(loop):
fut = asyncio.Future(loop=loop)
fut = create_future(loop)
loop.call_later(0.1, fut.set_result('done'))
with Timeout(0.2, loop=loop):
resp = yield from fut
Expand Down Expand Up @@ -151,7 +151,7 @@ def outer():

@pytest.mark.run_loop
def test_cancel_outer_coro(loop):
fut = asyncio.Future(loop=loop)
fut = create_future(loop)

@asyncio.coroutine
def outer():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from urllib.parse import unquote
from multidict import CIMultiDict
import aiohttp.web
from aiohttp import hdrs
from aiohttp import hdrs, helpers
from aiohttp.web import (UrlDispatcher, Request, Response,
HTTPMethodNotAllowed, HTTPNotFound,
HTTPCreated)
Expand Down Expand Up @@ -583,7 +583,7 @@ def test_static_handle_eof(self):
with mock.patch('aiohttp.web_urldispatcher.os') as m_os:
out_fd = 30
in_fd = 31
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
m_os.sendfile.return_value = 0
route._sendfile_cb(fut, out_fd, in_fd, 0, 100, loop, False)
m_os.sendfile.assert_called_with(out_fd, in_fd, 0, 100)
Expand All @@ -599,7 +599,7 @@ def test_static_handle_again(self):
with mock.patch('aiohttp.web_urldispatcher.os') as m_os:
out_fd = 30
in_fd = 31
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
m_os.sendfile.side_effect = BlockingIOError()
route._sendfile_cb(fut, out_fd, in_fd, 0, 100, loop, False)
m_os.sendfile.assert_called_with(out_fd, in_fd, 0, 100)
Expand All @@ -616,7 +616,7 @@ def test_static_handle_exception(self):
with mock.patch('aiohttp.web_urldispatcher.os') as m_os:
out_fd = 30
in_fd = 31
fut = asyncio.Future(loop=self.loop)
fut = helpers.create_future(self.loop)
exc = OSError()
m_os.sendfile.side_effect = exc
route._sendfile_cb(fut, out_fd, in_fd, 0, 100, loop, False)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_web_application.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import pytest

from aiohttp import web, log
from aiohttp import web, log, helpers
from unittest import mock


Expand Down Expand Up @@ -44,7 +44,7 @@ def test_app_register_on_finish(loop):
def test_app_register_coro(loop):
app = web.Application(loop=loop)

fut = asyncio.Future(loop=loop)
fut = helpers.create_future(loop)

@asyncio.coroutine
def cb(app):
Expand Down
Loading

0 comments on commit f2c742f

Please sign in to comment.