annotate src/testdir/test_channel_lsp.py @ 35172:c98f002b1fe4 default tip

runtime(doc): fix typo in usr_52.txt Commit: https://github.com/vim/vim/commit/b7258738f80f26be302a84a99f968b3bdc2f29bb Author: Christian Brabandt <cb@256bit.org> Date: Sun May 12 19:04:47 2024 +0200 runtime(doc): fix typo in usr_52.txt fixes: https://github.com/vim/vim/issues/14758 Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Sun, 12 May 2024 19:15:08 +0200
parents 41e2414d2886
children
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
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
32 def send_lsp_req(self, msgid, method, params):
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
33 v = {'jsonrpc': '2.0', 'id': msgid, 'method': method}
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
34 if len(params) != 0:
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
35 v['params'] = params
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
36 s = json.dumps(v)
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
37 req = "Content-Length: " + str(len(s)) + "\r\n"
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
38 req += "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n"
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
39 req += "\r\n"
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
40 req += s
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
41 if self.debug:
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
42 self.debuglog("SEND: ({0} bytes) '{1}'\n".format(len(req), req))
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
43 self.request.sendall(req.encode('utf-8'))
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
44
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
45 def send_lsp_resp(self, msgid, resp_dict):
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 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
47 if msgid != -1:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 v['id'] = msgid
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 resp = "Content-Length: " + str(len(s)) + "\r\n"
32311
10a03ae8ba60 patch 9.0.1487: Content-type header for LSP channel not according to spec
Bram Moolenaar <Bram@vim.org>
parents: 31665
diff changeset
51 resp += "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n"
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 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
55 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
56 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
57
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 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
59 v = 'wrong-payload'
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 resp = "Content-Length: " + str(len(s)) + "\r\n"
32311
10a03ae8ba60 patch 9.0.1487: Content-type header for LSP channel not according to spec
Bram Moolenaar <Bram@vim.org>
parents: 31665
diff changeset
62 resp += "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n"
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 resp += s
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_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
68 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
69 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70 resp = "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 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
73
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 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
75 resp = "Content-Length: 0\r\n"
32311
10a03ae8ba60 patch 9.0.1487: Content-type header for LSP channel not according to spec
Bram Moolenaar <Bram@vim.org>
parents: 31665
diff changeset
76 resp += "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n"
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77 resp += "\r\n"
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
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 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
81 # 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
82 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
83 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
84 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
85 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
86 resp += "Accept-Language: en-US,en\r\n"
32311
10a03ae8ba60 patch 9.0.1487: Content-type header for LSP channel not according to spec
Bram Moolenaar <Bram@vim.org>
parents: 31665
diff changeset
87 resp += "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n"
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 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
89 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91 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
92
28511
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
93 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
94 # 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
95 # payload
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105 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
106 # 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
107 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
108 s = json.dumps(v)
32311
10a03ae8ba60 patch 9.0.1487: Content-type header for LSP channel not according to spec
Bram Moolenaar <Bram@vim.org>
parents: 31665
diff changeset
109 resp = "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n"
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 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
113
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 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
115 # 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
116 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
117 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 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
119 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 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
122
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 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
124 # 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
125 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
126 s = json.dumps(v)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127 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
128 resp += "\r\n"
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 resp += s
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 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
131
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132 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
133 time.sleep(0.2)
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
134 self.send_lsp_resp(payload['id'], 'alive')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136 def do_echo(self, payload):
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
137 self.send_lsp_resp(-1, payload)
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139 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
140 # test for a simple RPC request
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
141 self.send_lsp_resp(payload['id'], 'simple-rpc')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
143 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
144 # test for sending a notification before replying to a request message
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
145 self.send_lsp_resp(-1, 'rpc-with-notif-notif')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146 # 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
147 time.sleep(0.2)
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
148 self.send_lsp_resp(payload['id'], 'rpc-with-notif-resp')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 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
151 # 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
152 self.send_wrong_payload()
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153 time.sleep(0.2)
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
154 self.send_lsp_resp(-1, 'wrong-payload')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155
28528
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
156 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
157 # test for sending a large (> 64K) payload
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
158 self.send_lsp_resp(payload['id'], payload)
28528
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
159
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 def do_rpc_resp_incorrect_id(self, payload):
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
161 self.send_lsp_resp(-1, 'rpc-resp-incorrect-id-1')
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
162 self.send_lsp_resp(-1, 'rpc-resp-incorrect-id-2')
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
163 self.send_lsp_resp(1, 'rpc-resp-incorrect-id-3')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 time.sleep(0.2)
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
165 self.send_lsp_resp(payload['id'], 'rpc-resp-incorrect-id-4')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 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
168 # notification message test
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
169 self.send_lsp_resp(-1, 'simple-notif')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 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
172 # send multiple notifications
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
173 self.send_lsp_resp(-1, 'multi-notif1')
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
174 self.send_lsp_resp(-1, 'multi-notif2')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 def do_msg_with_id(self, payload):
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
177 self.send_lsp_resp(payload['id'], 'msg-with-id')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 def do_msg_specific_cb(self, payload):
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
180 self.send_lsp_resp(payload['id'], 'msg-specific-cb')
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182 def do_server_req(self, payload):
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
183 self.send_lsp_resp(201, {'method': 'checkhealth', 'params': {'a': 20}})
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 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
186 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
187
32996
dd8da8f1c2bc patch 9.0.1790: Redundant LSP Content-Type header
Christian Brabandt <cb@256bit.org>
parents: 32311
diff changeset
188 def do_delayed_payload(self, payload):
28511
d7ca583e5772 patch 8.2.4780: parsing an LSP message fails when it is split
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
189 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
190
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 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
192 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
193
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 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
195 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
196
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 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
198 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
199
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200 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
201 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
202
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
203 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
204 self.send_empty_payload()
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
206 def do_server_req_in_middle(self, payload):
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
207 # Send a notification message to the client in the middle of processing
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
208 # a request message from the client
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
209 self.send_lsp_req(-1, 'server-req-in-middle', {'text': 'server-notif'})
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
210 # Send a request message to the client in the middle of processing a
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
211 # request message from the client.
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
212 self.send_lsp_req(payload['id'], 'server-req-in-middle', {'text': 'server-req'})
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
213
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
214 def do_server_req_in_middle_resp(self, payload):
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
215 # After receiving a response from the client send the response to the
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
216 # client request.
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
217 self.send_lsp_resp(payload['id'], {'text': 'server-resp'})
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
218
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
219 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
220 try:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221 decoded = json.loads(msg)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 if 'method' in decoded:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223 test_map = {
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
224 'ping': self.do_ping,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 'echo': self.do_echo,
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 '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
227 '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
228 '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
229 '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
230 '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
231 '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
232 '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
233 'msg-with-id': self.do_msg_with_id,
30986
360f286b5869 patch 9.0.0828: various typos
Bram Moolenaar <Bram@vim.org>
parents: 28528
diff changeset
234 'msg-specific-cb': self.do_msg_specific_cb,
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 '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
236 'extra-hdr-fields': self.do_extra_hdr_fields,
32996
dd8da8f1c2bc patch 9.0.1790: Redundant LSP Content-Type header
Christian Brabandt <cb@256bit.org>
parents: 32311
diff changeset
237 'delayed-payload': self.do_delayed_payload,
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 '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
239 '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
240 '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
241 'empty-header': self.do_empty_header,
33318
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
242 'empty-payload': self.do_empty_payload,
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
243 'server-req-in-middle': self.do_server_req_in_middle,
41e2414d2886 patch 9.0.1924: LSP server message still wrongly handled (after 9.0.1922)
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
244 'server-req-in-middle-resp': self.do_server_req_in_middle_resp,
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
245 }
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 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
247 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
248 else:
28528
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
249 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
250 else:
28528
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
251 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
252
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
253 except ValueError:
28528
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
254 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
255
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
256 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
257 while True:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258 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
259 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
260 # partial message received
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
261 return msgbuf
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
262 sidx += 16
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
263 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
264 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
265 # partial message received
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266 return msgbuf
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
267 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
268
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
269 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
270 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
271 # partial message received
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
272 return msgbuf
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
273
28528
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
274 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
275 if self.debug:
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
276 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
277 # partial message received
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
278 return msgbuf
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
279
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
280 if self.debug:
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
281 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
282
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
283 # Remove the header
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
284 msgbuf = msgbuf[hdrend + 4:]
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
285 payload = msgbuf[:msglen]
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
286
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
287 self.process_msg(payload)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
288
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
289 # Remove the processed message
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290 msgbuf = msgbuf[msglen:]
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
291
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
292 def handle(self):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
293 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
294 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
295 msgbuf = ''
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
296 while True:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
297 try:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
298 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
299 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
300 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
301 break
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
302 except IOError:
28528
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
303 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
304 break
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
305 if received == '':
28528
6b1da12297e5 patch 8.2.4788: large payload for LSP message not tested
Bram Moolenaar <Bram@vim.org>
parents: 28511
diff changeset
306 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
307 break
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 # 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
310 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
311 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
312
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
313 # 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
314 # 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
315 # a time.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
316 msgbuf += received
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
317 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
318
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
319 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
320 pass
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
321
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
322 def writePortInFile(port):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
323 # 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
324 f = open("Xportnr", "w")
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
325 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
326 f.close()
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
327
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
328 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
329 # 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
330 # 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
331 # 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
332 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
333 port = 13684
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
334 writePortInFile(port)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
335 time.sleep(0.5)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
336
31665
8492bbc9f533 patch 9.0.1165: tests using IPv6 sometimes fail
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
337 addrs = socket.getaddrinfo(host, port, 0, 0, socket.IPPROTO_TCP)
8492bbc9f533 patch 9.0.1165: tests using IPv6 sometimes fail
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
338 # Each addr is a (family, type, proto, canonname, sockaddr) tuple
8492bbc9f533 patch 9.0.1165: tests using IPv6 sometimes fail
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
339 sockaddr = addrs[0][4]
8492bbc9f533 patch 9.0.1165: tests using IPv6 sometimes fail
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
340 server_class.address_family = addrs[0][0]
8492bbc9f533 patch 9.0.1165: tests using IPv6 sometimes fail
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
341
8492bbc9f533 patch 9.0.1165: tests using IPv6 sometimes fail
Bram Moolenaar <Bram@vim.org>
parents: 30986
diff changeset
342 server = server_class(sockaddr[0:2], ThreadedTCPRequestHandler)
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
343 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
344
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
345 # 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
346 # for each connection.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
347 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
348 server_thread.start()
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
349
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
350 writePortInFile(port)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
351
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
352 # 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
353 # 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
354 try:
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
355 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
356 server_thread.join(1)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
357 except (KeyboardInterrupt, SystemExit):
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
358 server.shutdown()
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
359
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
360 if __name__ == "__main__":
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
361 main("localhost", 0)