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') |
| 7 | if !executable('python') |
| 8 | finish |
| 9 | endif |
| 10 | elseif has('win32') && !has('gui_win32') |
| 11 | " Use Python Launcher for Windows (py.exe). |
| 12 | if !executable('py') |
| 13 | finish |
| 14 | endif |
| 15 | else |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 16 | finish |
| 17 | endif |
| 18 | |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame^] | 19 | func 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 |
| 25 | endfunc |
| 26 | |
| 27 | func 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 |
| 33 | endfunc |
| 34 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 35 | func Test_communicate() |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame^] | 36 | call delete("Xportnr") |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 37 | " The Python program writes the port number in Xportnr. |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame^] | 38 | call s:start_server() |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 39 | |
| 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 Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame^] | 58 | call s:kill_server() |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 59 | 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 Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame^] | 78 | call s:kill_server() |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 79 | endfunc |