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