blob: 3caf5d21d933bb37b8ecdca9501f57e1b134e7db [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
2scriptencoding utf-8
3
4" This requires the Python command to run the test server.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +01005" This most likely only works on Unix and Windows console.
6if has('unix')
Bram Moolenaarf92591f2016-02-03 20:22:32 +01007 " We also need the pkill command to make sure the server can be stopped.
8 if !executable('python') || !executable('pkill')
Bram Moolenaara0f9cd12016-02-03 20:13:24 +01009 finish
10 endif
11elseif has('win32') && !has('gui_win32')
12 " Use Python Launcher for Windows (py.exe).
13 if !executable('py')
14 finish
15 endif
16else
Bram Moolenaard7ece102016-02-02 23:23:02 +010017 finish
18endif
19
Bram Moolenaar3b05b132016-02-03 23:25:07 +010020let s:port = -1
21
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010022func s:start_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010023 " The Python program writes the port number in Xportnr.
24 call delete("Xportnr")
25
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010026 if has('win32')
27 silent !start cmd /c start "test_channel" py test_channel.py
28 else
Bram Moolenaarf92591f2016-02-03 20:22:32 +010029 silent !python test_channel.py&
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010030 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010031
32 " Wait for up to 2 seconds for the port number to be there.
33 let cnt = 20
34 let l = []
35 while cnt > 0
36 try
37 let l = readfile("Xportnr")
38 catch
39 endtry
40 if len(l) >= 1
41 break
42 endif
43 sleep 100m
44 let cnt -= 1
45 endwhile
46 call delete("Xportnr")
47
48 if len(l) == 0
49 " Can't make the connection, give up.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010050 call s:kill_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010051 call assert_false(1, "Can't start test_channel.py")
52 return -1
Bram Moolenaard7ece102016-02-02 23:23:02 +010053 endif
Bram Moolenaar3b05b132016-02-03 23:25:07 +010054 let s:port = l[0]
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010055
Bram Moolenaar3b05b132016-02-03 23:25:07 +010056 let handle = ch_open('localhost:' . s:port, 'json')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010057 return handle
58endfunc
59
60func s:kill_server()
61 if has('win32')
62 call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
63 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010064 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010065 endif
66endfunc
67
68func Test_communicate()
69 let handle = s:start_server()
70 if handle < 0
71 return
72 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010073
74 " Simple string request and reply.
75 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
76
77 " Request that triggers sending two ex commands. These will usually be
78 " handled before getting the response, but it's not guaranteed, thus wait a
79 " tiny bit for the commands to get executed.
80 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
81 sleep 10m
82 call assert_equal('added1', getline(line('$') - 1))
83 call assert_equal('added2', getline('$'))
84
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010085 " Send an eval request that works.
86 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
87 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
88
89 " Send an eval request that fails.
90 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
91 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
92
Bram Moolenaar66624ff2016-02-03 23:59:43 +010093 " Send a bad eval request. There will be no response.
94 call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
95 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
96
Bram Moolenaard7ece102016-02-02 23:23:02 +010097 " make the server quit, can't check if this works, should not hang.
98 call ch_sendexpr(handle, '!quit!', 0)
99
Bram Moolenaara0f9cd12016-02-03 20:13:24 +0100100 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +0100101endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100102
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100103" Test that we can open two channels.
104func Test_two_channels()
105 let handle = s:start_server()
106 if handle < 0
107 return
108 endif
109 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
110
111 let newhandle = ch_open('localhost:' . s:port, 'json')
112 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
113 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
114
115 call ch_close(handle)
116 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
117
118 call s:kill_server()
119endfunc
120
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100121" Test that a server crash is handled gracefully.
122func Test_server_crash()
123 let handle = s:start_server()
124 if handle < 0
125 return
126 endif
127 call ch_sendexpr(handle, '!crash!')
128
129 " kill the server in case if failed to crash
130 sleep 10m
131 call s:kill_server()
132endfunc