OLD | NEW |
1 """Tests for events.py.""" | 1 """Tests for events.py.""" |
2 | 2 |
3 import concurrent.futures | 3 import concurrent.futures |
4 import gc | 4 import gc |
5 import io | 5 import io |
6 import os | 6 import os |
7 import re | 7 import re |
8 import signal | 8 import signal |
9 import socket | 9 import socket |
10 try: | 10 try: |
11 import ssl | 11 import ssl |
12 except ImportError: | 12 except ImportError: |
13 ssl = None | 13 ssl = None |
14 import sys | 14 import sys |
15 import threading | 15 import threading |
16 import time | 16 import time |
| 17 import errno |
17 import unittest | 18 import unittest |
18 import unittest.mock | 19 import unittest.mock |
| 20 from test.support import find_unused_port |
19 | 21 |
20 | 22 |
21 from tulip import futures | 23 from tulip import futures |
22 from tulip import events | 24 from tulip import events |
23 from tulip import transports | 25 from tulip import transports |
24 from tulip import protocols | 26 from tulip import protocols |
25 from tulip import selector_events | 27 from tulip import selector_events |
26 from tulip import tasks | 28 from tulip import tasks |
27 from tulip import test_utils | 29 from tulip import test_utils |
28 | 30 |
(...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 | 684 |
683 def test_start_serving(self): | 685 def test_start_serving(self): |
684 proto = None | 686 proto = None |
685 | 687 |
686 def factory(): | 688 def factory(): |
687 nonlocal proto | 689 nonlocal proto |
688 proto = MyProto() | 690 proto = MyProto() |
689 return proto | 691 return proto |
690 | 692 |
691 f = self.event_loop.start_serving(factory, '0.0.0.0', 0) | 693 f = self.event_loop.start_serving(factory, '0.0.0.0', 0) |
692 sock = self.event_loop.run_until_complete(f) | 694 socks = self.event_loop.run_until_complete(f) |
| 695 self.assertEqual(len(socks), 1) |
| 696 sock = socks[0] |
693 host, port = sock.getsockname() | 697 host, port = sock.getsockname() |
694 self.assertEqual(host, '0.0.0.0') | 698 self.assertEqual(host, '0.0.0.0') |
695 client = socket.socket() | 699 client = socket.socket() |
696 client.connect(('127.0.0.1', port)) | 700 client.connect(('127.0.0.1', port)) |
697 client.send(b'xxx') | 701 client.send(b'xxx') |
698 self.event_loop.run_once() | 702 self.event_loop.run_once() |
699 self.assertIsInstance(proto, MyProto) | 703 self.assertIsInstance(proto, MyProto) |
700 self.assertEqual('INITIAL', proto.state) | 704 self.assertEqual('INITIAL', proto.state) |
701 self.event_loop.run_once() | 705 self.event_loop.run_once() |
702 self.assertEqual('CONNECTED', proto.state) | 706 self.assertEqual('CONNECTED', proto.state) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 | 741 |
738 here = os.path.dirname(__file__) | 742 here = os.path.dirname(__file__) |
739 sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23) | 743 sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
740 sslcontext.load_cert_chain( | 744 sslcontext.load_cert_chain( |
741 certfile=os.path.join(here, 'sample.crt'), | 745 certfile=os.path.join(here, 'sample.crt'), |
742 keyfile=os.path.join(here, 'sample.key')) | 746 keyfile=os.path.join(here, 'sample.key')) |
743 | 747 |
744 f = self.event_loop.start_serving( | 748 f = self.event_loop.start_serving( |
745 factory, '127.0.0.1', 0, ssl=sslcontext) | 749 factory, '127.0.0.1', 0, ssl=sslcontext) |
746 | 750 |
747 sock = self.event_loop.run_until_complete(f) | 751 sock = self.event_loop.run_until_complete(f)[0] |
748 host, port = sock.getsockname() | 752 host, port = sock.getsockname() |
749 self.assertEqual(host, '127.0.0.1') | 753 self.assertEqual(host, '127.0.0.1') |
750 | 754 |
751 f_c = self.event_loop.create_connection( | 755 f_c = self.event_loop.create_connection( |
752 ClientMyProto, host, port, ssl=True) | 756 ClientMyProto, host, port, ssl=True) |
753 client, pr = self.event_loop.run_until_complete(f_c) | 757 client, pr = self.event_loop.run_until_complete(f_c) |
754 | 758 |
755 client.write(b'xxx') | 759 client.write(b'xxx') |
756 self.event_loop.run_once() | 760 self.event_loop.run_once() |
757 self.assertIsInstance(proto, MyProto) | 761 self.assertIsInstance(proto, MyProto) |
(...skipping 23 matching lines...) Expand all Loading... |
781 class TestMyProto(MyProto): | 785 class TestMyProto(MyProto): |
782 def __init__(self): | 786 def __init__(self): |
783 super().__init__() | 787 super().__init__() |
784 proto.set_result(self) | 788 proto.set_result(self) |
785 | 789 |
786 sock_ob = socket.socket(type=socket.SOCK_STREAM) | 790 sock_ob = socket.socket(type=socket.SOCK_STREAM) |
787 sock_ob.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | 791 sock_ob.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
788 sock_ob.bind(('0.0.0.0', 0)) | 792 sock_ob.bind(('0.0.0.0', 0)) |
789 | 793 |
790 f = self.event_loop.start_serving(TestMyProto, sock=sock_ob) | 794 f = self.event_loop.start_serving(TestMyProto, sock=sock_ob) |
791 sock = self.event_loop.run_until_complete(f) | 795 sock = self.event_loop.run_until_complete(f)[0] |
792 self.assertIs(sock, sock_ob) | 796 self.assertIs(sock, sock_ob) |
793 | 797 |
794 host, port = sock.getsockname() | 798 host, port = sock.getsockname() |
795 self.assertEqual(host, '0.0.0.0') | 799 self.assertEqual(host, '0.0.0.0') |
796 client = socket.socket() | 800 client = socket.socket() |
797 client.connect(('127.0.0.1', port)) | 801 client.connect(('127.0.0.1', port)) |
798 client.send(b'xxx') | 802 client.send(b'xxx') |
799 self.event_loop.run_until_complete(proto) | 803 self.event_loop.run_until_complete(proto) |
800 sock.close() | |
801 client.close() | 804 client.close() |
802 | 805 |
| 806 f = self.event_loop.start_serving(MyProto, host=host, port=port) |
| 807 with self.assertRaises(socket.error) as cm: |
| 808 self.event_loop.run_until_complete(f) |
| 809 self.assertEqual(cm.exception.errno, errno.EADDRINUSE) |
| 810 |
| 811 @unittest.skipUnless(socket.has_ipv6, 'IPv6 not supported') |
| 812 def test_start_serving_dual_stack(self): |
| 813 port = find_unused_port() |
| 814 f = self.event_loop.start_serving(MyProto, host=None, port=port) |
| 815 socks = self.event_loop.run_until_complete(f) |
| 816 with socket.socket() as client: |
| 817 client.connect(('127.0.0.1', port)) |
| 818 client.send(b'xxx') |
| 819 self.event_loop.run_once() |
| 820 self.event_loop.run_once() |
| 821 with socket.socket(socket.AF_INET6) as client: |
| 822 client.connect(('::1', port)) |
| 823 client.send(b'xxx') |
| 824 self.event_loop.run_once() |
| 825 self.event_loop.run_once() |
| 826 for s in socks: |
| 827 s.close() |
| 828 |
803 def test_stop_serving(self): | 829 def test_stop_serving(self): |
804 f = self.event_loop.start_serving(MyProto, '0.0.0.0', 0) | 830 f = self.event_loop.start_serving(MyProto, '0.0.0.0', 0) |
805 sock = self.event_loop.run_until_complete(f) | 831 sock = self.event_loop.run_until_complete(f)[0] |
806 host, port = sock.getsockname() | 832 host, port = sock.getsockname() |
807 | 833 |
808 client = socket.socket() | 834 client = socket.socket() |
809 client.connect(('127.0.0.1', port)) | 835 client.connect(('127.0.0.1', port)) |
810 client.send(b'xxx') | 836 client.send(b'xxx') |
811 client.close() | 837 client.close() |
812 | 838 |
813 self.event_loop.stop_serving(sock) | 839 self.event_loop.stop_serving(sock) |
814 | 840 |
815 client = socket.socket() | 841 client = socket.socket() |
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1426 old_policy = events.get_event_loop_policy() | 1452 old_policy = events.get_event_loop_policy() |
1427 | 1453 |
1428 policy = events.DefaultEventLoopPolicy() | 1454 policy = events.DefaultEventLoopPolicy() |
1429 events.set_event_loop_policy(policy) | 1455 events.set_event_loop_policy(policy) |
1430 self.assertIs(policy, events.get_event_loop_policy()) | 1456 self.assertIs(policy, events.get_event_loop_policy()) |
1431 self.assertIsNot(policy, old_policy) | 1457 self.assertIsNot(policy, old_policy) |
1432 | 1458 |
1433 | 1459 |
1434 if __name__ == '__main__': | 1460 if __name__ == '__main__': |
1435 unittest.main() | 1461 unittest.main() |
OLD | NEW |