PageRenderTime 67ms CodeModel.GetById 3ms app.highlight 58ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/greenio_test.py

https://bitbucket.org/opentekhnia/eventlet
Python | 866 lines | 828 code | 20 blank | 18 comment | 10 complexity | b81287ffdef98c59edd7596c1d54d6bc MD5 | raw file
  1import socket as _orig_sock
  2from tests import LimitedTestCase, skip_with_pyevent, main, skipped, s2b, skip_if, skip_on_windows
  3from eventlet import event, greenio, debug
  4from eventlet.hubs import get_hub
  5from eventlet.green import socket, time
  6from eventlet.support import get_errno
  7
  8import array
  9import errno
 10import eventlet
 11import fcntl
 12import os
 13import sys
 14import tempfile, shutil
 15
 16
 17def bufsized(sock, size=1):
 18    """ Resize both send and receive buffers on a socket.
 19    Useful for testing trampoline.  Returns the socket.
 20
 21    >>> import socket
 22    >>> sock = bufsized(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
 23    """
 24    sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, size)
 25    sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, size)
 26    return sock
 27
 28
 29def min_buf_size():
 30    """Return the minimum buffer size that the platform supports."""
 31    test_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 32    test_sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1)
 33    return test_sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
 34
 35
 36def using_epoll_hub(_f):
 37        try:
 38            return 'epolls' in type(get_hub()).__module__
 39        except Exception:
 40            return False
 41
 42
 43class TestGreenSocket(LimitedTestCase):
 44    def assertWriteToClosedFileRaises(self, fd):
 45        if sys.version_info[0] < 3:
 46            # 2.x socket._fileobjects are odd: writes don't check
 47            # whether the socket is closed or not, and you get an
 48            # AttributeError during flush if it is closed
 49            fd.write('a')
 50            self.assertRaises(Exception, fd.flush)
 51        else:
 52            # 3.x io write to closed file-like pbject raises ValueError
 53            self.assertRaises(ValueError, fd.write, 'a')
 54
 55    def test_connect_timeout(self):
 56        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 57        s.settimeout(0.1)
 58        gs = greenio.GreenSocket(s)
 59        try:
 60            gs.connect(('192.0.2.1', 80))
 61            self.fail("socket.timeout not raised")
 62        except socket.timeout, e:
 63            self.assert_(hasattr(e, 'args'))
 64            self.assertEqual(e.args[0], 'timed out')
 65        except socket.error, e:
 66            # unreachable is also a valid outcome
 67            if not get_errno(e) in (errno.EHOSTUNREACH, errno.ENETUNREACH):
 68                raise
 69
 70    def test_accept_timeout(self):
 71        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 72        s.bind(('', 0))
 73        s.listen(50)
 74
 75        s.settimeout(0.1)
 76        gs = greenio.GreenSocket(s)
 77        try:
 78            gs.accept()
 79            self.fail("socket.timeout not raised")
 80        except socket.timeout, e:
 81            self.assert_(hasattr(e, 'args'))
 82            self.assertEqual(e.args[0], 'timed out')
 83
 84    def test_connect_ex_timeout(self):
 85        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 86        s.settimeout(0.1)
 87        gs = greenio.GreenSocket(s)
 88        e = gs.connect_ex(('192.0.2.1', 80))
 89        if not e in (errno.EHOSTUNREACH, errno.ENETUNREACH):
 90            self.assertEquals(e, errno.EAGAIN)
 91
 92    def test_recv_timeout(self):
 93        listener = greenio.GreenSocket(socket.socket())
 94        listener.bind(('', 0))
 95        listener.listen(50)
 96
 97        evt = event.Event()
 98
 99        def server():
100            # accept the connection in another greenlet
101            sock, addr = listener.accept()
102            evt.wait()
103
104        gt = eventlet.spawn(server)
105
106        addr = listener.getsockname()
107
108        client = greenio.GreenSocket(socket.socket())
109        client.settimeout(0.1)
110
111        client.connect(addr)
112
113        try:
114            client.recv(8192)
115            self.fail("socket.timeout not raised")
116        except socket.timeout, e:
117            self.assert_(hasattr(e, 'args'))
118            self.assertEqual(e.args[0], 'timed out')
119
120        evt.send()
121        gt.wait()
122
123    def test_recvfrom_timeout(self):
124        gs = greenio.GreenSocket(
125            socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
126        gs.settimeout(.1)
127        gs.bind(('', 0))
128
129        try:
130            gs.recvfrom(8192)
131            self.fail("socket.timeout not raised")
132        except socket.timeout, e:
133            self.assert_(hasattr(e, 'args'))
134            self.assertEqual(e.args[0], 'timed out')
135
136    def test_recvfrom_into_timeout(self):
137        buf = buffer(array.array('B'))
138
139        gs = greenio.GreenSocket(
140            socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
141        gs.settimeout(.1)
142        gs.bind(('', 0))
143
144        try:
145            gs.recvfrom_into(buf)
146            self.fail("socket.timeout not raised")
147        except socket.timeout, e:
148            self.assert_(hasattr(e, 'args'))
149            self.assertEqual(e.args[0], 'timed out')
150
151    def test_recv_into_timeout(self):
152        buf = buffer(array.array('B'))
153
154        listener = greenio.GreenSocket(socket.socket())
155        listener.bind(('', 0))
156        listener.listen(50)
157
158        evt = event.Event()
159
160        def server():
161            # accept the connection in another greenlet
162            sock, addr = listener.accept()
163            evt.wait()
164
165        gt = eventlet.spawn(server)
166
167        addr = listener.getsockname()
168
169        client = greenio.GreenSocket(socket.socket())
170        client.settimeout(0.1)
171
172        client.connect(addr)
173
174        try:
175            client.recv_into(buf)
176            self.fail("socket.timeout not raised")
177        except socket.timeout, e:
178            self.assert_(hasattr(e, 'args'))
179            self.assertEqual(e.args[0], 'timed out')
180
181        evt.send()
182        gt.wait()
183
184    def test_send_timeout(self):
185        listener = bufsized(eventlet.listen(('', 0)))
186
187        evt = event.Event()
188
189        def server():
190            # accept the connection in another greenlet
191            sock, addr = listener.accept()
192            sock = bufsized(sock)
193            evt.wait()
194
195        gt = eventlet.spawn(server)
196
197        addr = listener.getsockname()
198
199        client = bufsized(greenio.GreenSocket(socket.socket()))
200        client.connect(addr)
201        try:
202            client.settimeout(0.00001)
203            msg = s2b("A") * 100000  # large enough number to overwhelm most buffers
204
205            total_sent = 0
206            # want to exceed the size of the OS buffer so it'll block in a
207            # single send
208            for x in range(10):
209                total_sent += client.send(msg)
210            self.fail("socket.timeout not raised")
211        except socket.timeout, e:
212            self.assert_(hasattr(e, 'args'))
213            self.assertEqual(e.args[0], 'timed out')
214
215        evt.send()
216        gt.wait()
217
218    def test_sendall_timeout(self):
219        listener = greenio.GreenSocket(socket.socket())
220        listener.bind(('', 0))
221        listener.listen(50)
222
223        evt = event.Event()
224
225        def server():
226            # accept the connection in another greenlet
227            sock, addr = listener.accept()
228            evt.wait()
229
230        gt = eventlet.spawn(server)
231
232        addr = listener.getsockname()
233
234        client = greenio.GreenSocket(socket.socket())
235        client.settimeout(0.1)
236        client.connect(addr)
237
238        try:
239            msg = s2b("A") * (8 << 20)
240
241            # want to exceed the size of the OS buffer so it'll block
242            client.sendall(msg)
243            self.fail("socket.timeout not raised")
244        except socket.timeout, e:
245            self.assert_(hasattr(e, 'args'))
246            self.assertEqual(e.args[0], 'timed out')
247
248        evt.send()
249        gt.wait()
250
251    def test_close_with_makefile(self):
252        def accept_close_early(listener):
253            # verify that the makefile and the socket are truly independent
254            # by closing the socket prior to using the made file
255            try:
256                conn, addr = listener.accept()
257                fd = conn.makefile('w')
258                conn.close()
259                fd.write('hello\n')
260                fd.close()
261                self.assertWriteToClosedFileRaises(fd)
262                self.assertRaises(socket.error, conn.send, s2b('b'))
263            finally:
264                listener.close()
265
266        def accept_close_late(listener):
267            # verify that the makefile and the socket are truly independent
268            # by closing the made file and then sending a character
269            try:
270                conn, addr = listener.accept()
271                fd = conn.makefile('w')
272                fd.write('hello')
273                fd.close()
274                conn.send(s2b('\n'))
275                conn.close()
276                self.assertWriteToClosedFileRaises(fd)
277                self.assertRaises(socket.error, conn.send, s2b('b'))
278            finally:
279                listener.close()
280
281        def did_it_work(server):
282            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
283            client.connect(('127.0.0.1', server.getsockname()[1]))
284            fd = client.makefile()
285            client.close()
286            assert fd.readline() == 'hello\n'
287            assert fd.read() == ''
288            fd.close()
289
290        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
291        server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
292        server.bind(('0.0.0.0', 0))
293        server.listen(50)
294        killer = eventlet.spawn(accept_close_early, server)
295        did_it_work(server)
296        killer.wait()
297
298        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
299        server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
300        server.bind(('0.0.0.0', 0))
301        server.listen(50)
302        killer = eventlet.spawn(accept_close_late, server)
303        did_it_work(server)
304        killer.wait()
305
306    def test_del_closes_socket(self):
307        def accept_once(listener):
308            # delete/overwrite the original conn
309            # object, only keeping the file object around
310            # closing the file object should close everything
311            try:
312                conn, addr = listener.accept()
313                conn = conn.makefile('w')
314                conn.write('hello\n')
315                conn.close()
316                self.assertWriteToClosedFileRaises(conn)
317            finally:
318                listener.close()
319
320        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
321        server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
322        server.bind(('127.0.0.1', 0))
323        server.listen(50)
324        killer = eventlet.spawn(accept_once, server)
325        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
326        client.connect(('127.0.0.1', server.getsockname()[1]))
327        fd = client.makefile()
328        client.close()
329        assert fd.read() == 'hello\n'
330        assert fd.read() == ''
331
332        killer.wait()
333
334    def test_full_duplex(self):
335        large_data = s2b('*') * 10 * min_buf_size()
336        listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
337        listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
338        listener.bind(('127.0.0.1', 0))
339        listener.listen(50)
340        bufsized(listener)
341
342        def send_large(sock):
343            sock.sendall(large_data)
344
345        def read_large(sock):
346            result = sock.recv(len(large_data))
347            while len(result) < len(large_data):
348                result += sock.recv(len(large_data))
349            self.assertEquals(result, large_data)
350
351        def server():
352            (sock, addr) = listener.accept()
353            sock = bufsized(sock)
354            send_large_coro = eventlet.spawn(send_large, sock)
355            eventlet.sleep(0)
356            result = sock.recv(10)
357            expected = s2b('hello world')
358            while len(result) < len(expected):
359                result += sock.recv(10)
360            self.assertEquals(result, expected)
361            send_large_coro.wait()
362
363        server_evt = eventlet.spawn(server)
364        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
365        client.connect(('127.0.0.1', listener.getsockname()[1]))
366        bufsized(client)
367        large_evt = eventlet.spawn(read_large, client)
368        eventlet.sleep(0)
369        client.sendall(s2b('hello world'))
370        server_evt.wait()
371        large_evt.wait()
372        client.close()
373
374    def test_sendall(self):
375        # test adapted from Marcus Cavanaugh's email
376        # it may legitimately take a while, but will eventually complete
377        self.timer.cancel()
378        second_bytes = 10
379
380        def test_sendall_impl(many_bytes):
381            bufsize = max(many_bytes // 15, 2)
382
383            def sender(listener):
384                (sock, addr) = listener.accept()
385                sock = bufsized(sock, size=bufsize)
386                sock.sendall(s2b('x') * many_bytes)
387                sock.sendall(s2b('y') * second_bytes)
388
389            listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
390            listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
391            listener.bind(("", 0))
392            listener.listen(50)
393            sender_coro = eventlet.spawn(sender, listener)
394            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
395            client.connect(('127.0.0.1', listener.getsockname()[1]))
396            bufsized(client, size=bufsize)
397            total = 0
398            while total < many_bytes:
399                data = client.recv(min(many_bytes - total, many_bytes // 10))
400                if not data:
401                    break
402                total += len(data)
403
404            total2 = 0
405            while total < second_bytes:
406                data = client.recv(second_bytes)
407                if not data:
408                    break
409                total2 += len(data)
410
411            sender_coro.wait()
412            client.close()
413
414        for how_many in (1000, 10000, 100000, 1000000):
415            test_sendall_impl(how_many)
416
417    def test_wrap_socket(self):
418        try:
419            import ssl
420        except ImportError:
421            pass  # pre-2.6
422        else:
423            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
424            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
425            sock.bind(('127.0.0.1', 0))
426            sock.listen(50)
427            ssl.wrap_socket(sock)
428
429    def test_timeout_and_final_write(self):
430        # This test verifies that a write on a socket that we've
431        # stopped listening for doesn't result in an incorrect switch
432        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
433        server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
434        server.bind(('127.0.0.1', 0))
435        server.listen(50)
436        bound_port = server.getsockname()[1]
437
438        def sender(evt):
439            s2, addr = server.accept()
440            wrap_wfile = s2.makefile('w')
441
442            eventlet.sleep(0.02)
443            wrap_wfile.write('hi')
444            s2.close()
445            evt.send('sent via event')
446
447        evt = event.Event()
448        eventlet.spawn(sender, evt)
449        eventlet.sleep(0)  # lets the socket enter accept mode, which
450                      # is necessary for connect to succeed on windows
451        try:
452            # try and get some data off of this pipe
453            # but bail before any is sent
454            eventlet.Timeout(0.01)
455            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
456            client.connect(('127.0.0.1', bound_port))
457            wrap_rfile = client.makefile()
458            wrap_rfile.read(1)
459            self.fail()
460        except eventlet.TimeoutError:
461            pass
462
463        result = evt.wait()
464        self.assertEquals(result, 'sent via event')
465        server.close()
466        client.close()
467
468    @skip_with_pyevent
469    def test_raised_multiple_readers(self):
470        debug.hub_prevent_multiple_readers(True)
471
472        def handle(sock, addr):
473            sock.recv(1)
474            sock.sendall("a")
475            raise eventlet.StopServe()
476
477        listener = eventlet.listen(('127.0.0.1', 0))
478        eventlet.spawn(eventlet.serve, listener, handle)
479
480        def reader(s):
481            s.recv(1)
482
483        s = eventlet.connect(('127.0.0.1', listener.getsockname()[1]))
484        a = eventlet.spawn(reader, s)
485        eventlet.sleep(0)
486        self.assertRaises(RuntimeError, s.recv, 1)
487        s.sendall('b')
488        a.wait()
489
490    @skip_with_pyevent
491    @skip_if(using_epoll_hub)
492    def test_closure(self):
493        def spam_to_me(address):
494            sock = eventlet.connect(address)
495            while True:
496                try:
497                    sock.sendall('hello world')
498                except socket.error, e:
499                    if get_errno(e) == errno.EPIPE:
500                        return
501                    raise
502
503        server = eventlet.listen(('127.0.0.1', 0))
504        sender = eventlet.spawn(spam_to_me, server.getsockname())
505        client, address = server.accept()
506        server.close()
507
508        def reader():
509            try:
510                while True:
511                    data = client.recv(1024)
512                    self.assert_(data)
513            except socket.error, e:
514                # we get an EBADF because client is closed in the same process
515                # (but a different greenthread)
516                if get_errno(e) != errno.EBADF:
517                    raise
518
519        def closer():
520            client.close()
521
522        reader = eventlet.spawn(reader)
523        eventlet.spawn_n(closer)
524        reader.wait()
525        sender.wait()
526
527    def test_invalid_connection(self):
528        # find an unused port by creating a socket then closing it
529        port = eventlet.listen(('127.0.0.1', 0)).getsockname()[1]
530        self.assertRaises(socket.error, eventlet.connect, ('127.0.0.1', port))
531
532    def test_zero_timeout_and_back(self):
533        listen = eventlet.listen(('', 0))
534        # Keep reference to server side of socket
535        server = eventlet.spawn(listen.accept)
536        client = eventlet.connect(listen.getsockname())
537
538        client.settimeout(0.05)
539        # Now must raise socket.timeout
540        self.assertRaises(socket.timeout, client.recv, 1)
541
542        client.settimeout(0)
543        # Now must raise socket.error with EAGAIN
544        try:
545            client.recv(1)
546            assert False
547        except socket.error, e:
548            assert get_errno(e) == errno.EAGAIN
549
550        client.settimeout(0.05)
551        # Now socket.timeout again
552        self.assertRaises(socket.timeout, client.recv, 1)
553        server.wait()
554
555    def test_default_nonblocking(self):
556        sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
557        flags = fcntl.fcntl(sock1.fd.fileno(), fcntl.F_GETFL)
558        assert flags & os.O_NONBLOCK
559
560        sock2 = socket.socket(sock1.fd)
561        flags = fcntl.fcntl(sock2.fd.fileno(), fcntl.F_GETFL)
562        assert flags & os.O_NONBLOCK
563
564    def test_dup_nonblocking(self):
565        sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
566        flags = fcntl.fcntl(sock1.fd.fileno(), fcntl.F_GETFL)
567        assert flags & os.O_NONBLOCK
568
569        sock2 = sock1.dup()
570        flags = fcntl.fcntl(sock2.fd.fileno(), fcntl.F_GETFL)
571        assert flags & os.O_NONBLOCK
572
573    def test_skip_nonblocking(self):
574        sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
575        fd = sock1.fd.fileno()
576        flags = fcntl.fcntl(fd, fcntl.F_GETFL)
577        flags = fcntl.fcntl(fd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
578        assert flags & os.O_NONBLOCK == 0
579
580        sock2 = socket.socket(sock1.fd, set_nonblocking=False)
581        flags = fcntl.fcntl(sock2.fd.fileno(), fcntl.F_GETFL)
582        assert flags & os.O_NONBLOCK == 0
583
584
585class TestGreenPipe(LimitedTestCase):
586    @skip_on_windows
587    def setUp(self):
588        super(self.__class__, self).setUp()
589        self.tempdir = tempfile.mkdtemp('_green_pipe_test')
590
591    def tearDown(self):
592        shutil.rmtree(self.tempdir)
593        super(self.__class__, self).tearDown()
594
595    def test_pipe(self):
596        r, w = os.pipe()
597        rf = greenio.GreenPipe(r, 'r')
598        wf = greenio.GreenPipe(w, 'w', 0)
599
600        def sender(f, content):
601            for ch in content:
602                eventlet.sleep(0.0001)
603                f.write(ch)
604            f.close()
605
606        one_line = "12345\n"
607        eventlet.spawn(sender, wf, one_line * 5)
608        for i in xrange(5):
609            line = rf.readline()
610            eventlet.sleep(0.01)
611            self.assertEquals(line, one_line)
612        self.assertEquals(rf.readline(), '')
613
614    def test_pipe_read(self):
615        # ensure that 'readline' works properly on GreenPipes when data is not
616        # immediately available (fd is nonblocking, was raising EAGAIN)
617        # also ensures that readline() terminates on '\n' and '\r\n'
618        r, w = os.pipe()
619
620        r = greenio.GreenPipe(r)
621        w = greenio.GreenPipe(w, 'w')
622
623        def writer():
624            eventlet.sleep(.1)
625
626            w.write('line\n')
627            w.flush()
628
629            w.write('line\r\n')
630            w.flush()
631
632        gt = eventlet.spawn(writer)
633
634        eventlet.sleep(0)
635
636        line = r.readline()
637        self.assertEquals(line, 'line\n')
638
639        line = r.readline()
640        self.assertEquals(line, 'line\r\n')
641
642        gt.wait()
643
644    def test_pipe_writes_large_messages(self):
645        r, w = os.pipe()
646
647        r = greenio.GreenPipe(r)
648        w = greenio.GreenPipe(w, 'w')
649
650        large_message = "".join([1024 * chr(i) for i in xrange(65)])
651
652        def writer():
653            w.write(large_message)
654            w.close()
655
656        gt = eventlet.spawn(writer)
657
658        for i in xrange(65):
659            buf = r.read(1024)
660            expected = 1024 * chr(i)
661            self.assertEquals(buf, expected,
662                "expected=%r..%r, found=%r..%r iter=%d"
663                % (expected[:4], expected[-4:], buf[:4], buf[-4:], i))
664        gt.wait()
665
666    def test_seek_on_buffered_pipe(self):
667        f = greenio.GreenPipe(self.tempdir + "/TestFile", 'w+', 1024)
668        self.assertEquals(f.tell(), 0)
669        f.seek(0, 2)
670        self.assertEquals(f.tell(), 0)
671        f.write('1234567890')
672        f.seek(0, 2)
673        self.assertEquals(f.tell(), 10)
674        f.seek(0)
675        value = f.read(1)
676        self.assertEqual(value, '1')
677        self.assertEquals(f.tell(), 1)
678        value = f.read(1)
679        self.assertEqual(value, '2')
680        self.assertEquals(f.tell(), 2)
681        f.seek(0, 1)
682        self.assertEqual(f.readline(), '34567890')
683        f.seek(-5, 1)
684        self.assertEqual(f.readline(), '67890')
685        f.seek(0)
686        self.assertEqual(f.readline(), '1234567890')
687        f.seek(0, 2)
688        self.assertEqual(f.readline(), '')
689
690    def test_truncate(self):
691        f = greenio.GreenPipe(self.tempdir + "/TestFile", 'w+', 1024)
692        f.write('1234567890')
693        f.truncate(9)
694        self.assertEquals(f.tell(), 9)
695
696
697class TestGreenIoLong(LimitedTestCase):
698    TEST_TIMEOUT = 10  # the test here might take a while depending on the OS
699
700    @skip_with_pyevent
701    def test_multiple_readers(self, clibufsize=False):
702        debug.hub_prevent_multiple_readers(False)
703        recvsize = 2 * min_buf_size()
704        sendsize = 10 * recvsize
705
706        # test that we can have multiple coroutines reading
707        # from the same fd.  We make no guarantees about which one gets which
708        # bytes, but they should both get at least some
709        def reader(sock, results):
710            while True:
711                data = sock.recv(recvsize)
712                if not data:
713                    break
714                results.append(data)
715
716        results1 = []
717        results2 = []
718        listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
719        listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
720        listener.bind(('127.0.0.1', 0))
721        listener.listen(50)
722
723        def server():
724            (sock, addr) = listener.accept()
725            sock = bufsized(sock)
726            try:
727                c1 = eventlet.spawn(reader, sock, results1)
728                c2 = eventlet.spawn(reader, sock, results2)
729                try:
730                    c1.wait()
731                    c2.wait()
732                finally:
733                    c1.kill()
734                    c2.kill()
735            finally:
736                sock.close()
737
738        server_coro = eventlet.spawn(server)
739        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
740        client.connect(('127.0.0.1', listener.getsockname()[1]))
741        if clibufsize:
742            bufsized(client, size=sendsize)
743        else:
744            bufsized(client)
745        client.sendall(s2b('*') * sendsize)
746        client.close()
747        server_coro.wait()
748        listener.close()
749        self.assert_(len(results1) > 0)
750        self.assert_(len(results2) > 0)
751        debug.hub_prevent_multiple_readers()
752
753    @skipped  # by rdw because it fails but it's not clear how to make it pass
754    @skip_with_pyevent
755    def test_multiple_readers2(self):
756        self.test_multiple_readers(clibufsize=True)
757
758
759class TestGreenIoStarvation(LimitedTestCase):
760    # fixme: this doesn't succeed, because of eventlet's predetermined
761    # ordering.  two processes, one with server, one with client eventlets
762    # might be more reliable?
763
764    TEST_TIMEOUT = 300  # the test here might take a while depending on the OS
765
766    @skipped  # by rdw, because it fails but it's not clear how to make it pass
767    @skip_with_pyevent
768    def test_server_starvation(self, sendloops=15):
769        recvsize = 2 * min_buf_size()
770        sendsize = 10000 * recvsize
771
772        results = [[] for i in xrange(5)]
773
774        listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
775        listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
776        listener.bind(('127.0.0.1', 0))
777        port = listener.getsockname()[1]
778        listener.listen(50)
779
780        base_time = time.time()
781
782        def server(my_results):
783            sock, addr = listener.accept()
784
785            datasize = 0
786
787            t1 = None
788            t2 = None
789            try:
790                while True:
791                    data = sock.recv(recvsize)
792                    if not t1:
793                        t1 = time.time() - base_time
794                    if not data:
795                        t2 = time.time() - base_time
796                        my_results.append(datasize)
797                        my_results.append((t1, t2))
798                        break
799                    datasize += len(data)
800            finally:
801                sock.close()
802
803        def client():
804            pid = os.fork()
805            if pid:
806                return pid
807
808            client = _orig_sock.socket(socket.AF_INET, socket.SOCK_STREAM)
809            client.connect(('127.0.0.1', port))
810
811            bufsized(client, size=sendsize)
812
813            for i in range(sendloops):
814                client.sendall(s2b('*') * sendsize)
815            client.close()
816            os._exit(0)
817
818        clients = []
819        servers = []
820        for r in results:
821            servers.append(eventlet.spawn(server, r))
822        for r in results:
823            clients.append(client())
824
825        for s in servers:
826            s.wait()
827        for c in clients:
828            os.waitpid(c, 0)
829
830        listener.close()
831
832        # now test that all of the server receive intervals overlap, and
833        # that there were no errors.
834        for r in results:
835            assert len(r) == 2, "length is %d not 2!: %s\n%s" % (len(r), r, results)
836            assert r[0] == sendsize * sendloops
837            assert len(r[1]) == 2
838            assert r[1][0] is not None
839            assert r[1][1] is not None
840
841        starttimes = sorted(r[1][0] for r in results)
842        endtimes = sorted(r[1][1] for r in results)
843        runlengths = sorted(r[1][1] - r[1][0] for r in results)
844
845        # assert that the last task started before the first task ended
846        # (our no-starvation condition)
847        assert starttimes[-1] < endtimes[0], "Not overlapping: starts %s ends %s" % (starttimes, endtimes)
848
849        maxstartdiff = starttimes[-1] - starttimes[0]
850
851        assert maxstartdiff * 2 < runlengths[0], "Largest difference in starting times more than twice the shortest running time!"
852        assert runlengths[0] * 2 > runlengths[-1], "Longest runtime more than twice as long as shortest!"
853
854
855def test_set_nonblocking():
856    sock = _orig_sock.socket(socket.AF_INET, socket.SOCK_DGRAM)
857    fileno = sock.fileno()
858    orig_flags = fcntl.fcntl(fileno, fcntl.F_GETFL)
859    assert orig_flags & os.O_NONBLOCK == 0
860    greenio.set_nonblocking(sock)
861    new_flags = fcntl.fcntl(fileno, fcntl.F_GETFL)
862    assert new_flags == (orig_flags | os.O_NONBLOCK)
863
864
865if __name__ == '__main__':
866    main()