1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
" Test for channel functions.
scriptencoding utf-8
if !has('channel')
finish
endif
" This test requires the Python command to run the test server.
" This most likely only works on Unix and Windows.
if has('unix')
" We also need the job feature or the pkill command to make sure the server
" can be stopped.
if !(executable('python') && (has('job') || executable('pkill')))
finish
endif
let s:python = 'python'
elseif has('win32')
" Use Python Launcher for Windows (py.exe) if available.
if executable('py.exe')
let s:python = 'py.exe'
elseif executable('python.exe')
let s:python = 'python.exe'
else
finish
endif
else
" Can't run this test.
finish
endif
let s:chopt = has('osx') ? {'waittime' : 1} : {}
" Run "testfunc" after sarting the server and stop the server afterwards.
func s:run_server(testfunc)
" The Python program writes the port number in Xportnr.
call delete("Xportnr")
try
if has('job')
let s:job = job_start(s:python . " test_channel.py")
elseif has('win32')
exe 'silent !start cmd /c start "test_channel" ' . s:python . ' test_channel.py'
else
exe 'silent !' . s:python . ' test_channel.py&'
endif
" Wait for up to 2 seconds for the port number to be there.
let cnt = 20
let l = []
while cnt > 0
try
let l = readfile("Xportnr")
catch
endtry
if len(l) >= 1
break
endif
sleep 100m
let cnt -= 1
endwhile
call delete("Xportnr")
if len(l) == 0
" Can't make the connection, give up.
call assert_false(1, "Can't start test_channel.py")
return -1
endif
let port = l[0]
call call(function(a:testfunc), [port])
catch
call assert_false(1, "Caught exception: " . v:exception)
finally
call s:kill_server()
endtry
endfunc
func s:kill_server()
if has('job')
if exists('s:job')
call job_stop(s:job)
unlet s:job
endif
elseif has('win32')
call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq test_channel"')
else
call system("pkill -f test_channel.py")
endif
endfunc
let s:responseMsg = ''
func s:RequestHandler(handle, msg)
let s:responseHandle = a:handle
let s:responseMsg = a:msg
endfunc
func s:communicate(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
" Simple string request and reply.
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
" Request that triggers sending two ex commands. These will usually be
" handled before getting the response, but it's not guaranteed, thus wait a
" tiny bit for the commands to get executed.
call assert_equal('ok', ch_sendexpr(handle, 'make change'))
sleep 10m
call assert_equal('added1', getline(line('$') - 1))
call assert_equal('added2', getline('$'))
call assert_equal('ok', ch_sendexpr(handle, 'do normal'))
sleep 10m
call assert_equal('added more', getline('$'))
" Send a request with a specific handler.
call ch_sendexpr(handle, 'hello!', 's:RequestHandler')
sleep 10m
if !exists('s:responseHandle')
call assert_false(1, 's:responseHandle was not set')
else
call assert_equal(handle, s:responseHandle)
endif
call assert_equal('got it', s:responseMsg)
unlet s:responseHandle
let s:responseMsg = ''
call ch_sendexpr(handle, 'hello!', function('s:RequestHandler'))
sleep 10m
if !exists('s:responseHandle')
call assert_false(1, 's:responseHandle was not set')
else
call assert_equal(handle, s:responseHandle)
endif
call assert_equal('got it', s:responseMsg)
" Send an eval request that works.
call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
sleep 10m
call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
" Send an eval request that fails.
call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
sleep 10m
call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
" Send an eval request that works but can't be encoded.
call assert_equal('ok', ch_sendexpr(handle, 'eval-error'))
sleep 10m
call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
" Send a bad eval request. There will be no response.
call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
sleep 10m
call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
" Send an expr request
call assert_equal('ok', ch_sendexpr(handle, 'an expr'))
sleep 10m
call assert_equal('one', getline(line('$') - 2))
call assert_equal('two', getline(line('$') - 1))
call assert_equal('three', getline('$'))
" Request a redraw, we don't check for the effect.
call assert_equal('ok', ch_sendexpr(handle, 'redraw'))
call assert_equal('ok', ch_sendexpr(handle, 'redraw!'))
call assert_equal('ok', ch_sendexpr(handle, 'empty-request'))
" make the server quit, can't check if this works, should not hang.
call ch_sendexpr(handle, '!quit!', 0)
endfunc
func Test_communicate()
call s:run_server('s:communicate')
endfunc
" Test that we can open two channels.
func s:two_channels(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
let newhandle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(newhandle) == "fail"
call assert_false(1, "Can't open second channel")
return
endif
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
call ch_close(handle)
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
call ch_close(newhandle)
endfunc
func Test_two_channels()
call s:run_server('s:two_channels')
endfunc
" Test that a server crash is handled gracefully.
func s:server_crash(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
call ch_sendexpr(handle, '!crash!')
sleep 10m
endfunc
func Test_server_crash()
call s:run_server('s:server_crash')
endfunc
let s:reply = ""
func s:Handler(chan, msg)
unlet s:reply
let s:reply = a:msg
endfunc
func s:channel_handler(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
" Test that it works while waiting on a numbered message.
call assert_equal('ok', ch_sendexpr(handle, 'call me'))
sleep 10m
call assert_equal('we called you', s:reply)
" Test that it works while not waiting on a numbered message.
call ch_sendexpr(handle, 'call me again', 0)
sleep 10m
call assert_equal('we did call you', s:reply)
endfunc
func Test_channel_handler()
let s:chopt.callback = 's:Handler'
call s:run_server('s:channel_handler')
let s:chopt.callback = function('s:Handler')
call s:run_server('s:channel_handler')
unlet s:chopt.callback
endfunc
" Test that trying to connect to a non-existing port fails quickly.
func Test_connect_waittime()
if !has('unix')
" TODO: Make this work again for MS-Windows.
return
endif
let start = reltime()
let handle = ch_open('localhost:9876', s:chopt)
if ch_status(handle) == "fail"
" Oops, port does exists.
call ch_close(handle)
else
let elapsed = reltime(start)
call assert_true(reltimefloat(elapsed) < 1.0)
endif
let start = reltime()
let handle = ch_open('localhost:9867', {'waittime': 2000})
if ch_status(handle) != "fail"
" Oops, port does exists.
call ch_close(handle)
else
" Failed connection doesn't wait the full time on Unix.
" TODO: why is MS-Windows different?
let elapsed = reltime(start)
call assert_true(reltimefloat(elapsed) < (has('unix') ? 1.0 : 3.0))
endif
endfunc
func Test_pipe()
if !has('job')
return
endif
let job = job_start(s:python . " test_channel_pipe.py")
call assert_equal("run", job_status(job))
try
let handle = job_getchannel(job)
call ch_sendraw(handle, "echo something\n", 0)
call assert_equal("something\n", ch_readraw(handle))
let reply = ch_sendraw(handle, "quit\n")
call assert_equal("Goodbye!\n", reply)
finally
call job_stop(job)
endtry
endfunc
""""""""""
let s:unletResponse = ''
func s:UnletHandler(handle, msg)
let s:unletResponse = a:msg
unlet s:channelfd
endfunc
" Test that "unlet handle" in a handler doesn't crash Vim.
func s:unlet_handle(port)
let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
call ch_sendexpr(s:channelfd, "test", function('s:UnletHandler'))
sleep 10m
call assert_equal('what?', s:unletResponse)
endfunc
func Test_unlet_handle()
call s:run_server('s:unlet_handle')
endfunc
""""""""""
let s:unletResponse = ''
func s:CloseHandler(handle, msg)
let s:unletResponse = a:msg
call ch_close(s:channelfd)
endfunc
" Test that "unlet handle" in a handler doesn't crash Vim.
func s:close_handle(port)
let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
call ch_sendexpr(s:channelfd, "test", function('s:CloseHandler'))
sleep 10m
call assert_equal('what?', s:unletResponse)
endfunc
func Test_close_handle()
call s:run_server('s:close_handle')
endfunc
""""""""""
func Test_open_fail()
silent! let ch = ch_open("noserver")
echo ch
let d = ch
endfunc
|