Mercurial > vim
annotate src/testdir/test_channel_lsp.py @ 28361:68c36f734b10 v8.2.4706
patch 8.2.4706: buffer remains active with WinClosed and tabpages
Commit: https://github.com/vim/vim/commit/6a06940f8ae7283999c83ccdf268540220573105
Author: zeertzjq <zeertzjq@outlook.com>
Date: Thu Apr 7 14:08:29 2022 +0100
patch 8.2.4706: buffer remains active with WinClosed and tabpages
Problem: Buffer remains active if a WinClosed event throws an exception
when there are multiple tabpages.
Solution: Ignore aborting() when closing the buffer. (closes #10101)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 07 Apr 2022 15:15:02 +0200 |
parents | 85b07a942518 |
children | d7ca583e5772 |
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 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
27 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
|
28 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
|
29 if msgid != -1: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
30 v['id'] = msgid |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
31 s = json.dumps(v) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
32 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
|
33 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
|
34 resp += "\r\n" |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
35 resp += s |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
36 if self.debug: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
37 with open("Xlspdebug.log", "a") as myfile: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
38 myfile.write("\n=> send\n" + resp) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
39 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
|
40 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
41 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
|
42 v = 'wrong-payload' |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
43 s = json.dumps(v) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
44 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
|
45 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
|
46 resp += "\r\n" |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
47 resp += s |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
48 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
|
49 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
50 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
|
51 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
|
52 s = json.dumps(v) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
53 resp = "\r\n" |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
54 resp += s |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
55 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
|
56 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 resp += "\r\n" |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
61 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
|
62 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
63 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
|
64 # 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
|
65 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
|
66 s = json.dumps(v) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 resp += "\r\n" |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
73 resp += s |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
74 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
|
75 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
76 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
|
77 # 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
|
78 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
|
79 s = json.dumps(v) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
80 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
|
81 resp += "\r\n" |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
82 resp += s |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
83 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
|
84 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
85 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
|
86 # 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
|
87 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
|
88 s = json.dumps(v) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
89 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
|
90 resp += "\r\n" |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
91 resp += s |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
92 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
|
93 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
94 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
|
95 # 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
|
96 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
|
97 s = json.dumps(v) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
98 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
|
99 resp += "\r\n" |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
100 resp += s |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
101 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
|
102 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
103 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
|
104 time.sleep(0.2) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
105 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
|
106 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
107 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
|
108 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
|
109 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
110 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
|
111 # 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
|
112 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
|
113 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
114 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
|
115 # 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
|
116 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
|
117 # 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
|
118 time.sleep(0.2) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
119 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
|
120 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
121 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
|
122 # 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
|
123 self.send_wrong_payload() |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
124 time.sleep(0.2) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
125 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
|
126 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 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
|
131 time.sleep(0.2) |
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(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
|
133 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
134 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
|
135 # notification message test |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
136 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
|
137 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
138 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
|
139 # send multiple notifications |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
140 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
|
141 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
|
142 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
143 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
|
144 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
|
145 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
146 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
|
147 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
|
148 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
149 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
|
150 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
|
151 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
152 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
|
153 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
|
154 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
155 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
|
156 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
|
157 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
158 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
|
159 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
|
160 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
161 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
|
162 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
|
163 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
164 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
|
165 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
|
166 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
167 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
|
168 self.send_empty_payload() |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
169 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
170 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
|
171 try: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
172 decoded = json.loads(msg) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
173 print("Decoded:") |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
174 print(str(decoded)) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
175 if 'method' in decoded: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
176 test_map = { |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
177 'ping': self.do_ping, |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
178 'echo': self.do_echo, |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
179 '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
|
180 '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
|
181 'wrong-payload': self.do_wrong_payload, |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
182 '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
|
183 '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
|
184 '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
|
185 '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
|
186 '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
|
187 '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
|
188 'extra-hdr-fields': self.do_extra_hdr_fields, |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
189 '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
|
190 '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
|
191 '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
|
192 '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
|
193 '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
|
194 } |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
195 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
|
196 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
|
197 else: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
198 print("Error: Unsupported method: " + decoded['method']) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
199 else: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
200 print("Error: 'method' field is not found") |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
201 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
202 except ValueError: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
203 print("json decoding failed") |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
204 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
205 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
|
206 while True: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
207 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
|
208 if sidx == -1: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
209 return msgbuf |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
210 sidx += 16 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
211 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
|
212 if eidx == -1: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
213 return msgbuf |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
214 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
|
215 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
216 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
|
217 if hdrend == -1: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
218 return msgbuf |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
219 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
220 # Remove the header |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
221 msgbuf = msgbuf[hdrend + 4:] |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
222 payload = msgbuf[:msglen] |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
223 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
224 self.process_msg(payload) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
225 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
226 # Remove the processed message |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
227 msgbuf = msgbuf[msglen:] |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
228 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
229 def handle(self): |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
230 print("=== socket opened ===") |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
231 self.debug = False |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
232 msgbuf = '' |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
233 while True: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
234 try: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
235 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
|
236 except socket.error: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
237 print("=== socket error ===") |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
238 break |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
239 except IOError: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
240 print("=== socket closed ===") |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
241 break |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
242 if received == '': |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
243 print("=== socket closed ===") |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
244 break |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
245 print("\nReceived:\n{0}".format(received)) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
246 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
247 # 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
|
248 if self.debug: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
249 with open("Xlspdebug.log", "a") as myfile: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
250 myfile.write("\n<= recv\n" + received) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
251 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
252 # 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
|
253 # 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
|
254 # a time. |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
255 msgbuf += received |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
256 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
|
257 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
258 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
|
259 pass |
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 def writePortInFile(port): |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
262 # 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
|
263 f = open("Xportnr", "w") |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
264 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
|
265 f.close() |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
266 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
267 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
|
268 # 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
|
269 # 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
|
270 # 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
|
271 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
|
272 port = 13684 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
273 writePortInFile(port) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
274 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
275 print("Wait for it...") |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
276 time.sleep(0.5) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
277 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
278 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
|
279 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
|
280 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
281 # 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
|
282 # for each connection. |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
283 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
|
284 server_thread.start() |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
285 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
286 writePortInFile(port) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
287 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
288 print("Listening on port {0}".format(port)) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
289 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
290 # 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
|
291 # 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
|
292 try: |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
293 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
|
294 server_thread.join(1) |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
295 except (KeyboardInterrupt, SystemExit): |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
296 server.shutdown() |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
297 |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
298 if __name__ == "__main__": |
85b07a942518
patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
299 main("localhost", 0) |