blob: d1289903e78400fe1983e17afd0d387e817ed812 [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 Moolenaara0f9cd12016-02-03 20:13:24 +010020func s:start_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010021 " The Python program writes the port number in Xportnr.
22 call delete("Xportnr")
23
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010024 if has('win32')
25 silent !start cmd /c start "test_channel" py test_channel.py
26 else
Bram Moolenaarf92591f2016-02-03 20:22:32 +010027 silent !python test_channel.py&
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010028 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010029
30 " Wait for up to 2 seconds for the port number to be there.
31 let cnt = 20
32 let l = []
33 while cnt > 0
34 try
35 let l = readfile("Xportnr")
36 catch
37 endtry
38 if len(l) >= 1
39 break
40 endif
41 sleep 100m
42 let cnt -= 1
43 endwhile
44 call delete("Xportnr")
45
46 if len(l) == 0
47 " Can't make the connection, give up.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010048 call s:kill_server()
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010049 call assert_false(1, "Can't start test_channel.py")
50 return -1
Bram Moolenaard7ece102016-02-02 23:23:02 +010051 endif
52 let port = l[0]
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010053
Bram Moolenaard7ece102016-02-02 23:23:02 +010054 let handle = ch_open('localhost:' . port, 'json')
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010055 return handle
56endfunc
57
58func s:kill_server()
59 if has('win32')
60 call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
61 else
62 call system("pkill --full test_channel.py")
63 endif
64endfunc
65
66func Test_communicate()
67 let handle = s:start_server()
68 if handle < 0
69 return
70 endif
Bram Moolenaard7ece102016-02-02 23:23:02 +010071
72 " Simple string request and reply.
73 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
74
75 " Request that triggers sending two ex commands. These will usually be
76 " handled before getting the response, but it's not guaranteed, thus wait a
77 " tiny bit for the commands to get executed.
78 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
79 sleep 10m
80 call assert_equal('added1', getline(line('$') - 1))
81 call assert_equal('added2', getline('$'))
82
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010083 " Send an eval request that works.
84 call assert_equal('ok', ch_sendexpr(handle, 'eval-works'))
85 call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result'))
86
87 " Send an eval request that fails.
88 call assert_equal('ok', ch_sendexpr(handle, 'eval-fails'))
89 call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result'))
90
Bram Moolenaard7ece102016-02-02 23:23:02 +010091 " make the server quit, can't check if this works, should not hang.
92 call ch_sendexpr(handle, '!quit!', 0)
93
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010094 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +010095endfunc
Bram Moolenaarfcb1e3d2016-02-03 21:32:46 +010096
97" Test that a server crash is handled gracefully.
98func Test_server_crash()
99 let handle = s:start_server()
100 if handle < 0
101 return
102 endif
103 call ch_sendexpr(handle, '!crash!')
104
105 " kill the server in case if failed to crash
106 sleep 10m
107 call s:kill_server()
108endfunc