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