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