blob: 94ad81cca025bfed71ddef86b036cf64554ae7e8 [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 Moolenaar66624ff2016-02-03 23:59:43 +0100121 " Send a bad eval request. There will be no response.
122 call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100123 sleep 10m
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100124 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
125
Bram Moolenaarf4160862016-02-05 23:09:12 +0100126 " Send an expr request
127 call assert_equal('ok', ch_sendexpr(handle, 'an expr'))
128 sleep 10m
129 call assert_equal('one', getline(line('$') - 2))
130 call assert_equal('two', getline(line('$') - 1))
131 call assert_equal('three', getline('$'))
132
133 " Request a redraw, we don't check for the effect.
134 call assert_equal('ok', ch_sendexpr(handle, 'redraw'))
135 call assert_equal('ok', ch_sendexpr(handle, 'redraw!'))
136
137 call assert_equal('ok', ch_sendexpr(handle, 'empty-request'))
138
Bram Moolenaard7ece102016-02-02 23:23:02 +0100139 " make the server quit, can't check if this works, should not hang.
140 call ch_sendexpr(handle, '!quit!', 0)
141
Bram Moolenaara0f9cd12016-02-03 20:13:24 +0100142 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +0100143endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100144
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100145" Test that we can open two channels.
146func Test_two_channels()
147 let handle = s:start_server()
148 if handle < 0
149 return
150 endif
151 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
152
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100153 let newhandle = ch_open('localhost:' . s:port)
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100154 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
155 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
156
157 call ch_close(handle)
158 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
159
160 call s:kill_server()
161endfunc
162
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100163" Test that a server crash is handled gracefully.
164func Test_server_crash()
165 let handle = s:start_server()
166 if handle < 0
167 return
168 endif
169 call ch_sendexpr(handle, '!crash!')
170
171 " kill the server in case if failed to crash
172 sleep 10m
173 call s:kill_server()
174endfunc