Skip to content

Commit

Permalink
handle: improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
koehlma committed Feb 9, 2016
1 parent 617905f commit ad33501
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
28 changes: 13 additions & 15 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from __future__ import print_function, unicode_literals, division, absolute_import

import contextlib
import os
import platform
import sys
Expand Down Expand Up @@ -80,21 +81,6 @@ def decorator(test):
class TestLoop(uv.Loop):
def __init__(self):
super(TestLoop, self).__init__()
self.callback_context = self
self.exc_type = None
self.exc_value = None
self.exc_traceback = None

def __enter__(self):
pass

def __exit__(self, exc_type, exc_value, exc_traceback):
if exc_type is not None and self.exc_type is None:
self.exc_type = exc_type
self.exc_value = exc_value
self.exc_traceback = exc_traceback
self.stop()
return True

def run(self, mode=uv.RunModes.DEFAULT):
self.exc_type = None
Expand Down Expand Up @@ -122,6 +108,18 @@ def set_up(self):
def tear_down(self):
pass

@contextlib.contextmanager
def should_raise(self, expected):
try:
yield
except Exception as exception:
if not isinstance(exception, expected):
import sys
exc_type, exc_value, exc_traceback = sys.exc_info()
reraise(exc_type, exc_value, exc_traceback)
else:
self.assert_false(True)

assert_true = unittest.TestCase.assertTrue
assert_false = unittest.TestCase.assertFalse

Expand Down
54 changes: 52 additions & 2 deletions tests/test_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,63 @@

from __future__ import print_function, unicode_literals, division, absolute_import

from common import TestCase
import common

import uv


class TestHandle(TestCase):
class TestHandle(common.TestCase):
def test_double_close(self):
self.prepare = uv.Prepare()
self.prepare.close()
self.prepare.close()

def test_active(self):
self.async = uv.Async()
self.assert_true(self.async.active)

def test_referencing(self):
self.async = uv.Async()
self.assert_true(self.async.referenced)
self.async.referenced = False
self.assert_false(self.async.referenced)
self.async.reference()
self.assert_true(self.async.referenced)
self.async.dereference()
self.assert_false(self.async.referenced)

def test_buffer_size(self):
self.tcp = uv.TCP()
self.tcp.bind((common.TEST_IPV4, common.TEST_PORT1))
self.tcp.send_buffer_size = 262144
self.assert_equal(self.tcp.send_buffer_size, 262144)
self.tcp.receive_buffer_size = 262144
self.assert_equal(self.tcp.receive_buffer_size, 262144)

def test_fileno(self):
self.tcp = uv.TCP()
self.tcp.bind((common.TEST_IPV4, common.TEST_PORT1))
self.assert_is_instance(self.tcp.fileno(), int)

def test_closed(self):
self.tcp = uv.TCP()
self.tcp.close()
self.assert_true(self.tcp.closing)
with self.should_raise(uv.ClosedHandleError):
self.tcp.referenced = True
with self.should_raise(uv.ClosedHandleError):
self.tcp.referenced = False
with self.should_raise(uv.ClosedHandleError):
send_buffer_size = self.tcp.send_buffer_size
with self.should_raise(uv.ClosedHandleError):
self.tcp.send_buffer_size = 42
with self.should_raise(uv.ClosedHandleError):
receive_buffer_size = self.tcp.receive_buffer_size
with self.should_raise(uv.ClosedHandleError):
self.tcp.receive_buffer_size = 42
self.assert_raises(uv.ClosedHandleError, self.tcp.fileno)
self.assert_is(self.tcp.close(), None)
self.loop.run()
self.assert_true(self.tcp.closed)
self.assert_false(self.tcp.active)
self.assert_false(self.tcp.referenced)
8 changes: 2 additions & 6 deletions uv/handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def __call__(self, cls):
def uv_alloc_cb(uv_handle, suggested_size, uv_buf):
handle = base.BaseHandle.detach(uv_handle)
""" :type: uv.Handle """
if handle is None:
if handle is None: # pragma: no cover
library.uv_buffer_set(uv_buf, ffi.NULL, 0)
else:
try:
handle.allocator.allocate(handle, suggested_size, uv_buf)
except Exception:
except Exception: # pragma: no cover
warnings.warn('exception in lib uv allocator')
library.uv_buffer_set(uv_buf, ffi.NULL, 0)

Expand Down Expand Up @@ -452,11 +452,7 @@ def close(self, on_closed=None):
if self.closing:
return
self.on_closed = on_closed or self.on_closed
# exclude the handle from garbage collection until it is closed
self.set_pending()
# the finalizer does not have to close the handle anymore
#common.detach_finalizer(self)
#lib.uv_close(self.uv_handle, uv_close_cb)
self.base_handle.close()

def set_pending(self):
Expand Down

0 comments on commit ad33501

Please sign in to comment.