comparison 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
comparison
equal deleted inserted replaced
7905:dfdf2ee817fa 7906:ea1fd8d750a6
16 else 16 else
17 finish 17 finish
18 endif 18 endif
19 19
20 func s:start_server() 20 func s:start_server()
21 " The Python program writes the port number in Xportnr.
22 call delete("Xportnr")
23
21 if has('win32') 24 if has('win32')
22 silent !start cmd /c start "test_channel" py test_channel.py 25 silent !start cmd /c start "test_channel" py test_channel.py
23 else 26 else
24 silent !python test_channel.py& 27 silent !python test_channel.py&
25 endif 28 endif
26 endfunc
27
28 func s:kill_server()
29 if has('win32')
30 call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
31 else
32 call system("pkill --full test_channel.py")
33 endif
34 endfunc
35
36 func Test_communicate()
37 call delete("Xportnr")
38 " The Python program writes the port number in Xportnr.
39 call s:start_server()
40 29
41 " Wait for up to 2 seconds for the port number to be there. 30 " Wait for up to 2 seconds for the port number to be there.
42 let cnt = 20 31 let cnt = 20
43 let l = [] 32 let l = []
44 while cnt > 0 33 while cnt > 0
55 call delete("Xportnr") 44 call delete("Xportnr")
56 45
57 if len(l) == 0 46 if len(l) == 0
58 " Can't make the connection, give up. 47 " Can't make the connection, give up.
59 call s:kill_server() 48 call s:kill_server()
49 call assert_false(1, "Can't start test_channel.py")
50 return -1
51 endif
52 let port = l[0]
53
54 let handle = ch_open('localhost:' . port, 'json')
55 return handle
56 endfunc
57
58 func s:kill_server()
59 if has('win32')
60 call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
61 else
62 call system("pkill --full test_channel.py")
63 endif
64 endfunc
65
66 func Test_communicate()
67 let handle = s:start_server()
68 if handle < 0
60 return 69 return
61 endif 70 endif
62 let port = l[0]
63 let handle = ch_open('localhost:' . port, 'json')
64 71
65 " Simple string request and reply. 72 " Simple string request and reply.
66 call assert_equal('got it', ch_sendexpr(handle, 'hello!')) 73 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
67 74
68 " Request that triggers sending two ex commands. These will usually be 75 " Request that triggers sending two ex commands. These will usually be
71 call assert_equal('ok', ch_sendexpr(handle, 'make change')) 78 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
72 sleep 10m 79 sleep 10m
73 call assert_equal('added1', getline(line('$') - 1)) 80 call assert_equal('added1', getline(line('$') - 1))
74 call assert_equal('added2', getline('$')) 81 call assert_equal('added2', getline('$'))
75 82
83 " Send an eval request that works.
84 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
85 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
86
87 " Send an eval request that fails.
88 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
89 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
90
76 " make the server quit, can't check if this works, should not hang. 91 " make the server quit, can't check if this works, should not hang.
77 call ch_sendexpr(handle, '!quit!', 0) 92 call ch_sendexpr(handle, '!quit!', 0)
78 93
79 call s:kill_server() 94 call s:kill_server()
80 endfunc 95 endfunc
96
97 " Test that a server crash is handled gracefully.
98 func Test_server_crash()
99 let handle = s:start_server()
100 if handle < 0
101 return
102 endif
103 call ch_sendexpr(handle, '!crash!')
104
105 " kill the server in case if failed to crash
106 sleep 10m
107 call s:kill_server()
108 endfunc