annotate src/testdir/test_channel.py @ 8153:240deebfadde v7.4.1370

commit https://github.com/vim/vim/commit/ddbe7d26b10c4374f406b807ae161826cf2096e1 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Feb 20 18:26:48 2016 +0100 patch 7.4.1370 Problem: The Python test script may keep on running. Solution: Join the threads. (Yasuhiro Matsumoto)
author Christian Brabandt <cb@256bit.org>
date Sat, 20 Feb 2016 18:30:05 +0100
parents 4aea0b0aa714
children d0958e22d9ff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1 #!/usr/bin/python
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2 #
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3 # Server that will accept connections from a Vim channel.
8041
c6443e78cf2d commit https://github.com/vim/vim/commit/7707344ddec9069b495b2a5ed41f2104466fc88b
Christian Brabandt <cb@256bit.org>
parents: 8009
diff changeset
4 # Used by test_channel.vim.
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 #
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 # This requires Python 2.6 or later.
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 from __future__ import print_function
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9 import json
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 import socket
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 import sys
8114
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
12 import time
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13 import threading
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15 try:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16 # Python 3
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17 import socketserver
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18 except ImportError:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
19 # Python 2
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
20 import SocketServer as socketserver
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
21
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
22 class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
23
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
24 def handle(self):
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
25 print("=== socket opened ===")
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
26 while True:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
27 try:
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
28 received = self.request.recv(4096).decode('utf-8')
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
29 except socket.error:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
30 print("=== socket error ===")
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
31 break
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
32 except IOError:
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 print("=== socket closed ===")
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34 break
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
35 if received == '':
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 print("=== socket closed ===")
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37 break
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
38 print("received: {}".format(received))
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
39
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
40 # We may receive two messages at once. Take the part up to the
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
41 # matching "]" (recognized by finding "][").
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
42 todo = received
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
43 while todo != '':
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
44 splitidx = todo.find('][')
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
45 if splitidx < 0:
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
46 used = todo
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
47 todo = ''
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
48 else:
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
49 used = todo[:splitidx + 1]
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
50 todo = todo[splitidx + 1:]
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
51 if used != received:
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
52 print("using: {}".format(used))
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
53
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
54 try:
7914
35973ce58c84 commit https://github.com/vim/vim/commit/608a8919cae982cb38e38725a843df47b234dae6
Christian Brabandt <cb@256bit.org>
parents: 7912
diff changeset
55 decoded = json.loads(used)
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
56 except ValueError:
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
57 print("json decoding failed")
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
58 decoded = [-1, '']
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
59
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
60 # Send a response if the sequence number is positive.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
61 if decoded[0] >= 0:
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
62 if decoded[1] == 'hello!':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
63 # simply send back a string
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
64 response = "got it"
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
65 elif decoded[1] == 'make change':
7918
ce5a7a613867 commit https://github.com/vim/vim/commit/66624ff0d9e1de2fc5eb4f95f3a3a2ed70b10138
Christian Brabandt <cb@256bit.org>
parents: 7916
diff changeset
66 # Send two ex commands at the same time, before
ce5a7a613867 commit https://github.com/vim/vim/commit/66624ff0d9e1de2fc5eb4f95f3a3a2ed70b10138
Christian Brabandt <cb@256bit.org>
parents: 7916
diff changeset
67 # replying to the request.
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
68 cmd = '["ex","call append(\\"$\\",\\"added1\\")"]'
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
69 cmd += '["ex","call append(\\"$\\",\\"added2\\")"]'
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
70 print("sending: {}".format(cmd))
7916
54602dcac207 commit https://github.com/vim/vim/commit/3b05b135e3ee4cfd59983fd63461e8f7642c1713
Christian Brabandt <cb@256bit.org>
parents: 7914
diff changeset
71 self.request.sendall(cmd.encode('utf-8'))
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
72 response = "ok"
7939
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
73 elif decoded[1] == 'do normal':
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
74 # Send a normal command.
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
75 cmd = '["normal","G$s more\u001b"]'
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
76 print("sending: {}".format(cmd))
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
77 self.request.sendall(cmd.encode('utf-8'))
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
78 response = "ok"
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
79 elif decoded[1] == 'eval-works':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
80 # Send an eval request. We ignore the response.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
81 cmd = '["eval","\\"foo\\" . 123", -1]'
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
82 print("sending: {}".format(cmd))
7916
54602dcac207 commit https://github.com/vim/vim/commit/3b05b135e3ee4cfd59983fd63461e8f7642c1713
Christian Brabandt <cb@256bit.org>
parents: 7914
diff changeset
83 self.request.sendall(cmd.encode('utf-8'))
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
84 response = "ok"
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
85 elif decoded[1] == 'eval-fails':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
86 # Send an eval request that will fail.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
87 cmd = '["eval","xxx", -2]'
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
88 print("sending: {}".format(cmd))
7916
54602dcac207 commit https://github.com/vim/vim/commit/3b05b135e3ee4cfd59983fd63461e8f7642c1713
Christian Brabandt <cb@256bit.org>
parents: 7914
diff changeset
89 self.request.sendall(cmd.encode('utf-8'))
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
90 response = "ok"
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7939
diff changeset
91 elif decoded[1] == 'eval-error':
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7939
diff changeset
92 # Send an eval request that works but the result can't
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7939
diff changeset
93 # be encoded.
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7939
diff changeset
94 cmd = '["eval","function(\\"tr\\")", -3]'
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7939
diff changeset
95 print("sending: {}".format(cmd))
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7939
diff changeset
96 self.request.sendall(cmd.encode('utf-8'))
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7939
diff changeset
97 response = "ok"
7918
ce5a7a613867 commit https://github.com/vim/vim/commit/66624ff0d9e1de2fc5eb4f95f3a3a2ed70b10138
Christian Brabandt <cb@256bit.org>
parents: 7916
diff changeset
98 elif decoded[1] == 'eval-bad':
ce5a7a613867 commit https://github.com/vim/vim/commit/66624ff0d9e1de2fc5eb4f95f3a3a2ed70b10138
Christian Brabandt <cb@256bit.org>
parents: 7916
diff changeset
99 # Send an eval request missing the third argument.
ce5a7a613867 commit https://github.com/vim/vim/commit/66624ff0d9e1de2fc5eb4f95f3a3a2ed70b10138
Christian Brabandt <cb@256bit.org>
parents: 7916
diff changeset
100 cmd = '["eval","xxx"]'
ce5a7a613867 commit https://github.com/vim/vim/commit/66624ff0d9e1de2fc5eb4f95f3a3a2ed70b10138
Christian Brabandt <cb@256bit.org>
parents: 7916
diff changeset
101 print("sending: {}".format(cmd))
ce5a7a613867 commit https://github.com/vim/vim/commit/66624ff0d9e1de2fc5eb4f95f3a3a2ed70b10138
Christian Brabandt <cb@256bit.org>
parents: 7916
diff changeset
102 self.request.sendall(cmd.encode('utf-8'))
ce5a7a613867 commit https://github.com/vim/vim/commit/66624ff0d9e1de2fc5eb4f95f3a3a2ed70b10138
Christian Brabandt <cb@256bit.org>
parents: 7916
diff changeset
103 response = "ok"
7939
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
104 elif decoded[1] == 'an expr':
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
105 # Send an expr request.
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
106 cmd = '["expr","setline(\\"$\\", [\\"one\\",\\"two\\",\\"three\\"])"]'
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
107 print("sending: {}".format(cmd))
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
108 self.request.sendall(cmd.encode('utf-8'))
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
109 response = "ok"
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
110 elif decoded[1] == 'redraw':
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
111 cmd = '["redraw",""]'
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
112 print("sending: {}".format(cmd))
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
113 self.request.sendall(cmd.encode('utf-8'))
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
114 response = "ok"
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
115 elif decoded[1] == 'redraw!':
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
116 cmd = '["redraw","force"]'
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
117 print("sending: {}".format(cmd))
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
118 self.request.sendall(cmd.encode('utf-8'))
dcc0bd6b1574 commit https://github.com/vim/vim/commit/f416086f264c1d998863b2e600f4c14f799d0d99
Christian Brabandt <cb@256bit.org>
parents: 7937
diff changeset
119 response = "ok"
7937
2e905dfc6999 commit https://github.com/vim/vim/commit/6076fe1986255d32b7a078a28bf9e7bea19d6f30
Christian Brabandt <cb@256bit.org>
parents: 7920
diff changeset
120 elif decoded[1] == 'empty-request':
2e905dfc6999 commit https://github.com/vim/vim/commit/6076fe1986255d32b7a078a28bf9e7bea19d6f30
Christian Brabandt <cb@256bit.org>
parents: 7920
diff changeset
121 cmd = '[]'
2e905dfc6999 commit https://github.com/vim/vim/commit/6076fe1986255d32b7a078a28bf9e7bea19d6f30
Christian Brabandt <cb@256bit.org>
parents: 7920
diff changeset
122 print("sending: {}".format(cmd))
2e905dfc6999 commit https://github.com/vim/vim/commit/6076fe1986255d32b7a078a28bf9e7bea19d6f30
Christian Brabandt <cb@256bit.org>
parents: 7920
diff changeset
123 self.request.sendall(cmd.encode('utf-8'))
2e905dfc6999 commit https://github.com/vim/vim/commit/6076fe1986255d32b7a078a28bf9e7bea19d6f30
Christian Brabandt <cb@256bit.org>
parents: 7920
diff changeset
124 response = "ok"
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
125 elif decoded[1] == 'eval-result':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
126 # Send back the last received eval result.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
127 response = last_eval
8009
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
128 elif decoded[1] == 'call me':
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
129 cmd = '[0,"we called you"]'
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
130 print("sending: {}".format(cmd))
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
131 self.request.sendall(cmd.encode('utf-8'))
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
132 response = "ok"
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
133 elif decoded[1] == 'call me again':
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
134 cmd = '[0,"we did call you"]'
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
135 print("sending: {}".format(cmd))
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
136 self.request.sendall(cmd.encode('utf-8'))
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
137 response = ""
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
138 elif decoded[1] == '!quit!':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
139 # we're done
7920
1ebc7be4dbbf commit https://github.com/vim/vim/commit/b3e2f00f39d6edafda6e5508a926ebd244997a0f
Christian Brabandt <cb@256bit.org>
parents: 7918
diff changeset
140 self.server.shutdown()
7993
0756eab66b71 commit https://github.com/vim/vim/commit/b92abad0c58de36d0b0afdcd4ec05261fa1fa84c
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
141 return
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
142 elif decoded[1] == '!crash!':
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
143 # Crash!
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
144 42 / 0
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
145 else:
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
146 response = "what?"
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
147
8009
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
148 if response == "":
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
149 print("no response")
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
150 else:
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
151 encoded = json.dumps([decoded[0], response])
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
152 print("sending: {}".format(encoded))
b2cfa3416ba0 commit https://github.com/vim/vim/commit/f6157284de71d8881f3b89fbd79d1ecbf842929f
Christian Brabandt <cb@256bit.org>
parents: 7993
diff changeset
153 self.request.sendall(encoded.encode('utf-8'))
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
154
7912
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
155 # Negative numbers are used for "eval" responses.
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
156 elif decoded[0] < 0:
1c6ef9113556 commit https://github.com/vim/vim/commit/e7bed627c89ed80bc4b2d96f542819029adf6e76
Christian Brabandt <cb@256bit.org>
parents: 7906
diff changeset
157 last_eval = decoded
7906
ea1fd8d750a6 commit https://github.com/vim/vim/commit/fcb1e3d16832ce06da0dc38ecb7ab9aaa3ee4383
Christian Brabandt <cb@256bit.org>
parents: 7902
diff changeset
158
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
159 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
160 pass
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
161
8114
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
162 def writePortInFile(port):
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
163 # Write the port number in Xportnr, so that the test knows it.
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
164 f = open("Xportnr", "w")
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
165 f.write("{}".format(port))
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
166 f.close()
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
167
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
168 if __name__ == "__main__":
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
169 HOST, PORT = "localhost", 0
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
170
8114
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
171 # Wait half a second before opening the port to test waittime in ch_open().
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
172 # We do want to get the port number, get that first. We cannot open the
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
173 # socket, guess a port is free.
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
174 if len(sys.argv) >= 2 and sys.argv[1] == 'delay':
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
175 PORT = 13684
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
176 writePortInFile(PORT)
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
177
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
178 print("Wait for it...")
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
179 time.sleep(0.5)
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
180
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
181 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
182 ip, port = server.server_address
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
183
7937
2e905dfc6999 commit https://github.com/vim/vim/commit/6076fe1986255d32b7a078a28bf9e7bea19d6f30
Christian Brabandt <cb@256bit.org>
parents: 7920
diff changeset
184 # Start a thread with the server. That thread will then start a new thread
2e905dfc6999 commit https://github.com/vim/vim/commit/6076fe1986255d32b7a078a28bf9e7bea19d6f30
Christian Brabandt <cb@256bit.org>
parents: 7920
diff changeset
185 # for each connection.
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
186 server_thread = threading.Thread(target=server.serve_forever)
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
187 server_thread.start()
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
188
8114
4aea0b0aa714 commit https://github.com/vim/vim/commit/81661fb86801e6d6e5194b43dfd27d73fcc016ec
Christian Brabandt <cb@256bit.org>
parents: 8041
diff changeset
189 writePortInFile(port)
7899
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
190
93c61501c2cf commit https://github.com/vim/vim/commit/d7ece1008ee6173afda6d173bed486ae79c1c38a
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
191 print("Listening on port {}".format(port))
7920
1ebc7be4dbbf commit https://github.com/vim/vim/commit/b3e2f00f39d6edafda6e5508a926ebd244997a0f
Christian Brabandt <cb@256bit.org>
parents: 7918
diff changeset
192
1ebc7be4dbbf commit https://github.com/vim/vim/commit/b3e2f00f39d6edafda6e5508a926ebd244997a0f
Christian Brabandt <cb@256bit.org>
parents: 7918
diff changeset
193 # Main thread terminates, but the server continues running
1ebc7be4dbbf commit https://github.com/vim/vim/commit/b3e2f00f39d6edafda6e5508a926ebd244997a0f
Christian Brabandt <cb@256bit.org>
parents: 7918
diff changeset
194 # until server.shutdown() is called.
8153
240deebfadde commit https://github.com/vim/vim/commit/ddbe7d26b10c4374f406b807ae161826cf2096e1
Christian Brabandt <cb@256bit.org>
parents: 8114
diff changeset
195 try:
240deebfadde commit https://github.com/vim/vim/commit/ddbe7d26b10c4374f406b807ae161826cf2096e1
Christian Brabandt <cb@256bit.org>
parents: 8114
diff changeset
196 while server_thread.isAlive():
240deebfadde commit https://github.com/vim/vim/commit/ddbe7d26b10c4374f406b807ae161826cf2096e1
Christian Brabandt <cb@256bit.org>
parents: 8114
diff changeset
197 server_thread.join(1)
240deebfadde commit https://github.com/vim/vim/commit/ddbe7d26b10c4374f406b807ae161826cf2096e1
Christian Brabandt <cb@256bit.org>
parents: 8114
diff changeset
198 except (KeyboardInterrupt, SystemExit):
240deebfadde commit https://github.com/vim/vim/commit/ddbe7d26b10c4374f406b807ae161826cf2096e1
Christian Brabandt <cb@256bit.org>
parents: 8114
diff changeset
199 server.shutdown()