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 | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame^] | 20 | let s:port = -1 |
| 21 | |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 22 | func s:start_server() |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 23 | " The Python program writes the port number in Xportnr. |
| 24 | call delete("Xportnr") |
| 25 | |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 26 | if has('win32') |
| 27 | silent !start cmd /c start "test_channel" py test_channel.py |
| 28 | else |
Bram Moolenaar | f92591f | 2016-02-03 20:22:32 +0100 | [diff] [blame] | 29 | silent !python test_channel.py& |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 30 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 31 | |
| 32 | " Wait for up to 2 seconds for the port number to be there. |
| 33 | let cnt = 20 |
| 34 | let l = [] |
| 35 | while cnt > 0 |
| 36 | try |
| 37 | let l = readfile("Xportnr") |
| 38 | catch |
| 39 | endtry |
| 40 | if len(l) >= 1 |
| 41 | break |
| 42 | endif |
| 43 | sleep 100m |
| 44 | let cnt -= 1 |
| 45 | endwhile |
| 46 | call delete("Xportnr") |
| 47 | |
| 48 | if len(l) == 0 |
| 49 | " Can't make the connection, give up. |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 50 | call s:kill_server() |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 51 | call assert_false(1, "Can't start test_channel.py") |
| 52 | return -1 |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 53 | endif |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame^] | 54 | let s:port = l[0] |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 55 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame^] | 56 | let handle = ch_open('localhost:' . s:port, 'json') |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 57 | return handle |
| 58 | endfunc |
| 59 | |
| 60 | func s:kill_server() |
| 61 | if has('win32') |
| 62 | call system('taskkill /IM py.exe /T /F /FI "WINDOWTITLE eq test_channel"') |
| 63 | else |
Bram Moolenaar | 608a891 | 2016-02-03 22:39:51 +0100 | [diff] [blame] | 64 | call system("pkill -f test_channel.py") |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 65 | endif |
| 66 | endfunc |
| 67 | |
| 68 | func Test_communicate() |
| 69 | let handle = s:start_server() |
| 70 | if handle < 0 |
| 71 | return |
| 72 | endif |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 73 | |
| 74 | " Simple string request and reply. |
| 75 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 76 | |
| 77 | " Request that triggers sending two ex commands. These will usually be |
| 78 | " handled before getting the response, but it's not guaranteed, thus wait a |
| 79 | " tiny bit for the commands to get executed. |
| 80 | call assert_equal('ok', ch_sendexpr(handle, 'make change')) |
| 81 | sleep 10m |
| 82 | call assert_equal('added1', getline(line('$') - 1)) |
| 83 | call assert_equal('added2', getline('$')) |
| 84 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 85 | " Send an eval request that works. |
| 86 | call assert_equal('ok', ch_sendexpr(handle, 'eval-works')) |
| 87 | call assert_equal([-1, 'foo123'], ch_sendexpr(handle, 'eval-result')) |
| 88 | |
| 89 | " Send an eval request that fails. |
| 90 | call assert_equal('ok', ch_sendexpr(handle, 'eval-fails')) |
| 91 | call assert_equal([-2, 'ERROR'], ch_sendexpr(handle, 'eval-result')) |
| 92 | |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 93 | " make the server quit, can't check if this works, should not hang. |
| 94 | call ch_sendexpr(handle, '!quit!', 0) |
| 95 | |
Bram Moolenaar | a0f9cd1 | 2016-02-03 20:13:24 +0100 | [diff] [blame] | 96 | call s:kill_server() |
Bram Moolenaar | d7ece10 | 2016-02-02 23:23:02 +0100 | [diff] [blame] | 97 | endfunc |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 98 | |
Bram Moolenaar | 3b05b13 | 2016-02-03 23:25:07 +0100 | [diff] [blame^] | 99 | " Test that we can open two channels. |
| 100 | func Test_two_channels() |
| 101 | let handle = s:start_server() |
| 102 | if handle < 0 |
| 103 | return |
| 104 | endif |
| 105 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 106 | |
| 107 | let newhandle = ch_open('localhost:' . s:port, 'json') |
| 108 | call assert_equal('got it', ch_sendexpr(newhandle, 'hello!')) |
| 109 | call assert_equal('got it', ch_sendexpr(handle, 'hello!')) |
| 110 | |
| 111 | call ch_close(handle) |
| 112 | call assert_equal('got it', ch_sendexpr(newhandle, 'hello!')) |
| 113 | |
| 114 | call s:kill_server() |
| 115 | endfunc |
| 116 | |
Bram Moolenaar | fcb1e3d | 2016-02-03 21:32:46 +0100 | [diff] [blame] | 117 | " Test that a server crash is handled gracefully. |
| 118 | func Test_server_crash() |
| 119 | let handle = s:start_server() |
| 120 | if handle < 0 |
| 121 | return |
| 122 | endif |
| 123 | call ch_sendexpr(handle, '!crash!') |
| 124 | |
| 125 | " kill the server in case if failed to crash |
| 126 | sleep 10m |
| 127 | call s:kill_server() |
| 128 | endfunc |