-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_rpdb.py
265 lines (217 loc) · 5.85 KB
/
test_rpdb.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import unittest
import os
import subprocess
import factor
import rpdb
import time
import signal
import socket
import json
# Do not move
def start_prog():
pid = os.fork()
if pid == 0:
rpdb.set_trace()
factor.factor_fibonacci(num=10)
return pid
# Do not move
class FakeSocket(object):
def __init__(self, recv_data=None):
self.recv_data = recv_data or []
self.sent = []
def recv(self, _):
if not self.recv_data:
return ''
return self.recv_data.pop(0)
def sendall(self, data):
self.sent.append(data)
class TestJsonSocket(unittest.TestCase):
def test_simple(self):
expected = {
'a': 1,
'hello': 2,
'foo': 'bar'
}
exp_s = json.dumps(expected)
sock = FakeSocket([str(len(exp_s)) + '\r\n' + exp_s])
jsock = rpdb.JsonSocket(sock)
self.assertEquals(expected, jsock.recv_msg())
def test_split_size(self):
expected = {
'a': 1,
'hello': 2,
'foo': 'bar'
}
exp_s = json.dumps(expected)
msg_sz = str(len(exp_s))
sock = FakeSocket([msg_sz[0], msg_sz[1:] + '\r\n' + exp_s])
jsock = rpdb.JsonSocket(sock)
self.assertEquals(expected, jsock.recv_msg())
def test_split_msg(self):
expected = {
'a': 1,
'hello': 2,
'foo': 'bar'
}
exp_s = json.dumps(expected)
msg_sz = str(len(exp_s))
sock = FakeSocket([msg_sz + '\r\n' + exp_s[:10], exp_s[10:]])
jsock = rpdb.JsonSocket(sock)
self.assertEquals(expected, jsock.recv_msg())
def test_second_msg(self):
expected1 = {
'a': 1,
'hello': 2,
'foo': 'bar'
}
exp_s1 = json.dumps(expected1)
msg_sz1 = str(len(exp_s1))
expected2 = {
'kajhsd': 87,
'khjahs': 'kasd'
}
exp_s2 = json.dumps(expected2)
msg_sz2 = str(len(exp_s2))
sock = FakeSocket([
msg_sz1 + '\r\n' + exp_s1[:10],
exp_s1[10:] + msg_sz2 + '\r\n' + exp_s2[:10],
exp_s2[10:]
])
jsock = rpdb.JsonSocket(sock)
self.assertEquals(expected1, jsock.recv_msg())
self.assertEquals(expected2, jsock.recv_msg())
def test_send_msg(self):
obj = {
'a': 1,
'hello': 2,
'foo': 'bar'
}
exp_s = json.dumps(obj)
msg_sz = str(len(exp_s))
expected = msg_sz + '\r\n' + exp_s
sock = FakeSocket()
jsock = rpdb.JsonSocket(sock)
jsock.send_msg(obj)
self.assertEquals(expected, sock.sent[0])
class TestRpdb(unittest.TestCase):
def connect_socket(self, tries_left=10):
try:
self.conn = socket.socket(rpdb.ADDR_FAMILY, socket.SOCK_STREAM)
self.conn.connect(rpdb.SOCKET_ADDR)
except Exception:
time.sleep(1)
if tries_left == 0:
raise
self.connect_socket(tries_left-1)
def setUp(self):
self.child = subprocess.Popen(['./factor.py', 'listening'])
tries_left = 10
self.connect_socket()
self.jsock = rpdb.JsonSocket(self.conn)
def tearDown(self):
self.child.kill()
self.conn.close()
def test_first_frame_sent(self):
msg = self.jsock.recv_msg()
self.assertEquals('current_frame', msg['type'])
self.assertEquals(41, msg['line_no'])
self.assertIn('factor.py', msg['file'])
def test_step(self):
msg = self.jsock.recv_msg()
self.jsock.send_msg({'command': 'step'})
msg = self.jsock.recv_msg()
self.assertEquals('current_frame', msg['type'])
self.assertEquals(9, msg['line_no'])
self.assertIn('factor.py', msg['file'])
def test_break(self):
msg = self.jsock.recv_msg()
self.jsock.send_msg({
'command': 'set_breaks',
'args': {
'breaks': [
{
'file': 'primes.py',
'line_no': 12,
},
],
}
})
self.jsock.send_msg({'command': 'continue'})
msg = self.jsock.recv_msg()
self.assertEquals('current_frame', msg['type'])
self.assertEquals(12, msg['line_no'])
self.assertIn('primes.py', msg['file'])
def test_clean_breaks(self):
msg = self.jsock.recv_msg()
self.jsock.send_msg({
'command': 'set_breaks',
'args': {
'breaks': [
{
'file': 'primes.py',
'line_no': 8,
},
{
'file': 'fibonacci.py',
'line_no': 12,
},
]
}
})
self.jsock.send_msg({
'command': 'clear_breaks',
'args': {
'breaks': [
{
'file': 'fibonacci.py',
'line_no': 12,
}
]
}
})
self.jsock.send_msg({'command': 'continue'})
msg = self.jsock.recv_msg()
self.assertEquals('current_frame', msg['type'])
self.assertEquals(8, msg['line_no'])
self.assertIn('primes.py', msg['file'])
def test_next(self):
msg = self.jsock.recv_msg()
self.jsock.send_msg({
'command': 'set_breaks',
'args': {
'breaks': [
{
'file': 'primes.py',
'line_no': 8,
},
]
}
})
self.jsock.send_msg({
'command': 'continue'
})
msg = self.jsock.recv_msg()
self.jsock.send_msg({
'command': 'next'
})
msg = self.jsock.recv_msg()
self.assertEquals('current_frame', msg['type'])
self.assertEquals(9, msg['line_no'])
self.assertIn('primes.py', msg['file'])
class TestRpdbListening(TestRpdb):
@classmethod
def setUpClass(cls):
cls.listening_socket = socket.socket(rpdb.ADDR_FAMILY, socket.SOCK_STREAM)
cls.listening_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
cls.listening_socket.bind(('', 57000))
cls.listening_socket.listen(1)
@classmethod
def tearDownClass(cls):
cls.listening_socket.close()
def setUp(self):
self.child = subprocess.Popen(['./factor.py', 'not_listening'])
self.conn, addr = self.listening_socket.accept()
self.jsock = rpdb.JsonSocket(self.conn)
def tearDown(self):
self.child.kill()
self.conn.close()