blob: e76c524ffa61f5e676926c19b13a6fc9ceed5066 [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 Moolenaar3b05b132016-02-03 23:25:07 +010060 let handle = ch_open('localhost:' . s:port, 'json')
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 Moolenaara07fec92016-02-05 21:04:08 +010096 " Send a request with a specific handler.
97 call ch_sendexpr(handle, 'hello!', 's:RequestHandler')
98 sleep 10m
99 call assert_equal(handle, s:responseHandle)
100 call assert_equal('got it', s:responseMsg)
101
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100102 " Send an eval request that works.
103 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100104 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100105 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
106
107 " Send an eval request that fails.
108 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100109 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100110 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
111
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100112 " Send a bad eval request. There will be no response.
113 call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100114 sleep 10m
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100115 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
116
Bram Moolenaard7ece102016-02-02 23:23:02 +0100117 " make the server quit, can't check if this works, should not hang.
118 call ch_sendexpr(handle, '!quit!', 0)
119
Bram Moolenaara0f9cd12016-02-03 20:13:24 +0100120 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +0100121endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100122
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100123" Test that we can open two channels.
124func Test_two_channels()
125 let handle = s:start_server()
126 if handle < 0
127 return
128 endif
129 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
130
131 let newhandle = ch_open('localhost:' . s:port, 'json')
132 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
133 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
134
135 call ch_close(handle)
136 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
137
138 call s:kill_server()
139endfunc
140
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100141" Test that a server crash is handled gracefully.
142func Test_server_crash()
143 let handle = s:start_server()
144 if handle < 0
145 return
146 endif
147 call ch_sendexpr(handle, '!crash!')
148
149 " kill the server in case if failed to crash
150 sleep 10m
151 call s:kill_server()
152endfunc