blob: 46bd364311a602b8f6f8818ccb1308c27cda95d8 [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 Moolenaarf92591f2016-02-03 20:22:32 +010011 " We also need the pkill command to make sure the server can be stopped.
12 if !executable('python') || !executable('pkill')
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010013 finish
14 endif
Bram Moolenaara8343c12016-02-04 22:09:48 +010015elseif has('win32')
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010016 " Use Python Launcher for Windows (py.exe).
17 if !executable('py')
18 finish
19 endif
20else
Bram Moolenaard7ece102016-02-02 23:23:02 +010021 finish
22endif
23
Bram Moolenaar3b05b132016-02-03 23:25:07 +010024let s:port = -1
25
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010026func s:start_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010027 " The Python program writes the port number in Xportnr.
28 call delete("Xportnr")
29
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010030 if has('win32')
31 silent !start cmd /c start "test_channel" py test_channel.py
32 else
Bram Moolenaarf92591f2016-02-03 20:22:32 +010033 silent !python test_channel.py&
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010034 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010035
36 " Wait for up to 2 seconds for the port number to be there.
37 let cnt = 20
38 let l = []
39 while cnt > 0
40 try
41 let l = readfile("Xportnr")
42 catch
43 endtry
44 if len(l) >= 1
45 break
46 endif
47 sleep 100m
48 let cnt -= 1
49 endwhile
50 call delete("Xportnr")
51
52 if len(l) == 0
53 " Can't make the connection, give up.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010054 call s:kill_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010055 call assert_false(1, "Can't start test_channel.py")
56 return -1
Bram Moolenaard7ece102016-02-02 23:23:02 +010057 endif
Bram Moolenaar3b05b132016-02-03 23:25:07 +010058 let s:port = l[0]
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010059
Bram Moolenaar4d919d72016-02-05 22:36:41 +010060 let handle = ch_open('localhost:' . s:port)
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010061 return handle
62endfunc
63
64func s:kill_server()
65 if has('win32')
66 call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
67 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010068 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010069 endif
70endfunc
71
Bram Moolenaara07fec92016-02-05 21:04:08 +010072let s:responseHandle = -1
73let s:responseMsg = ''
74func s:RequestHandler(handle, msg)
75 let s:responseHandle = a:handle
76 let s:responseMsg = a:msg
77endfunc
78
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010079func Test_communicate()
80 let handle = s:start_server()
81 if handle < 0
82 return
83 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010084
85 " Simple string request and reply.
86 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
87
88 " Request that triggers sending two ex commands. These will usually be
89 " handled before getting the response, but it's not guaranteed, thus wait a
90 " tiny bit for the commands to get executed.
91 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
92 sleep 10m
93 call assert_equal('added1', getline(line('$') - 1))
94 call assert_equal('added2', getline('$'))
95
Bram Moolenaarf4160862016-02-05 23:09:12 +010096 call assert_equal('ok', ch_sendexpr(handle, 'do normal'))
97 sleep 10m
98 call assert_equal('added more', getline('$'))
99
Bram Moolenaara07fec92016-02-05 21:04:08 +0100100 " Send a request with a specific handler.
101 call ch_sendexpr(handle, 'hello!', 's:RequestHandler')
102 sleep 10m
103 call assert_equal(handle, s:responseHandle)
104 call assert_equal('got it', s:responseMsg)
105
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100106 " Send an eval request that works.
107 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100108 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100109 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
110
111 " Send an eval request that fails.
112 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100113 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100114 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
115
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100116 " Send a bad eval request. There will be no response.
117 call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100118 sleep 10m
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100119 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
120
Bram Moolenaarf4160862016-02-05 23:09:12 +0100121 " Send an expr request
122 call assert_equal('ok', ch_sendexpr(handle, 'an expr'))
123 sleep 10m
124 call assert_equal('one', getline(line('$') - 2))
125 call assert_equal('two', getline(line('$') - 1))
126 call assert_equal('three', getline('$'))
127
128 " Request a redraw, we don't check for the effect.
129 call assert_equal('ok', ch_sendexpr(handle, 'redraw'))
130 call assert_equal('ok', ch_sendexpr(handle, 'redraw!'))
131
132 call assert_equal('ok', ch_sendexpr(handle, 'empty-request'))
133
Bram Moolenaard7ece102016-02-02 23:23:02 +0100134 " make the server quit, can't check if this works, should not hang.
135 call ch_sendexpr(handle, '!quit!', 0)
136
Bram Moolenaara0f9cd12016-02-03 20:13:24 +0100137 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +0100138endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100139
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100140" Test that we can open two channels.
141func Test_two_channels()
142 let handle = s:start_server()
143 if handle < 0
144 return
145 endif
146 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
147
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100148 let newhandle = ch_open('localhost:' . s:port)
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100149 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
150 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
151
152 call ch_close(handle)
153 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
154
155 call s:kill_server()
156endfunc
157
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100158" Test that a server crash is handled gracefully.
159func Test_server_crash()
160 let handle = s:start_server()
161 if handle < 0
162 return
163 endif
164 call ch_sendexpr(handle, '!crash!')
165
166 " kill the server in case if failed to crash
167 sleep 10m
168 call s:kill_server()
169endfunc