Commit a51e0937 authored by Jason Madden's avatar Jason Madden

More selector tests

parent 8d001495
......@@ -5,9 +5,6 @@ try:
import selectors
except ImportError:
import selectors2 as selectors
import socket
import gevent
from gevent.monkey import patch_all
import gevent.testing as greentest
......@@ -16,9 +13,9 @@ patch_all()
from gevent.selectors import DefaultSelector
from gevent.selectors import GeventSelector
from gevent.tests.test__selectors import SelectorTestMixin
class TestSelectors(greentest.TestCase):
class TestSelectors(SelectorTestMixin, greentest.TestCase):
@greentest.skipOnPy2(
'selectors2 backport does not use _select'
......@@ -39,40 +36,10 @@ class TestSelectors(greentest.TestCase):
self.assertIs(selectors.DefaultSelector, GeventSelector)
def test_import_selectors(self):
# selectors can always be imported. On Python 2,
# selectors can always be imported once monkey-patched. On Python 2,
# this is an alias for gevent.selectors.
__import__('selectors')
def _check_selector(self, sel):
def read(conn, _mask):
data = conn.recv(100) # Should be ready
if data:
conn.send(data) # Hope it won't block
sel.unregister(conn)
conn.close()
def run_selector_once():
events = sel.select()
for key, mask in events:
key.data(key.fileobj, mask)
sock1, sock2 = socket.socketpair()
try:
sel.register(sock1, selectors.EVENT_READ, read)
glet = gevent.spawn(run_selector_once)
DATA = b'abcdef'
sock2.send(DATA)
data = sock2.recv(50)
self.assertEqual(data, DATA)
finally:
sel.close()
sock1.close()
sock2.close()
glet.join(10)
self.assertTrue(glet.ready())
def _make_test(name, kind): # pylint:disable=no-self-argument
if kind is None:
def m(self):
......
# Tests for gevent.selectors in its native form, without
# monkey-patching.
import gevent
from gevent import socket
from gevent import selectors
import gevent.testing as greentest
class SelectorTestMixin(object):
@staticmethod
def run_selector_once(sel):
# Run in a background greenlet, leaving the main
# greenlet free to send data.
events = sel.select(timeout=3)
for key, mask in events:
key.data(sel, key.fileobj, mask)
gevent.sleep()
unregister_after_send = True
def read_from_ready_socket_and_reply(self, selector, conn, _events):
data = conn.recv(100) # Should be ready
if data:
conn.send(data) # Hope it won't block
# Must unregister before we close.
if self.unregister_after_send:
selector.unregister(conn)
conn.close()
def _check_selector(self, sel):
server, client = socket.socketpair()
try:
sel.register(server, selectors.EVENT_READ, self.read_from_ready_socket_and_reply)
glet = gevent.spawn(self.run_selector_once, sel)
DATA = b'abcdef'
client.send(DATA)
data = client.recv(50) # here is probably where we yield to the event loop
self.assertEqual(data, DATA)
finally:
sel.close()
server.close()
client.close()
glet.join(10)
self.assertTrue(glet.ready())
class GeventSelectorTest(SelectorTestMixin,
greentest.TestCase):
def test_select_using_socketpair(self):
# Basic test.
with selectors.GeventSelector() as sel:
self._check_selector(sel)
def test_select_many_sockets(self):
pairs = [socket.socketpair() for _ in range(10)]
clients = [s[1] for s in pairs]
try:
server_sel = selectors.GeventSelector()
client_sel = selectors.GeventSelector()
for i, pair in enumerate(pairs):
server, client = pair
server_sel.register(server, selectors.EVENT_READ,
self.read_from_ready_socket_and_reply)
client_sel.register(client, selectors.EVENT_READ, i)
# Prime them all to be ready at once.
for i, client in enumerate(clients):
data = str(i).encode('ascii')
client.send(data)
# Read and reply to all the clients
self.run_selector_once(server_sel)
found = 0
for key, _ in client_sel.select(timeout=3):
expected = str(key.data).encode('ascii')
data = key.fileobj.recv(50)
self.assertEqual(data, expected)
found += 1
self.assertEqual(found, len(pairs))
finally:
server_sel.close()
client_sel.close()
for pair in pairs:
for s in pair:
s.close()
if __name__ == '__main__':
greentest.main()
......@@ -438,7 +438,7 @@ class TestMaxsize(TestCase):
pool.spawn(sleep, 0.2)
pool.spawn(sleep, 0.3)
gevent.sleep(0.2)
self.assertEqual(pool.size, 3)
self.assertGreaterEqual(pool.size, 2)
pool.maxsize = 0
gevent.sleep(0.2)
self.assertEqualFlakyRaceCondition(pool.size, 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment