forked from fabioz/PyDev.Debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_check_pydevconsole.py
121 lines (94 loc) · 4.54 KB
/
test_check_pydevconsole.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
import threading
import unittest
import os
import pytest
import pydevconsole
from _pydev_bundle.pydev_imports import xmlrpclib, SimpleXMLRPCServer
from _pydev_bundle.pydev_localhost import get_localhost
try:
raw_input
raw_input_name = "raw_input"
except NameError:
raw_input_name = "input"
try:
from IPython import core # @UnusedImport
has_ipython = True
except:
has_ipython = False
# =======================================================================================================================
# Test
# =======================================================================================================================
@pytest.mark.skipif(os.environ.get("TRAVIS") == "true" or not has_ipython, reason="Too flaky on Travis (and requires IPython).")
class Test(unittest.TestCase):
def start_client_thread(self, client_port):
class ClientThread(threading.Thread):
def __init__(self, client_port):
threading.Thread.__init__(self)
self.client_port = client_port
def run(self):
class HandleRequestInput:
def RequestInput(self):
client_thread.requested_input = True
return "RequestInput: OK"
def NotifyFinished(self, *args, **kwargs):
client_thread.notified_finished += 1
return 1
handle_request_input = HandleRequestInput()
from _pydev_bundle import pydev_localhost
self.client_server = client_server = SimpleXMLRPCServer(
(pydev_localhost.get_localhost(), self.client_port), logRequests=False
)
client_server.register_function(handle_request_input.RequestInput)
client_server.register_function(handle_request_input.NotifyFinished)
client_server.serve_forever()
def shutdown(self):
return
self.client_server.shutdown()
client_thread = ClientThread(client_port)
client_thread.requested_input = False
client_thread.notified_finished = 0
client_thread.daemon = True
client_thread.start()
return client_thread
def get_free_addresses(self):
from _pydev_bundle.pydev_localhost import get_socket_names
socket_names = get_socket_names(2, close=True)
return [socket_name[1] for socket_name in socket_names]
def test_server(self):
# Just making sure that the singleton is created in this thread.
from _pydev_bundle.pydev_ipython_console_011 import get_pydev_frontend
get_pydev_frontend(get_localhost(), 0)
client_port, server_port = self.get_free_addresses()
class ServerThread(threading.Thread):
def __init__(self, client_port, server_port):
threading.Thread.__init__(self)
self.client_port = client_port
self.server_port = server_port
def run(self):
from _pydev_bundle import pydev_localhost
print("Starting server with:", pydev_localhost.get_localhost(), self.server_port, self.client_port)
pydevconsole.start_server(pydev_localhost.get_localhost(), self.server_port, self.client_port)
server_thread = ServerThread(client_port, server_port)
server_thread.daemon = True
server_thread.start()
client_thread = self.start_client_thread(client_port) # @UnusedVariable
try:
import time
time.sleep(0.3) # let's give it some time to start the threads
from _pydev_bundle import pydev_localhost
server = xmlrpclib.Server("http://%s:%s" % (pydev_localhost.get_localhost(), server_port))
server.execLine("import sys; print('Running with: %s %s' % (sys.executable or sys.platform, sys.version))")
server.execLine("class Foo:")
server.execLine(" pass")
server.execLine("")
server.execLine("foo = Foo()")
server.execLine("a = %s()" % raw_input_name)
initial = time.time()
while not client_thread.requested_input:
if time.time() - initial > 2:
raise AssertionError("Did not get the return asked before the timeout.")
time.sleep(0.1)
frame_xml = server.getFrame()
self.assertTrue("RequestInput" in frame_xml, "Did not fid RequestInput in:\n%s" % (frame_xml,))
finally:
client_thread.shutdown()