1818endif
1919
2020func s: start_server ()
21+ " The Python program writes the port number in Xportnr.
22+ call delete (" Xportnr" )
23+
2124 if has (' win32' )
2225 silent ! start cmd /c start " test_channel" py test_channel.py
2326 else
2427 silent ! python test_channel.py &
2528 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 ()
4029
4130 " Wait for up to 2 seconds for the port number to be there.
4231 let cnt = 20
@@ -57,10 +46,28 @@ func Test_communicate()
5746 if len (l ) == 0
5847 " Can't make the connection, give up.
5948 call s: kill_server ()
60- return
49+ call assert_false (1 , " Can't start test_channel.py" )
50+ return -1
6151 endif
6252 let port = l [0 ]
53+
6354 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
69+ return
70+ endif
6471
6572 " Simple string request and reply.
6673 call assert_equal (' got it' , ch_sendexpr (handle, ' hello!' ))
@@ -73,8 +80,29 @@ func Test_communicate()
7380 call assert_equal (' added1' , getline (line (' $' ) - 1 ))
7481 call assert_equal (' added2' , getline (' $' ))
7582
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+
7691 " make the server quit, can't check if this works, should not hang.
7792 call ch_sendexpr (handle, ' !quit!' , 0 )
7893
7994 call s: kill_server ()
8095endfunc
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 10 m
107+ call s: kill_server ()
108+ endfunc
0 commit comments