blob: 8fcc73b41ebd701095d12e14c6f8e83a1697dd90 [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
26
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010027func s:start_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010028 " The Python program writes the port number in Xportnr.
29 call delete("Xportnr")
30
Bram Moolenaar835dc632016-02-07 14:27:38 +010031 if has('job')
32 let s:job = job_start("python test_channel.py")
33 elseif has('win32')
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010034 silent !start cmd /c start "test_channel" py test_channel.py
35 else
Bram Moolenaarf92591f2016-02-03 20:22:32 +010036 silent !python test_channel.py&
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010037 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010038
39 " Wait for up to 2 seconds for the port number to be there.
40 let cnt = 20
41 let l = []
42 while cnt > 0
43 try
44 let l = readfile("Xportnr")
45 catch
46 endtry
47 if len(l) >= 1
48 break
49 endif
50 sleep 100m
51 let cnt -= 1
52 endwhile
53 call delete("Xportnr")
54
55 if len(l) == 0
56 " Can't make the connection, give up.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010057 call s:kill_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010058 call assert_false(1, "Can't start test_channel.py")
59 return -1
Bram Moolenaard7ece102016-02-02 23:23:02 +010060 endif
Bram Moolenaar3b05b132016-02-03 23:25:07 +010061 let s:port = l[0]
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010062
Bram Moolenaar4d919d72016-02-05 22:36:41 +010063 let handle = ch_open('localhost:' . s:port)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010064 return handle
65endfunc
66
67func s:kill_server()
Bram Moolenaar835dc632016-02-07 14:27:38 +010068 if has('job')
69 call job_stop(s:job)
70 elseif has('win32')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010071 call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
72 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010073 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010074 endif
75endfunc
76
Bram Moolenaara07fec92016-02-05 21:04:08 +010077let s:responseHandle = -1
78let s:responseMsg = ''
79func s:RequestHandler(handle, msg)
80 let s:responseHandle = a:handle
81 let s:responseMsg = a:msg
82endfunc
83
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010084func Test_communicate()
85 let handle = s:start_server()
86 if handle < 0
87 return
88 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010089
90 " Simple string request and reply.
91 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
92
93 " Request that triggers sending two ex commands. These will usually be
94 " handled before getting the response, but it's not guaranteed, thus wait a
95 " tiny bit for the commands to get executed.
96 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
97 sleep 10m
98 call assert_equal('added1', getline(line('$') - 1))
99 call assert_equal('added2', getline('$'))
100
Bram Moolenaarf4160862016-02-05 23:09:12 +0100101 call assert_equal('ok', ch_sendexpr(handle, 'do normal'))
102 sleep 10m
103 call assert_equal('added more', getline('$'))
104
Bram Moolenaara07fec92016-02-05 21:04:08 +0100105 " Send a request with a specific handler.
106 call ch_sendexpr(handle, 'hello!', 's:RequestHandler')
107 sleep 10m
108 call assert_equal(handle, s:responseHandle)
109 call assert_equal('got it', s:responseMsg)
110
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100111 " Send an eval request that works.
112 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100113 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100114 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
115
116 " Send an eval request that fails.
117 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100118 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100119 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
120
Bram Moolenaar55fab432016-02-07 16:53:13 +0100121 " Send an eval request that works but can't be encoded.
122 call assert_equal('ok', ch_sendexpr(handle, 'eval-error'))
123 sleep 10m
124 call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
125
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100126 " Send a bad eval request. There will be no response.
127 call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100128 sleep 10m
Bram Moolenaar55fab432016-02-07 16:53:13 +0100129 call assert_equal([-3, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100130
Bram Moolenaarf4160862016-02-05 23:09:12 +0100131 " Send an expr request
132 call assert_equal('ok', ch_sendexpr(handle, 'an expr'))
133 sleep 10m
134 call assert_equal('one', getline(line('$') - 2))
135 call assert_equal('two', getline(line('$') - 1))
136 call assert_equal('three', getline('$'))
137
138 " Request a redraw, we don't check for the effect.
139 call assert_equal('ok', ch_sendexpr(handle, 'redraw'))
140 call assert_equal('ok', ch_sendexpr(handle, 'redraw!'))
141
142 call assert_equal('ok', ch_sendexpr(handle, 'empty-request'))
143
Bram Moolenaard7ece102016-02-02 23:23:02 +0100144 " make the server quit, can't check if this works, should not hang.
145 call ch_sendexpr(handle, '!quit!', 0)
146
Bram Moolenaara0f9cd12016-02-03 20:13:24 +0100147 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +0100148endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100149
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100150" Test that we can open two channels.
151func Test_two_channels()
152 let handle = s:start_server()
153 if handle < 0
154 return
155 endif
156 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
157
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100158 let newhandle = ch_open('localhost:' . s:port)
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100159 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
160 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
161
162 call ch_close(handle)
163 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
164
165 call s:kill_server()
166endfunc
167
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100168" Test that a server crash is handled gracefully.
169func Test_server_crash()
170 let handle = s:start_server()
171 if handle < 0
172 return
173 endif
174 call ch_sendexpr(handle, '!crash!')
175
176 " kill the server in case if failed to crash
177 sleep 10m
178 call s:kill_server()
179endfunc
Bram Moolenaar7a84dbe2016-02-07 21:29:00 +0100180
181" Test that trying to connect to a non-existing port fails quickly.
182func Test_connect_waittime()
183 let start = reltime()
184 let handle = ch_open('localhost:9876')
185 if handle >= 0
186 " Oops, port does exists.
187 call ch_close(handle)
188 else
189 let elapsed = reltime(start)
190 call assert_true(elapsed < 1.0)
191 endif
192
193 let start = reltime()
194 let handle = ch_open('localhost:9867', {'waittime': 2000})
195 if handle >= 0
196 " Oops, port does exists.
197 call ch_close(handle)
198 else
199 " Failed connection doesn't wait the full time.
200 let elapsed = reltime(start)
201 call assert_true(elapsed < 1.0)
202 endif
203endfunc