Skip to content

Commit d6a8d48

Browse files
committed
patch 7.4.1298
Problem: When the channel test fails in an unexpected way the server keeps running. Solution: Use try/catch. (Ozaki Kiichi)
1 parent a483326 commit d6a8d48

2 files changed

Lines changed: 72 additions & 44 deletions

File tree

src/testdir/test_channel.vim

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,63 @@ elseif has('win32')
1919
finish
2020
endif
2121
else
22+
" Can't run this test.
2223
finish
2324
endif
2425

25-
let s:port = -1
2626
let s:chopt = has('macunix') ? {'waittime' : 1} : {}
2727

28-
func s:start_server()
28+
" Run "testfunc" after sarting the server and stop the server afterwards.
29+
func s:run_server(testfunc)
2930
" The Python program writes the port number in Xportnr.
3031
call delete("Xportnr")
3132

32-
if has('job')
33-
let s:job = job_start("python test_channel.py")
34-
elseif has('win32')
35-
silent !start cmd /c start "test_channel" py test_channel.py
36-
else
37-
silent !python test_channel.py&
38-
endif
33+
try
34+
if has('job')
35+
let s:job = job_start("python test_channel.py")
36+
elseif has('win32')
37+
silent !start cmd /c start "test_channel" py test_channel.py
38+
else
39+
silent !python test_channel.py&
40+
endif
3941

40-
" Wait for up to 2 seconds for the port number to be there.
41-
let cnt = 20
42-
let l = []
43-
while cnt > 0
44-
try
45-
let l = readfile("Xportnr")
46-
catch
47-
endtry
48-
if len(l) >= 1
49-
break
42+
" Wait for up to 2 seconds for the port number to be there.
43+
let cnt = 20
44+
let l = []
45+
while cnt > 0
46+
try
47+
let l = readfile("Xportnr")
48+
catch
49+
endtry
50+
if len(l) >= 1
51+
break
52+
endif
53+
sleep 100m
54+
let cnt -= 1
55+
endwhile
56+
call delete("Xportnr")
57+
58+
if len(l) == 0
59+
" Can't make the connection, give up.
60+
call assert_false(1, "Can't start test_channel.py")
61+
return -1
5062
endif
51-
sleep 100m
52-
let cnt -= 1
53-
endwhile
54-
call delete("Xportnr")
63+
let port = l[0]
5564

56-
if len(l) == 0
57-
" Can't make the connection, give up.
65+
call call(function(a:testfunc), [port])
66+
catch
67+
call assert_false(1, "Caught exception: " . v:exception)
68+
finally
5869
call s:kill_server()
59-
call assert_false(1, "Can't start test_channel.py")
60-
return -1
61-
endif
62-
let s:port = l[0]
63-
64-
let handle = ch_open('localhost:' . s:port, s:chopt)
65-
return handle
70+
endtry
6671
endfunc
6772

6873
func s:kill_server()
6974
if has('job')
70-
call job_stop(s:job)
75+
if exists('s:job')
76+
call job_stop(s:job)
77+
unlet s:job
78+
endif
7179
elseif has('win32')
7280
call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
7381
else
@@ -82,9 +90,10 @@ func s:RequestHandler(handle, msg)
8290
let s:responseMsg = a:msg
8391
endfunc
8492

85-
func Test_communicate()
86-
let handle = s:start_server()
93+
func s:communicate(port)
94+
let handle = ch_open('localhost:' . a:port, s:chopt)
8795
if handle < 0
96+
call assert_false(1, "Can't open channel")
8897
return
8998
endif
9099

@@ -144,39 +153,55 @@ func Test_communicate()
144153

145154
" make the server quit, can't check if this works, should not hang.
146155
call ch_sendexpr(handle, '!quit!', 0)
156+
endfunc
147157

148-
call s:kill_server()
158+
func Test_communicate()
159+
call s:run_server('s:communicate')
149160
endfunc
150161

151162
" Test that we can open two channels.
152-
func Test_two_channels()
153-
let handle = s:start_server()
163+
func s:two_channels(port)
164+
let handle = ch_open('localhost:' . a:port)
154165
if handle < 0
166+
call assert_false(1, "Can't open channel")
155167
return
156168
endif
169+
157170
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
158171

159-
let newhandle = ch_open('localhost:' . s:port, s:chopt)
172+
let newhandle = ch_open('localhost:' . a:port, s:chopt)
173+
if newhandle < 0
174+
call assert_false(1, "Can't open second channel")
175+
return
176+
endif
160177
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
161178
call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
162179

163180
call ch_close(handle)
164181
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
165182

166-
call s:kill_server()
183+
call ch_close(newhandle)
184+
endfunc
185+
186+
func Test_two_channels()
187+
call s:run_server('s:two_channels')
167188
endfunc
168189

169190
" Test that a server crash is handled gracefully.
170-
func Test_server_crash()
171-
let handle = s:start_server()
191+
func s:server_crash(port)
192+
let handle = ch_open('localhost:' . a:port, s:chopt)
172193
if handle < 0
194+
call assert_false(1, "Can't open channel")
173195
return
174196
endif
197+
175198
call ch_sendexpr(handle, '!crash!')
176199

177-
" kill the server in case if failed to crash
178200
sleep 10m
179-
call s:kill_server()
201+
endfunc
202+
203+
func Test_server_crash()
204+
call s:run_server('s:server_crash')
180205
endfunc
181206

182207
" Test that trying to connect to a non-existing port fails quickly.
@@ -198,6 +223,7 @@ func Test_connect_waittime()
198223
call ch_close(handle)
199224
else
200225
" Failed connection doesn't wait the full time on Unix.
226+
" TODO: why is MS-Windows different?
201227
let elapsed = reltime(start)
202228
call assert_true(reltimefloat(elapsed) < (has('unix') ? 1.0 : 3.0))
203229
endif

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ static char *(features[]) =
747747

748748
static int included_patches[] =
749749
{ /* Add new patch number below this line */
750+
/**/
751+
1298,
750752
/**/
751753
1297,
752754
/**/

0 commit comments

Comments
 (0)