blob: a819961536c34151cb71c9d443b6ae9025dde5fb [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
72func Test_communicate()
73 let handle = s:start_server()
74 if handle < 0
75 return
76 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010077
78 " Simple string request and reply.
79 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
80
81 " Request that triggers sending two ex commands. These will usually be
82 " handled before getting the response, but it's not guaranteed, thus wait a
83 " tiny bit for the commands to get executed.
84 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
85 sleep 10m
86 call assert_equal('added1', getline(line('$') - 1))
87 call assert_equal('added2', getline('$'))
88
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010089 " Send an eval request that works.
90 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
Bram Moolenaara02b3212016-02-04 21:03:33 +010091 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010092 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
93
94 " Send an eval request that fails.
95 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
Bram Moolenaara02b3212016-02-04 21:03:33 +010096 sleep 10m
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010097 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
98
Bram Moolenaar66624ff2016-02-03 23:59:43 +010099 " Send a bad eval request. There will be no response.
100 call assert_equal('ok', ch_sendexpr(handle, 'eval-bad'))
Bram Moolenaara02b3212016-02-04 21:03:33 +0100101 sleep 10m
Bram Moolenaar66624ff2016-02-03 23:59:43 +0100102 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
103
Bram Moolenaard7ece102016-02-02 23:23:02 +0100104 " make the server quit, can't check if this works, should not hang.
105 call ch_sendexpr(handle, '!quit!', 0)
106
Bram Moolenaara0f9cd12016-02-03 20:13:24 +0100107 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +0100108endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100109
Bram Moolenaar3b05b132016-02-03 23:25:07 +0100110" Test that we can open two channels.
111func Test_two_channels()
112 let handle = s:start_server()
113 if handle < 0
114 return
115 endif
116 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
117
118 let newhandle = ch_open('localhost:' . s:port, 'json')
119 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
120 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
121
122 call ch_close(handle)
123 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
124
125 call s:kill_server()
126endfunc
127
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100128" Test that a server crash is handled gracefully.
129func Test_server_crash()
130 let handle = s:start_server()
131 if handle < 0
132 return
133 endif
134 call ch_sendexpr(handle, '!crash!')
135
136 " kill the server in case if failed to crash
137 sleep 10m
138 call s:kill_server()
139endfunc