blob: b416520e45e27b955b939475f42ee9bc523cbf75 [file] [log] [blame]
Bram Moolenaard7ece102016-02-02 23:23:02 +01001" Test for channel functions.
2scriptencoding utf-8
3
4" This requires the Python command to run the test server.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +01005" This most likely only works on Unix and Windows console.
6if has('unix')
Bram Moolenaarf92591f2016-02-03 20:22:32 +01007 " We also need the pkill command to make sure the server can be stopped.
8 if !executable('python') || !executable('pkill')
Bram Moolenaara0f9cd12016-02-03 20:13:24 +01009 finish
10 endif
11elseif has('win32') && !has('gui_win32')
12 " Use Python Launcher for Windows (py.exe).
13 if !executable('py')
14 finish
15 endif
16else
Bram Moolenaard7ece102016-02-02 23:23:02 +010017 finish
18endif
19
Bram Moolenaar3b05b132016-02-03 23:25:07 +010020let s:port = -1
21
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010022func s:start_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010023 " The Python program writes the port number in Xportnr.
24 call delete("Xportnr")
25
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010026 if has('win32')
27 silent !start cmd /c start "test_channel" py test_channel.py
28 else
Bram Moolenaarf92591f2016-02-03 20:22:32 +010029 silent !python test_channel.py&
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010030 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010031
32 " Wait for up to 2 seconds for the port number to be there.
33 let cnt = 20
34 let l = []
35 while cnt > 0
36 try
37 let l = readfile("Xportnr")
38 catch
39 endtry
40 if len(l) >= 1
41 break
42 endif
43 sleep 100m
44 let cnt -= 1
45 endwhile
46 call delete("Xportnr")
47
48 if len(l) == 0
49 " Can't make the connection, give up.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010050 call s:kill_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010051 call assert_false(1, "Can't start test_channel.py")
52 return -1
Bram Moolenaard7ece102016-02-02 23:23:02 +010053 endif
Bram Moolenaar3b05b132016-02-03 23:25:07 +010054 let s:port = l[0]
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010055
Bram Moolenaar3b05b132016-02-03 23:25:07 +010056 let handle = ch_open('localhost:' . s:port, 'json')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010057 return handle
58endfunc
59
60func s:kill_server()
61 if has('win32')
62 call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
63 else
Bram Moolenaar608a8912016-02-03 22:39:51 +010064 call system("pkill -f test_channel.py")
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010065 endif
66endfunc
67
68func Test_communicate()
69 let handle = s:start_server()
70 if handle < 0
71 return
72 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010073
74 " Simple string request and reply.
75 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
76
77 " Request that triggers sending two ex commands. These will usually be
78 " handled before getting the response, but it's not guaranteed, thus wait a
79 " tiny bit for the commands to get executed.
80 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
81 sleep 10m
82 call assert_equal('added1', getline(line('$') - 1))
83 call assert_equal('added2', getline('$'))
84
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010085 " Send an eval request that works.
86 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
87 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
88
89 " Send an eval request that fails.
90 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
91 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
92
Bram Moolenaard7ece102016-02-02 23:23:02 +010093 " make the server quit, can't check if this works, should not hang.
94 call ch_sendexpr(handle, '!quit!', 0)
95
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010096 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +010097endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010098
Bram Moolenaar3b05b132016-02-03 23:25:07 +010099" Test that we can open two channels.
100func Test_two_channels()
101 let handle = s:start_server()
102 if handle < 0
103 return
104 endif
105 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
106
107 let newhandle = ch_open('localhost:' . s:port, 'json')
108 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
109 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
110
111 call ch_close(handle)
112 call assert_equal('got it', ch_sendexpr(newhandle, 'hello!'))
113
114 call s:kill_server()
115endfunc
116
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +0100117" Test that a server crash is handled gracefully.
118func Test_server_crash()
119 let handle = s:start_server()
120 if handle < 0
121 return
122 endif
123 call ch_sendexpr(handle, '!crash!')
124
125 " kill the server in case if failed to crash
126 sleep 10m
127 call s:kill_server()
128endfunc