blob: 432171d9b4b5c35575cac1d6f4c0f0c0295da69c [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
2scriptencoding utf-8
3
Bram Moolenaare2469252016-02-04 10:54:34 +01004if !has('channel')
5 finish
6endif
7
8" This test requires the Python command to run the test server.
Bram Moolenaara8343c12016-02-04 22:09:48 +01009" This most likely only works on Unix and Windows.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010010if has('unix')
Bram Moolenaar835dc632016-02-07 14:27:38 +010011 " We also need the job feature or the pkill command to make sure the server
12 " can be stopped.
13 if !(executable('python') && (has('job') || executable('pkill')))
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010014 finish
15 endif
Bram Moolenaara8343c12016-02-04 22:09:48 +010016elseif has('win32')
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010017 " Use Python Launcher for Windows (py.exe).
18 if !executable('py')
19 finish
20 endif
21else
Bram Moolenaard7ece102016-02-02 23:23:02 +010022 finish
23endif
24
Bram Moolenaar3b05b132016-02-03 23:25:07 +010025let s:port = -1
Bram Moolenaara4833262016-02-09 23:33:25 +010026let s:chopt = has('macunix') ? {'waittime' : 1} : {}
Bram Moolenaar3b05b132016-02-03 23:25:07 +010027
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010028func s:start_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010029 " The Python program writes the port number in Xportnr.
30 call delete("Xportnr")
31
Bram Moolenaar835dc632016-02-07 14:27:38 +010032 if has('job')
33 let s:job = job_start("python test_channel.py")
34 elseif has('win32')
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010035 silent !start cmd /c start "test_channel" py test_channel.py
36 else
Bram Moolenaarf92591f2016-02-03 20:22:32 +010037 silent !python test_channel.py&
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010038 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010039
40 " Wait for up to 2 seconds for the port number to be there.
41 let cnt = 20
42 let l = []
43 while cnt > 0
44 try
45 let l = readfile("Xportnr")
46 catch
47 endtry
48 if len(l) >= 1
49 break
50 endif
51 sleep 100m
52 let cnt -= 1
53 endwhile
54 call delete("Xportnr")
55
56 if len(l) == 0
57 " Can't make the connection, give up.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010058 call s:kill_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010059 call assert_false(1, "Can't start test_channel.py")
60 return -1
Bram Moolenaard7ece102016-02-02 23:23:02 +010061 endif
Bram Moolenaar3b05b132016-02-03 23:25:07 +010062 let s:port = l[0]
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010063
Bram Moolenaara4833262016-02-09 23:33:25 +010064 let handle = ch_open('localhost:' . s:port, s:chopt)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010065 return handle
66endfunc
67
68func s:kill_server()
Bram Moolenaar835dc632016-02-07 14:27:38 +010069 if has('job')
70 call job_stop(s:job)
71 elseif has('win32')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010072 call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
73 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010074 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010075 endif
76endfunc
77
Bram Moolenaara07fec92016-02-05 21:04:08 +010078let s:responseHandle = -1
79let s:responseMsg = ''
80func s:RequestHandler(handle, msg)
81 let s:responseHandle = a:handle
82 let s:responseMsg = a:msg
83endfunc
84
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010085func Test_communicate()
86 let handle = s:start_server()
87 if handle < 0
88 return
89 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010090
91 " Simple string request and reply.
92 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
93
94 " Request that triggers sending two ex commands. These will usually be
95 " handled before getting the response, but it's not guaranteed, thus wait a
96 " tiny bit for the commands to get executed.
97 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
98 sleep 10m
99 call assert_equal('added1', getline(line('$') - 1))
100 call assert_equal('added2', getline('$'))
101
Bram Moolenaarf4160862016-02-05 23:09:12 +0100102 call assert_equal('ok', ch_sendexpr(handle, 'do normal'))
103 sleep 10m
104 call assert_equal('added more', getline('$'))
105
Bram Moolenaara07fec92016-02-05 21:04:08 +0100106 " Send a request with a specific handler.
107 call ch_sendexpr(handle, 'hello!', 's:RequestHandler')
108 sleep 10m
109 call assert_equal(handle, s:responseHandle)
110 call assert_equal('got it', s:responseMsg)
111
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100112 " Send an eval request that works.
113 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100114 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100115 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
116
117 " Send an eval request that fails.
118 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100119 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100120 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
121
Bram Moolenaar55fab432016-02-07 16:53:13 +0100122 " Send an eval request that works but can't be encoded.
123 call assert_equal('ok', ch_sendexpr(handle, 'eval-error'))
124 sleep 10m
125 call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
126
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100127 " Send a bad eval request. There will be no response.
128 call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100129 sleep 10m
Bram Moolenaar55fab432016-02-07 16:53:13 +0100130 call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100131
Bram Moolenaarf4160862016-02-05 23:09:12 +0100132 " Send an expr request
133 call assert_equal('ok', ch_sendexpr(handle, 'an expr'))
134 sleep 10m
135 call assert_equal('one', getline(line('$') - 2))
136 call assert_equal('two', getline(line('$') - 1))
137 call assert_equal('three', getline('$'))
138
139 " Request a redraw, we don't check for the effect.
140 call assert_equal('ok', ch_sendexpr(handle, 'redraw'))
141 call assert_equal('ok', ch_sendexpr(handle, 'redraw!'))
142
143 call assert_equal('ok', ch_sendexpr(handle, 'empty-request'))
144
Bram Moolenaard7ece102016-02-02 23:23:02 +0100145 " make the server quit, can't check if this works, should not hang.
146 call ch_sendexpr(handle, '!quit!', 0)
147
Bram Moolenaara0f9cd12016-02-03 20:13:24 +0100148 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +0100149endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100150
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100151" Test that we can open two channels.
152func Test_two_channels()
153 let handle = s:start_server()
154 if handle < 0
155 return
156 endif
157 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
158
Bram Moolenaara4833262016-02-09 23:33:25 +0100159 let newhandle = ch_open('localhost:' . s:port, s:chopt)
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100160 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
161 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
162
163 call ch_close(handle)
164 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
165
166 call s:kill_server()
167endfunc
168
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100169" Test that a server crash is handled gracefully.
170func Test_server_crash()
171 let handle = s:start_server()
172 if handle < 0
173 return
174 endif
175 call ch_sendexpr(handle, '!crash!')
176
177 " kill the server in case if failed to crash
178 sleep 10m
179 call s:kill_server()
180endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100181
182" Test that trying to connect to a non-existing port fails quickly.
183func Test_connect_waittime()
184 let start = reltime()
Bram Moolenaara4833262016-02-09 23:33:25 +0100185 let handle = ch_open('localhost:9876', s:chopt)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100186 if handle >= 0
187 " Oops, port does exists.
188 call ch_close(handle)
189 else
190 let elapsed = reltime(start)
Bram Moolenaar74f5e652016-02-07 21:44:49 +0100191 call assert_true(reltimefloat(elapsed) < 1.0)
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100192 endif
193
194 let start = reltime()
195 let handle = ch_open('localhost:9867', {'waittime': 2000})
196 if handle >= 0
197 " Oops, port does exists.
198 call ch_close(handle)
199 else
Bram Moolenaar0fa98e72016-02-07 22:21:19 +0100200 " Failed connection doesn't wait the full time on Unix.
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100201 let elapsed = reltime(start)
Bram Moolenaar0fa98e72016-02-07 22:21:19 +0100202 call assert_true(reltimefloat(elapsed) < (has('unix') ? 1.0 : 3.0))
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100203 endif
204endfunc