Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 1 | " Test for channel functions. |
| 2 | scriptencoding utf-8 |
| 3 | |
| 4 | " This requires the Python command to run the test server. |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 5 | " This most likely only works on Unix and Windows console. |
| 6 | if has('unix') |
Bram Moolenaar | f92591f | 2016-02-03 20:22:32 +0100 | [diff] [blame^] | 7 | " We also need the pkill command to make sure the server can be stopped. |
| 8 | if !executable('python') || !executable('pkill') |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 9 | finish |
| 10 | endif |
| 11 | elseif has('win32') && !has('gui_win32') |
| 12 | " Use Python Launcher for Windows (py.exe). |
| 13 | if !executable('py') |
| 14 | finish |
| 15 | endif |
| 16 | else |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 17 | finish |
| 18 | endif |
| 19 | |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 20 | func s:start_server() |
| 21 | if has('win32') |
| 22 | silent !start cmd /c start "test_channel" py test_channel.py |
| 23 | else |
Bram Moolenaar | f92591f | 2016-02-03 20:22:32 +0100 | [diff] [blame^] | 24 | silent !python test_channel.py& |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 25 | endif |
| 26 | endfunc |
| 27 | |
| 28 | func s:kill_server() |
| 29 | if has('win32') |
| 30 | call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"') |
| 31 | else |
Bram Moolenaar | f92591f | 2016-02-03 20:22:32 +0100 | [diff] [blame^] | 32 | call system("pkill --full test_channel.py") |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 33 | endif |
| 34 | endfunc |
| 35 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 36 | func Test_communicate() |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 37 | call delete("Xportnr") |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 38 | " The Python program writes the port number in Xportnr. |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 39 | call s:start_server() |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 40 | |
| 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 Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 59 | call s:kill_server() |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 60 | 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 Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 79 | call s:kill_server() |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 80 | endfunc |