annotate src/testdir/test_channel_lsp.py @ 29206:a2c89e5446b7 v8.2.5122

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