blob: 140691c3984c1ba9b951145799906383b9b0e191 [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.
5" This most likely only works on Unix.
6if !has('unix') || !executable('python')
7 finish
8endif
9
10func Test_communicate()
11 " The Python program writes the port number in Xportnr.
12 silent !./test_channel.py&
13
14 " Wait for up to 2 seconds for the port number to be there.
15 let cnt = 20
16 let l = []
17 while cnt > 0
18 try
19 let l = readfile("Xportnr")
20 catch
21 endtry
22 if len(l) >= 1
23 break
24 endif
25 sleep 100m
26 let cnt -= 1
27 endwhile
28 call delete("Xportnr")
29
30 if len(l) == 0
31 " Can't make the connection, give up.
32 call system("killall test_channel.py")
33 return
34 endif
35 let port = l[0]
36 let handle = ch_open('localhost:' . port, 'json')
37
38 " Simple string request and reply.
39 call assert_equal('got it', ch_sendexpr(handle, 'hello!'))
40
41 " Request that triggers sending two ex commands. These will usually be
42 " handled before getting the response, but it's not guaranteed, thus wait a
43 " tiny bit for the commands to get executed.
44 call assert_equal('ok', ch_sendexpr(handle, 'make change'))
45 sleep 10m
46 call assert_equal('added1', getline(line('$') - 1))
47 call assert_equal('added2', getline('$'))
48
49 " make the server quit, can't check if this works, should not hang.
50 call ch_sendexpr(handle, '!quit!', 0)
51
52 call system("killall test_channel.py")
53endfunc