comparison src/testdir/test_channel_lsp.py @ 28244:85b07a942518 v8.2.4648

patch 8.2.4648: handling LSP messages is a bit slow Commit: https://github.com/vim/vim/commit/9247a221ce7800c0ae1b3487112d314b8ab79f53 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Wed Mar 30 10:16:05 2022 +0100 patch 8.2.4648: handling LSP messages is a bit slow Problem: Handling LSP messages is a bit slow. Solution: Included support for LSP messages. (Yegappan Lakshmanan, closes #10025)
author Bram Moolenaar <Bram@vim.org>
date Wed, 30 Mar 2022 11:30:05 +0200
parents
children d7ca583e5772
comparison
equal deleted inserted replaced
28243:0e3366e35705 28244:85b07a942518
1 #!/usr/bin/env python
2 #
3 # Server that will accept connections from a Vim channel.
4 # Used by test_channel.vim to test LSP functionality.
5 #
6 # This requires Python 2.6 or later.
7
8 from __future__ import print_function
9 import json
10 import socket
11 import sys
12 import time
13 import threading
14
15 try:
16 # Python 3
17 import socketserver
18 except ImportError:
19 # Python 2
20 import SocketServer as socketserver
21
22 class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
23
24 def setup(self):
25 self.request.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
26
27 def send_lsp_msg(self, msgid, resp_dict):
28 v = {'jsonrpc': '2.0', 'result': resp_dict}
29 if msgid != -1:
30 v['id'] = msgid
31 s = json.dumps(v)
32 resp = "Content-Length: " + str(len(s)) + "\r\n"
33 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
34 resp += "\r\n"
35 resp += s
36 if self.debug:
37 with open("Xlspdebug.log", "a") as myfile:
38 myfile.write("\n=> send\n" + resp)
39 self.request.sendall(resp.encode('utf-8'))
40
41 def send_wrong_payload(self):
42 v = 'wrong-payload'
43 s = json.dumps(v)
44 resp = "Content-Length: " + str(len(s)) + "\r\n"
45 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
46 resp += "\r\n"
47 resp += s
48 self.request.sendall(resp.encode('utf-8'))
49
50 def send_empty_header(self, msgid, resp_dict):
51 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
52 s = json.dumps(v)
53 resp = "\r\n"
54 resp += s
55 self.request.sendall(resp.encode('utf-8'))
56
57 def send_empty_payload(self):
58 resp = "Content-Length: 0\r\n"
59 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
60 resp += "\r\n"
61 self.request.sendall(resp.encode('utf-8'))
62
63 def send_extra_hdr_fields(self, msgid, resp_dict):
64 # test for sending extra fields in the http header
65 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
66 s = json.dumps(v)
67 resp = "Host: abc.vim.org\r\n"
68 resp += "User-Agent: Python\r\n"
69 resp += "Accept-Language: en-US,en\r\n"
70 resp += "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
71 resp += "Content-Length: " + str(len(s)) + "\r\n"
72 resp += "\r\n"
73 resp += s
74 self.request.sendall(resp.encode('utf-8'))
75
76 def send_hdr_without_len(self, msgid, resp_dict):
77 # test for sending the http header without length
78 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
79 s = json.dumps(v)
80 resp = "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n"
81 resp += "\r\n"
82 resp += s
83 self.request.sendall(resp.encode('utf-8'))
84
85 def send_hdr_with_wrong_len(self, msgid, resp_dict):
86 # test for sending the http header with wrong length
87 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
88 s = json.dumps(v)
89 resp = "Content-Length: 1000\r\n"
90 resp += "\r\n"
91 resp += s
92 self.request.sendall(resp.encode('utf-8'))
93
94 def send_hdr_with_negative_len(self, msgid, resp_dict):
95 # test for sending the http header with negative length
96 v = {'jsonrpc': '2.0', 'id': msgid, 'result': resp_dict}
97 s = json.dumps(v)
98 resp = "Content-Length: -1\r\n"
99 resp += "\r\n"
100 resp += s
101 self.request.sendall(resp.encode('utf-8'))
102
103 def do_ping(self, payload):
104 time.sleep(0.2)
105 self.send_lsp_msg(payload['id'], 'alive')
106
107 def do_echo(self, payload):
108 self.send_lsp_msg(-1, payload)
109
110 def do_simple_rpc(self, payload):
111 # test for a simple RPC request
112 self.send_lsp_msg(payload['id'], 'simple-rpc')
113
114 def do_rpc_with_notif(self, payload):
115 # test for sending a notification before replying to a request message
116 self.send_lsp_msg(-1, 'rpc-with-notif-notif')
117 # sleep for some time to make sure the notification is delivered
118 time.sleep(0.2)
119 self.send_lsp_msg(payload['id'], 'rpc-with-notif-resp')
120
121 def do_wrong_payload(self, payload):
122 # test for sending a non dict payload
123 self.send_wrong_payload()
124 time.sleep(0.2)
125 self.send_lsp_msg(-1, 'wrong-payload')
126
127 def do_rpc_resp_incorrect_id(self, payload):
128 self.send_lsp_msg(-1, 'rpc-resp-incorrect-id-1')
129 self.send_lsp_msg(-1, 'rpc-resp-incorrect-id-2')
130 self.send_lsp_msg(1, 'rpc-resp-incorrect-id-3')
131 time.sleep(0.2)
132 self.send_lsp_msg(payload['id'], 'rpc-resp-incorrect-id-4')
133
134 def do_simple_notif(self, payload):
135 # notification message test
136 self.send_lsp_msg(-1, 'simple-notif')
137
138 def do_multi_notif(self, payload):
139 # send multiple notifications
140 self.send_lsp_msg(-1, 'multi-notif1')
141 self.send_lsp_msg(-1, 'multi-notif2')
142
143 def do_msg_with_id(self, payload):
144 self.send_lsp_msg(payload['id'], 'msg-with-id')
145
146 def do_msg_specific_cb(self, payload):
147 self.send_lsp_msg(payload['id'], 'msg-specifc-cb')
148
149 def do_server_req(self, payload):
150 self.send_lsp_msg(201, {'method': 'checkhealth', 'params': {'a': 20}})
151
152 def do_extra_hdr_fields(self, payload):
153 self.send_extra_hdr_fields(payload['id'], 'extra-hdr-fields')
154
155 def do_hdr_without_len(self, payload):
156 self.send_hdr_without_len(payload['id'], 'hdr-without-len')
157
158 def do_hdr_with_wrong_len(self, payload):
159 self.send_hdr_with_wrong_len(payload['id'], 'hdr-with-wrong-len')
160
161 def do_hdr_with_negative_len(self, payload):
162 self.send_hdr_with_negative_len(payload['id'], 'hdr-with-negative-len')
163
164 def do_empty_header(self, payload):
165 self.send_empty_header(payload['id'], 'empty-header')
166
167 def do_empty_payload(self, payload):
168 self.send_empty_payload()
169
170 def process_msg(self, msg):
171 try:
172 decoded = json.loads(msg)
173 print("Decoded:")
174 print(str(decoded))
175 if 'method' in decoded:
176 test_map = {
177 'ping': self.do_ping,
178 'echo': self.do_echo,
179 'simple-rpc': self.do_simple_rpc,
180 'rpc-with-notif': self.do_rpc_with_notif,
181 'wrong-payload': self.do_wrong_payload,
182 'rpc-resp-incorrect-id': self.do_rpc_resp_incorrect_id,
183 'simple-notif': self.do_simple_notif,
184 'multi-notif': self.do_multi_notif,
185 'msg-with-id': self.do_msg_with_id,
186 'msg-specifc-cb': self.do_msg_specific_cb,
187 'server-req': self.do_server_req,
188 'extra-hdr-fields': self.do_extra_hdr_fields,
189 'hdr-without-len': self.do_hdr_without_len,
190 'hdr-with-wrong-len': self.do_hdr_with_wrong_len,
191 'hdr-with-negative-len': self.do_hdr_with_negative_len,
192 'empty-header': self.do_empty_header,
193 'empty-payload': self.do_empty_payload
194 }
195 if decoded['method'] in test_map:
196 test_map[decoded['method']](decoded)
197 else:
198 print("Error: Unsupported method: " + decoded['method'])
199 else:
200 print("Error: 'method' field is not found")
201
202 except ValueError:
203 print("json decoding failed")
204
205 def process_msgs(self, msgbuf):
206 while True:
207 sidx = msgbuf.find('Content-Length: ')
208 if sidx == -1:
209 return msgbuf
210 sidx += 16
211 eidx = msgbuf.find('\r\n')
212 if eidx == -1:
213 return msgbuf
214 msglen = int(msgbuf[sidx:eidx])
215
216 hdrend = msgbuf.find('\r\n\r\n')
217 if hdrend == -1:
218 return msgbuf
219
220 # Remove the header
221 msgbuf = msgbuf[hdrend + 4:]
222 payload = msgbuf[:msglen]
223
224 self.process_msg(payload)
225
226 # Remove the processed message
227 msgbuf = msgbuf[msglen:]
228
229 def handle(self):
230 print("=== socket opened ===")
231 self.debug = False
232 msgbuf = ''
233 while True:
234 try:
235 received = self.request.recv(4096).decode('utf-8')
236 except socket.error:
237 print("=== socket error ===")
238 break
239 except IOError:
240 print("=== socket closed ===")
241 break
242 if received == '':
243 print("=== socket closed ===")
244 break
245 print("\nReceived:\n{0}".format(received))
246
247 # Write the received lines into the file for debugging
248 if self.debug:
249 with open("Xlspdebug.log", "a") as myfile:
250 myfile.write("\n<= recv\n" + received)
251
252 # Can receive more than one line in a response or a partial line.
253 # Accumulate all the received characters and process one line at
254 # a time.
255 msgbuf += received
256 msgbuf = self.process_msgs(msgbuf)
257
258 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
259 pass
260
261 def writePortInFile(port):
262 # Write the port number in Xportnr, so that the test knows it.
263 f = open("Xportnr", "w")
264 f.write("{0}".format(port))
265 f.close()
266
267 def main(host, port, server_class=ThreadedTCPServer):
268 # Wait half a second before opening the port to test waittime in ch_open().
269 # We do want to get the port number, get that first. We cannot open the
270 # socket, guess a port is free.
271 if len(sys.argv) >= 2 and sys.argv[1] == 'delay':
272 port = 13684
273 writePortInFile(port)
274
275 print("Wait for it...")
276 time.sleep(0.5)
277
278 server = server_class((host, port), ThreadedTCPRequestHandler)
279 ip, port = server.server_address[0:2]
280
281 # Start a thread with the server. That thread will then start a new thread
282 # for each connection.
283 server_thread = threading.Thread(target=server.serve_forever)
284 server_thread.start()
285
286 writePortInFile(port)
287
288 print("Listening on port {0}".format(port))
289
290 # Main thread terminates, but the server continues running
291 # until server.shutdown() is called.
292 try:
293 while server_thread.is_alive():
294 server_thread.join(1)
295 except (KeyboardInterrupt, SystemExit):
296 server.shutdown()
297
298 if __name__ == "__main__":
299 main("localhost", 0)