Skip to content

Commit

Permalink
Check closed in add fds
Browse files Browse the repository at this point in the history
  • Loading branch information
Insoleet authored and harvimt committed Jan 2, 2016
1 parent a34ab1e commit 30e555b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions quamash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ def time(self):

def add_reader(self, fd, callback, *args):
"""Register a callback for when a file descriptor is ready for reading."""
self._check_closed()

try:
existing = self._read_notifiers[fd]
except KeyError:
Expand Down Expand Up @@ -417,6 +419,7 @@ def remove_reader(self, fd):

def add_writer(self, fd, callback, *args):
"""Register a callback for when a file descriptor is ready for writing."""
self._check_closed()
try:
existing = self._write_notifiers[fd]
except KeyError:
Expand Down
35 changes: 24 additions & 11 deletions tests/test_qeventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,24 +399,37 @@ def can_read():

assert got_msg is None, 'Should not have received a read notification'

def test_can_remove_reader_after_closing(loop, sock_pair):
def test_remove_reader_after_closing(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
client_sock, srv_sock = sock_pair

nonlocal got_msg
got_msg = data
loop.add_reader(srv_sock.fileno(), lambda: None)
loop.close()
loop.remove_reader(srv_sock.fileno())

def test_remove_writer_after_closing(loop, sock_pair):
"""Verify that we can remove a reader callback from an event loop."""
client_sock, srv_sock = sock_pair

got_msg = None
loop.add_reader(srv_sock.fileno(), can_read)
loop.add_writer(client_sock.fileno(), lambda: None)
loop.close()
loop.remove_reader(srv_sock.fileno())
loop.remove_writer(client_sock.fileno())

assert got_msg is None, 'Should not have received a read notification'
def test_add_reader_after_closing(loop, sock_pair):
"""Verify that we can remove a reader callback from an event loop."""
client_sock, srv_sock = sock_pair

loop.close()
with pytest.raises(RuntimeError):
loop.add_reader(srv_sock.fileno(), lambda:None)

def test_add_writer_after_closing(loop, sock_pair):
"""Verify that we can remove a reader callback from an event loop."""
client_sock, srv_sock = sock_pair

loop.close()
with pytest.raises(RuntimeError):
loop.add_writer(client_sock.fileno(), lambda:None)

def test_can_add_writer(loop, sock_pair):
"""Verify that we can add a writer callback to an event loop."""
Expand Down

0 comments on commit 30e555b

Please sign in to comment.