diff src/testdir/test_channel.vim @ 7906:ea1fd8d750a6 v7.4.1249

commit https://github.com/vim/vim/commit/fcb1e3d16832ce06da0dc38ecb7ab9aaa3ee4383 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Feb 3 21:32:46 2016 +0100 patch 7.4.1249 Problem: Crash when the process a channel is connected to exits. Solution: Use the file descriptor properly. Add a test. (Damien) Also add a test for eval().
author Christian Brabandt <cb@256bit.org>
date Wed, 03 Feb 2016 21:45:04 +0100
parents 14a5de0990a5
children 35973ce58c84
line wrap: on
line diff
--- a/src/testdir/test_channel.vim
+++ b/src/testdir/test_channel.vim
@@ -18,25 +18,14 @@ else
 endif
 
 func s:start_server()
+  " The Python program writes the port number in Xportnr.
+  call delete("Xportnr")
+
   if has('win32')
     silent !start cmd /c start "test_channel" py test_channel.py
   else
     silent !python test_channel.py&
   endif
-endfunc
-
-func s:kill_server()
-  if has('win32')
-    call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
-  else
-    call system("pkill --full test_channel.py")
-  endif
-endfunc
-
-func Test_communicate()
-  call delete("Xportnr")
-  " The Python program writes the port number in Xportnr.
-  call s:start_server()
 
   " Wait for up to 2 seconds for the port number to be there.
   let cnt = 20
@@ -57,10 +46,28 @@ func Test_communicate()
   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
+  let port = l[0]
+
+  let handle = ch_open('localhost:' . port, 'json')
+  return handle
+endfunc
+
+func s:kill_server()
+  if has('win32')
+    call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
+  else
+    call system("pkill --full test_channel.py")
+  endif
+endfunc
+
+func Test_communicate()
+  let handle = s:start_server()
+  if handle < 0
     return
   endif
-  let port = l[0]
-  let handle = ch_open('localhost:' . port, 'json')
 
   " Simple string request and reply.
   call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
@@ -73,8 +80,29 @@ func Test_communicate()
   call assert_equal('added1', getline(line('$') - 1))
   call assert_equal('added2', getline('$'))
 
+  " Send an eval request that works.
+  call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
+  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'))
+  call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
+
   " make the server quit, can't check if this works, should not hang.
   call ch_sendexpr(handle, '!quit!', 0)
 
   call s:kill_server()
 endfunc
+
+" Test that a server crash is handled gracefully.
+func Test_server_crash()
+  let handle = s:start_server()
+  if handle < 0
+    return
+  endif
+  call ch_sendexpr(handle, '!crash!')
+
+  " kill the server in case if failed to crash
+  sleep 10m
+  call s:kill_server()
+endfunc