Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 1 | " Tests for the +clientserver feature. |
| 2 | |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | CheckFeature job |
| 5 | CheckFeature clientserver |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 6 | |
| 7 | source shared.vim |
| 8 | |
| 9 | func Test_client_server() |
| 10 | let cmd = GetVimCommand() |
| 11 | if cmd == '' |
| 12 | return |
| 13 | endif |
Bram Moolenaar | a683ec4 | 2017-03-25 20:14:34 +0100 | [diff] [blame] | 14 | if has('x11') |
| 15 | if empty($DISPLAY) |
| 16 | throw 'Skipped: $DISPLAY is not set' |
| 17 | endif |
Bram Moolenaar | a2845b8 | 2017-03-25 15:20:06 +0100 | [diff] [blame] | 18 | try |
| 19 | call remote_send('xxx', '') |
| 20 | catch |
| 21 | if v:exception =~ 'E240:' |
Bram Moolenaar | a683ec4 | 2017-03-25 20:14:34 +0100 | [diff] [blame] | 22 | throw 'Skipped: no connection to the X server' |
Bram Moolenaar | a2845b8 | 2017-03-25 15:20:06 +0100 | [diff] [blame] | 23 | endif |
| 24 | " ignore other errors |
| 25 | endtry |
| 26 | endif |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 27 | |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 28 | let name = 'XVIMTEST' |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 29 | let cmd .= ' --servername ' . name |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 30 | let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'}) |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 31 | call WaitForAssert({-> assert_equal("run", job_status(job))}) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 32 | |
| 33 | " Takes a short while for the server to be active. |
Bram Moolenaar | 9d18961 | 2017-09-09 18:11:00 +0200 | [diff] [blame] | 34 | " When using valgrind it takes much longer. |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 35 | call WaitForAssert({-> assert_match(name, serverlist())}) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 36 | |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 37 | eval name->remote_foreground() |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 38 | |
| 39 | call remote_send(name, ":let testvar = 'yes'\<CR>") |
Bram Moolenaar | d97fbf1 | 2017-11-02 19:23:03 +0100 | [diff] [blame] | 40 | call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"') |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 41 | call assert_equal('yes', remote_expr(name, "testvar", "", 2)) |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 42 | call assert_fails("let x=remote_expr(name, '2+x')", 'E449:') |
| 43 | call assert_fails("let x=remote_expr('[], '2+2')", 'E116:') |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 44 | |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 45 | if has('unix') && has('gui') && !has('gui_running') |
Bram Moolenaar | 60964f6 | 2017-06-10 17:06:16 +0200 | [diff] [blame] | 46 | " Running in a terminal and the GUI is available: Tell the server to open |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 47 | " the GUI and check that the remote command still works. |
| 48 | " Need to wait for the GUI to start up, otherwise the send hangs in trying |
| 49 | " to send to the terminal window. |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 50 | if has('gui_athena') || has('gui_motif') |
| 51 | " For those GUIs, ignore the 'failed to create input context' error. |
| 52 | call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>") |
| 53 | else |
| 54 | call remote_send(name, ":gui -f\<CR>") |
| 55 | endif |
| 56 | " Wait for the server to be up and answering requests. |
Bram Moolenaar | 60964f6 | 2017-06-10 17:06:16 +0200 | [diff] [blame] | 57 | sleep 100m |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 58 | call WaitForAssert({-> assert_true(name->remote_expr("v:version", "", 1) != "")}) |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 59 | |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 60 | call remote_send(name, ":let testvar = 'maybe'\<CR>") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 61 | call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))}) |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 62 | endif |
| 63 | |
| 64 | call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241') |
| 65 | |
| 66 | " Expression evaluated locally. |
| 67 | if v:servername == '' |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 68 | eval 'MYSELF'->remote_startserver() |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 69 | " May get MYSELF1 when running the test again. |
| 70 | call assert_match('MYSELF', v:servername) |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 71 | call assert_fails("call remote_startserver('MYSELF')", 'E941:') |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 72 | endif |
| 73 | let g:testvar = 'myself' |
| 74 | call assert_equal('myself', remote_expr(v:servername, 'testvar')) |
| 75 | |
| 76 | call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid') |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 77 | call assert_equal('got it', g:myserverid->remote_read(2)) |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 78 | |
Bram Moolenaar | 196b466 | 2019-09-06 21:34:30 +0200 | [diff] [blame] | 79 | call remote_send(name, ":eval expand('<client>')->server2client('another')\<CR>", 'g:myserverid') |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 80 | let peek_result = 'nothing' |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 81 | let r = g:myserverid->remote_peek('peek_result') |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 82 | " unpredictable whether the result is already available. |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 83 | if r > 0 |
| 84 | call assert_equal('another', peek_result) |
| 85 | elseif r == 0 |
| 86 | call assert_equal('nothing', peek_result) |
| 87 | else |
| 88 | call assert_report('remote_peek() failed') |
| 89 | endif |
| 90 | let g:peek_result = 'empty' |
| 91 | call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0') |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 92 | call assert_equal('another', g:peek_result) |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 93 | call assert_equal('another', remote_read(g:myserverid, 2)) |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 94 | |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 95 | eval name->remote_send(":qa!\<CR>") |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 96 | try |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 97 | call WaitForAssert({-> assert_equal("dead", job_status(job))}) |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 98 | finally |
| 99 | if job_status(job) != 'dead' |
| 100 | call assert_report('Server did not exit') |
| 101 | call job_stop(job, 'kill') |
| 102 | endif |
| 103 | endtry |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 104 | |
| 105 | call assert_fails("let x=remote_peek([])", 'E730:') |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 106 | call assert_fails("let x=remote_read('vim10')", 'E277:') |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 107 | endfunc |
| 108 | |
| 109 | " Uncomment this line to get a debugging log |
| 110 | " call ch_logfile('channellog', 'w') |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 111 | |
| 112 | " vim: shiftwidth=2 sts=2 expandtab |