blob: ee7a422447f97910a4b1716614a8704861cdf155 [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')
7 if !executable('python')
8 finish
9 endif
10elseif has('win32') && !has('gui_win32')
11 " Use Python Launcher for Windows (py.exe).
12 if !executable('py')
13 finish
14 endif
15else
Bram Moolenaard7ece102016-02-02 23:23:02 +010016 finish
17endif
18
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010019func s:start_server()
20 if has('win32')
21 silent !start cmd /c start "test_channel" py test_channel.py
22 else
23 silent !./test_channel.py&
24 endif
25endfunc
26
27func s:kill_server()
28 if has('win32')
29 call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"')
30 else
31 call system("killall test_channel.py")
32 endif
33endfunc
34
Bram Moolenaard7ece102016-02-02 23:23:02 +010035func Test_communicate()
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010036 call delete("Xportnr")
Bram Moolenaard7ece102016-02-02 23:23:02 +010037 " The Python program writes the port number in Xportnr.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010038 call s:start_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +010039
40 " Wait for up to 2 seconds for the port number to be there.
41 let cnt = 20
42 let l = []
43 while cnt > 0
44 try
45 let l = readfile("Xportnr")
46 catch
47 endtry
48 if len(l) >= 1
49 break
50 endif
51 sleep 100m
52 let cnt -= 1
53 endwhile
54 call delete("Xportnr")
55
56 if len(l) == 0
57 " Can't make the connection, give up.
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010058 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +010059 return
60 endif
61 let port = l[0]
62 let handle = ch_open('localhost:' . port, 'json')
63
64 " Simple string request and reply.
65 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
66
67 " Request that triggers sending two ex commands. These will usually be
68 " handled before getting the response, but it's not guaranteed, thus wait a
69 " tiny bit for the commands to get executed.
70 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
71 sleep 10m
72 call assert_equal('added1', getline(line('$') - 1))
73 call assert_equal('added2', getline('$'))
74
75 " make the server quit, can't check if this works, should not hang.
76 call ch_sendexpr(handle, '!quit!', 0)
77
Bram Moolenaara0f9cd12016-02-03 20:13:24 +010078 call s:kill_server()
Bram Moolenaard7ece102016-02-02 23:23:02 +010079endfunc