Created
February 9, 2016 20:12
-
-
Save ichizok/3820b16b7198847e75c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/testdir/test_channel.vim b/src/testdir/test_channel.vim | |
--- a/src/testdir/test_channel.vim | |
+++ b/src/testdir/test_channel.vim | |
@@ -24,49 +24,57 @@ endif | |
let s:port = -1 | |
-func s:start_server() | |
+func s:start_server(testfunc) | |
" The Python program writes the port number in Xportnr. | |
call delete("Xportnr") | |
- if has('job') | |
- let s:job = job_start("python test_channel.py") | |
- elseif has('win32') | |
- silent !start cmd /c start "test_channel" py test_channel.py | |
- else | |
- silent !python test_channel.py& | |
- endif | |
+ try | |
+ if has('job') | |
+ let s:job = job_start("python test_channel.py") | |
+ elseif has('win32') | |
+ silent !start cmd /c start "test_channel" py test_channel.py | |
+ else | |
+ silent !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 | |
+ " 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 s:kill_server() | |
+ call assert_false(1, "Can't start test_channel.py") | |
+ return -1 | |
endif | |
- sleep 100m | |
- let cnt -= 1 | |
- endwhile | |
- call delete("Xportnr") | |
+ let s:port = l[0] | |
- if len(l) == 0 | |
- " Can't make the connection, give up. | |
+ call call(function(a:testfunc), []) | |
+ catch | |
+ call assert_false(1, "Got exception: " . v:exception) | |
+ finally | |
call s:kill_server() | |
- call assert_false(1, "Can't start test_channel.py") | |
- return -1 | |
- endif | |
- let s:port = l[0] | |
- | |
- let handle = ch_open('localhost:' . s:port) | |
- return handle | |
+ endtry | |
endfunc | |
func s:kill_server() | |
if has('job') | |
- call job_stop(s:job) | |
+ if exists('s:job') | |
+ call job_stop(s:job) | |
+ unlet s:job | |
+ endif | |
elseif has('win32') | |
call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"') | |
else | |
@@ -81,9 +89,10 @@ func s:RequestHandler(handle, msg) | |
let s:responseMsg = a:msg | |
endfunc | |
-func Test_communicate() | |
- let handle = s:start_server() | |
+func s:communicate() | |
+ let handle = ch_open('localhost:' . s:port) | |
if handle < 0 | |
+ call assert_false(1, "Can't open channel") | |
return | |
endif | |
@@ -143,16 +152,20 @@ func Test_communicate() | |
" make the server quit, can't check if this works, should not hang. | |
call ch_sendexpr(handle, '!quit!', 0) | |
+endfunc | |
- call s:kill_server() | |
+func Test_communicate() | |
+ call s:start_server('s:communicate') | |
endfunc | |
" Test that we can open two channels. | |
-func Test_two_channels() | |
- let handle = s:start_server() | |
+func s:two_channels() | |
+ let handle = ch_open('localhost:' . s:port) | |
if handle < 0 | |
+ call assert_false(1, "Can't open channel") | |
return | |
endif | |
+ | |
call assert_equal('got it', ch_sendexpr(handle, 'hello!')) | |
let newhandle = ch_open('localhost:' . s:port) | |
@@ -162,20 +175,29 @@ func Test_two_channels() | |
call ch_close(handle) | |
call assert_equal('got it', ch_sendexpr(newhandle, 'hello!')) | |
- call s:kill_server() | |
+ call ch_close(newhandle) | |
+endfunc | |
+ | |
+func Test_two_channels() | |
+ call s:start_server('s:two_channels') | |
endfunc | |
" Test that a server crash is handled gracefully. | |
-func Test_server_crash() | |
- let handle = s:start_server() | |
+func s:server_crash() | |
+ let handle = ch_open('localhost:' . s:port) | |
if handle < 0 | |
+ call assert_false(1, "Can't open channel") | |
return | |
endif | |
+ | |
call ch_sendexpr(handle, '!crash!') | |
" kill the server in case if failed to crash | |
sleep 10m | |
- call s:kill_server() | |
+endfunc | |
+ | |
+func Test_server_crash() | |
+ call s:start_server('s:server_crash') | |
endfunc | |
" Test that trying to connect to a non-existing port fails quickly. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment