Skip to content

Commit

Permalink
Fix code style in unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
claws authored and 1st1 committed May 7, 2016
1 parent 77dc53e commit f894ac7
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 73 deletions.
2 changes: 0 additions & 2 deletions tests/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
else:
skip_tests = False

import asyncio
import uvloop
import unittest

from uvloop import _testbase as tb
Expand Down
30 changes: 16 additions & 14 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def cb():

loop.set_debug(debug)
if debug:
msg = ("Non-thread-safe operation invoked on an event loop other "
"than the current one")
msg = ("Non-thread-safe operation invoked on an "
"event loop other than the current one")
with self.assertRaisesRegex(RuntimeError, msg):
loop.call_soon(cb)
with self.assertRaisesRegex(RuntimeError, msg):
Expand Down Expand Up @@ -189,6 +189,7 @@ def test_thread(loop, debug, create_loop=False):

def test_run_once_in_executor_plain(self):
called = []

def cb(arg):
called.append(arg)

Expand All @@ -206,18 +207,17 @@ def test_set_debug(self):
self.assertFalse(self.loop.get_debug())

def test_run_until_complete_type_error(self):
self.assertRaises(TypeError,
self.loop.run_until_complete, 'blah')
self.assertRaises(
TypeError, self.loop.run_until_complete, 'blah')

def test_run_until_complete_loop(self):
task = asyncio.Future(loop=self.loop)
other_loop = self.new_loop()
self.addCleanup(other_loop.close)
self.assertRaises(ValueError,
other_loop.run_until_complete, task)
self.assertRaises(
ValueError, other_loop.run_until_complete, task)

def test_run_until_complete_error(self):
task = asyncio.Future(loop=self.loop)
async def foo():
raise ValueError('aaa')
with self.assertRaisesRegex(ValueError, 'aaa'):
Expand All @@ -228,7 +228,7 @@ def test_default_exc_handler_callback(self):

def zero_error(fut):
fut.set_result(True)
1/0
1 / 0

logger = logging.getLogger('asyncio')

Expand Down Expand Up @@ -258,11 +258,12 @@ def test_set_exc_handler_custom(self):
def run_loop():
def zero_error():
self.loop.stop()
1/0
1 / 0
self.loop.call_soon(zero_error)
self.loop.run_forever()

errors = []

def handler(loop, exc):
errors.append(exc)

Expand All @@ -278,8 +279,8 @@ def handler(loop, exc):
with mock.patch.object(logger, 'error') as log:
run_loop()
log.assert_called_with(
self.mock_pattern('Exception in callback.*zero'),
exc_info=mock.ANY)
self.mock_pattern('Exception in callback.*zero'),
exc_info=mock.ANY)

self.assertEqual(len(errors), 1)

Expand All @@ -289,7 +290,7 @@ def test_set_exc_handler_broken(self):
def run_loop():
def zero_error():
self.loop.stop()
1/0
1 / 0
self.loop.call_soon(zero_error)
self.loop.run_forever()

Expand Down Expand Up @@ -328,7 +329,7 @@ def default_exception_handler(self, context):
def run_loop():
def zero_error():
loop.stop()
1/0
1 / 0
loop.call_soon(zero_error)
loop.run_forever()

Expand Down Expand Up @@ -358,7 +359,8 @@ def custom_handler(loop, context):

def test_set_task_factory_invalid(self):
with self.assertRaisesRegex(
TypeError, 'task factory must be a callable or None'):
TypeError,
'task factory must be a callable or None'):

self.loop.set_task_factory(1)

Expand Down
4 changes: 1 addition & 3 deletions tests/test_dns.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import asyncio
import socket
import uvloop
import unittest

from uvloop import _testbase as tb
Expand Down Expand Up @@ -136,6 +134,6 @@ async def run():


class Test_AIO_DNS(BaseTestDNS, tb.AIOTestCase):
pass

def test_getaddrinfo_11(self):
self._test_getaddrinfo(_HOST.encode(), str(_PORT))
2 changes: 2 additions & 0 deletions tests/test_pipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def test_write_pipe(self):
transport.write(b'1')

data = bytearray()

def reader(data):
chunk = os.read(rpipe, 1024)
data += chunk
Expand Down Expand Up @@ -205,6 +206,7 @@ def test_write_pty(self):
transport.write(b'1')

data = bytearray()

def reader(data):
chunk = os.read(master, 1024)
data += chunk
Expand Down
56 changes: 16 additions & 40 deletions tests/test_process.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import asyncio
import contextlib
import signal
import socket
import subprocess
import sys
import tempfile
import uvloop

from asyncio import test_utils
from uvloop import _testbase as tb
from test import support


class _TestProcess:
Expand Down Expand Up @@ -276,12 +273,12 @@ def test_stdin_not_inheritable(self):
def len_message(message):
code = 'import sys; data = sys.stdin.read(); print(len(data))'
proc = yield from asyncio.create_subprocess_exec(
sys.executable, '-c', code,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
close_fds=False,
loop=self.loop)
sys.executable, '-c', code,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
close_fds=False,
loop=self.loop)
stdout, stderr = yield from proc.communicate(message)
exitcode = yield from proc.wait()
return (stdout, exitcode)
Expand All @@ -296,10 +293,10 @@ def test_stdin_stdout(self):
@asyncio.coroutine
def run(data):
proc = yield from asyncio.create_subprocess_exec(
*args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
loop=self.loop)
*args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
loop=self.loop)

# feed data
proc.stdin.write(data)
Expand All @@ -323,10 +320,10 @@ def test_communicate(self):
@asyncio.coroutine
def run(data):
proc = yield from asyncio.create_subprocess_exec(
*args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
loop=self.loop)
*args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
loop=self.loop)
stdout, stderr = yield from proc.communicate(data)
return proc.returncode, stdout

Expand Down Expand Up @@ -389,35 +386,14 @@ def send_signal(proc):
returncode = self.loop.run_until_complete(send_signal(proc))
self.assertEqual(-signal.SIGHUP, returncode)

def test_stdin_not_inheritable(self):
# asyncio issue #209: stdin must not be inheritable, otherwise
# the Process.communicate() hangs
@asyncio.coroutine
def len_message(message):
code = 'import sys; data = sys.stdin.read(); print(len(data))'
proc = yield from asyncio.create_subprocess_exec(
sys.executable, '-c', code,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
close_fds=False,
loop=self.loop)
stdout, stderr = yield from proc.communicate(message)
exitcode = yield from proc.wait()
return (stdout, exitcode)

output, exitcode = self.loop.run_until_complete(len_message(b'abc'))
self.assertEqual(output.rstrip(), b'3')
self.assertEqual(exitcode, 0)

def test_cancel_process_wait(self):
# Issue #23140: cancel Process.wait()

@asyncio.coroutine
def cancel_wait():
proc = yield from asyncio.create_subprocess_exec(
*self.PROGRAM_BLOCKED,
loop=self.loop)
*self.PROGRAM_BLOCKED,
loop=self.loop)

# Create an internal future waiting on the process exit
task = self.loop.create_task(proc.wait())
Expand Down
1 change: 0 additions & 1 deletion tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import subprocess
import sys
import time
import uvloop

from uvloop import _testbase as tb

Expand Down
2 changes: 0 additions & 2 deletions tests/test_sockets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import asyncio
import socket
import uvloop

from uvloop import _testbase as tb

Expand Down
3 changes: 0 additions & 3 deletions tests/test_tcp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import asyncio
import logging
import socket
import uvloop
import ssl
import sys
import warnings

from uvloop import _testbase as tb

Expand Down
3 changes: 0 additions & 3 deletions tests/test_udp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import asyncio
import logging
import socket
import uvloop
import ssl
import sys
import warnings

from asyncio import test_utils
from uvloop import _testbase as tb
Expand Down
8 changes: 3 additions & 5 deletions tests/test_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import os
import socket
import tempfile
import uvloop
import unittest.mock

from uvloop import _testbase as tb

Expand Down Expand Up @@ -114,7 +112,6 @@ async def start_server_sock():
for srv_sock in srv_socks:
self.assertEqual(srv_sock.fileno(), -1)


self.loop.run_until_complete(start_server())
self.assertEqual(CNT, TOTAL_CNT)

Expand Down Expand Up @@ -246,7 +243,8 @@ def run(coro):
for _ in range(TOTAL_CNT):
tasks.append(coro(srv.addr))

self.loop.run_until_complete(asyncio.gather(*tasks, loop=self.loop))
self.loop.run_until_complete(
asyncio.gather(*tasks, loop=self.loop))
srv.join()
self.assertEqual(CNT, TOTAL_CNT)

Expand Down Expand Up @@ -293,7 +291,7 @@ async def client():

self.assertEqual(len(excs), 1)
self.assertIn(excs[0].__class__,
(BrokenPipeError, ConnectionResetError))
(BrokenPipeError, ConnectionResetError))

def test_transport_fromsock_get_extra_info(self):
async def test(sock):
Expand Down

0 comments on commit f894ac7

Please sign in to comment.