[Pkg-bitcoin-commits] [python-quamash] 114/269: Implement remove_reader and remove_writer
Jonas Smedegaard
dr at jones.dk
Fri Nov 24 11:26:22 UTC 2017
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository python-quamash.
commit ed608e95b405318369b5f87c15b669b4076524fe
Author: Arve Knudsen <arve.knudsen at gmail.com>
Date: Thu Jul 24 13:01:30 2014 +0200
Implement remove_reader and remove_writer
---
quamash/__init__.py | 20 ++++++++----
tests/test_qeventloop.py | 84 +++++++++++++++++++++++++++++++++++-------------
2 files changed, 76 insertions(+), 28 deletions(-)
diff --git a/quamash/__init__.py b/quamash/__init__.py
index d92540f..b17e5c7 100644
--- a/quamash/__init__.py
+++ b/quamash/__init__.py
@@ -229,8 +229,8 @@ class QEventLoop(_baseclass):
self.__debug_enabled = False
self.__default_executor = None
self.__exception_handler = None
- self.__read_notifiers = {}
- self.__write_notifiers = {}
+ self._read_notifiers = {}
+ self._write_notifiers = {}
assert self.__app is not None
@@ -285,6 +285,10 @@ class QEventLoop(_baseclass):
self.__app = None
if self.__default_executor is not None:
self.__default_executor.shutdown()
+
+ self._read_notifiers = {}
+ self._write_notifiers = {}
+
super().close()
def call_later(self, delay, callback, *args):
@@ -331,10 +335,12 @@ class QEventLoop(_baseclass):
notifier.setEnabled(True)
self._logger.debug('Adding reader callback for file descriptor {}'.format(fd))
notifier.activated.connect(lambda: callback(*args))
- self.__read_notifiers[fd] = notifier
+ self._read_notifiers[fd] = notifier
def remove_reader(self, fd):
- raise NotImplementedError
+ """Remove reader callback."""
+ notifier = self._read_notifiers.pop(fd)
+ notifier.setEnabled(False)
def add_writer(self, fd, callback, *args):
"""Register a callback for when a file descriptor is ready for writing."""
@@ -342,10 +348,12 @@ class QEventLoop(_baseclass):
notifier.setEnabled(True)
self._logger.debug('Adding writer callback for file descriptor {}'.format(fd))
notifier.activated.connect(lambda: callback(*args))
- self.__write_notifiers[fd] = notifier
+ self._write_notifiers[fd] = notifier
def remove_writer(self, fd):
- raise NotImplementedError
+ """Remove writer callback."""
+ notifier = self._write_notifiers.pop(fd)
+ notifier.setEnabled(False)
# Methods for interacting with threads.
diff --git a/tests/test_qeventloop.py b/tests/test_qeventloop.py
index 472f71b..542a0e6 100644
--- a/tests/test_qeventloop.py
+++ b/tests/test_qeventloop.py
@@ -198,11 +198,21 @@ def test_get_set_debug(loop):
assert not loop.get_debug()
-def _create_sock_pair(port=0):
+ at pytest.fixture
+def sock_pair(request):
"""Create socket pair.
If socket.socketpair isn't available, we emulate it.
"""
+ def fin():
+ if client_sock is not None:
+ client_sock.close()
+ if srv_sock is not None:
+ srv_sock.close()
+
+ client_sock = srv_sock = None
+ request.addfinalizer(fin)
+
# See if socketpair() is available.
have_socketpair = hasattr(socket, 'socketpair')
if have_socketpair:
@@ -212,7 +222,7 @@ def _create_sock_pair(port=0):
# Create a non-blocking temporary server socket
temp_srv_sock = socket.socket()
temp_srv_sock.setblocking(False)
- temp_srv_sock.bind(('', port))
+ temp_srv_sock.bind(('', 0))
port = temp_srv_sock.getsockname()[1]
temp_srv_sock.listen(1)
@@ -238,7 +248,7 @@ def _create_sock_pair(port=0):
return client_sock, srv_sock
-def test_can_add_reader(loop):
+def test_can_add_reader(loop, sock_pair):
"""Verify that we can add a reader callback to an event loop."""
def can_read():
data = srv_sock.recv(1)
@@ -255,32 +265,62 @@ def test_can_add_reader(loop):
client_sock.close()
ref_msg = b'a'
- client_sock, srv_sock = _create_sock_pair()
- try:
- loop.call_soon(write)
+ client_sock, srv_sock = sock_pair
+ loop.call_soon(write)
- got_msg = None
- fut = asyncio.Future()
- loop.add_reader(srv_sock.fileno(), can_read)
- loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))
- finally:
- client_sock.close()
- srv_sock.close()
+ got_msg = None
+ fut = asyncio.Future()
+ loop.add_reader(srv_sock.fileno(), can_read)
+ assert len(loop._read_notifiers) == 1, 'Notifier should be added'
+ loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))
assert got_msg == ref_msg
-def test_can_add_writer(loop):
+def test_can_remove_reader(loop, sock_pair):
+ """Verify that we can remove a reader callback from an event loop."""
+ def can_read():
+ data = srv_sock.recv(1)
+ if len(data) != 1:
+ return
+
+ nonlocal got_msg
+ got_msg = data
+
+ client_sock, srv_sock = sock_pair
+
+ got_msg = None
+ loop.add_reader(srv_sock.fileno(), can_read)
+ loop.remove_reader(srv_sock.fileno())
+ assert not loop._read_notifiers, 'Notifier should be removed'
+ client_sock.send(b'a')
+ client_sock.close()
+ # Run for a short while to see if we get a read notification
+ loop.call_later(0.1, loop.stop)
+ loop.run_forever()
+
+ assert got_msg is None, 'Should not have received a read notification'
+
+
+def test_can_add_writer(loop, sock_pair):
"""Verify that we can add a writer callback to an event loop."""
def can_write():
# Indicate that we're done
fut.set_result(None)
- client_sock, srv_sock = _create_sock_pair()
- try:
- fut = asyncio.Future()
- loop.add_writer(client_sock.fileno(), can_write)
- loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))
- finally:
- client_sock.close()
- srv_sock.close()
+ client_sock, srv_sock = sock_pair
+ fut = asyncio.Future()
+ loop.add_writer(client_sock.fileno(), can_write)
+ assert len(loop._write_notifiers) == 1, 'Notifier should be added'
+ loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))
+
+
+def test_can_remove_writer(loop, sock_pair):
+ """Verify that we can remove a writer callback from an event loop."""
+ def can_write():
+ pass
+
+ client_sock, srv_sock = sock_pair
+ loop.add_writer(client_sock.fileno(), can_write)
+ loop.remove_writer(client_sock.fileno())
+ assert not loop._write_notifiers, 'Notifier should be removed'
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-bitcoin/python-quamash.git
More information about the Pkg-bitcoin-commits
mailing list