-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_event_loop.py
123 lines (90 loc) · 3.35 KB
/
test_event_loop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import unittest
import socket
from event_loop import *
class TestEventLoop(unittest.TestCase):
def setUp(self):
self.loop = EventLoop()
def test_read_event(self):
parent_sock, child_sock = socket.socketpair()
def on_readable():
data = parent_sock.recv(1024)
self.assertEqual(data, b"Hello")
parent_sock.close()
child_sock.close()
self.loop.add_rule(parent_sock, READ_EVENT, on_readable)
child_sock.sendall(b"Hello")
self.loop.wait_next_event(1000)
def test_write_event(self):
parent_sock, child_sock = socket.socketpair()
def on_writable():
parent_sock.sendall(b"Hello")
data = child_sock.recv(1024)
self.assertEqual(data, b"Hello")
parent_sock.close()
child_sock.close()
self.loop.add_rule(parent_sock, WRITE_EVENT, on_writable)
self.loop.wait_next_event(1000)
def test_close_file_descriptor(self):
parent_sock, child_sock = socket.socketpair()
def on_readable():
data = parent_sock.recv(1024)
self.assertEqual(data, b"Hello")
parent_sock.close()
self.loop.add_rule(parent_sock, READ_EVENT, on_readable)
child_sock.sendall(b"Hello")
self.loop.wait_next_event(1000)
# Try to read again after closing
closed = False
try:
parent_sock.recv(1024)
except OSError:
closed = True
self.assertTrue(closed)
def test_timeout(self):
parent_sock, child_sock = socket.socketpair()
def on_readable():
pass
self.loop.add_rule(parent_sock, READ_EVENT, on_readable)
result = self.loop.wait_next_event(1000) # 1 second timeout
self.assertFalse(result)
parent_sock.close()
child_sock.close()
class TestSocketPair(unittest.TestCase):
def test_socket_pair(self):
socket_pair = SocketPair()
# 初始状态应为未关闭
self.assertFalse(socket_pair.closed)
# 关闭套接字
socket_pair.close()
# 状态应为已关闭
self.assertTrue(socket_pair.closed)
# 尝试使用已关闭的套接字应引发异常
with self.assertRaises(ValueError):
socket_pair.send(b"Hello")
with self.assertRaises(ValueError):
socket_pair.recv(1024)
with self.assertRaises(ValueError):
socket_pair.fileno()
class TestEventLoopWithSocketPair(unittest.TestCase):
def setUp(self):
self.loop = EventLoop()
def test_read_event(self):
socket_pair = SocketPair()
def on_readable():
data = socket_pair.recv(1024)
self.assertEqual(data, b"Hello")
socket_pair.close()
self.loop.add_rule(socket_pair, READ_EVENT, on_readable)
socket_pair.child_sock.sendall(b"Hello")
self.loop.wait_next_event(1000)
def test_write_event(self):
socket_pair = SocketPair()
def on_writable():
socket_pair.send(b"Hello")
data = socket_pair.child_sock.recv(1024)
self.assertEqual(data, b"Hello")
socket_pair.close()
self.loop.add_rule(socket_pair, WRITE_EVENT, on_writable)
self.loop.wait_next_event(1000)
if __name__ == "__main__":
unittest.main()