Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 1 | " Tests for the +clientserver feature. |
| 2 | |
| 3 | if !has('job') || !has('clientserver') |
| 4 | finish |
| 5 | endif |
| 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 |
| 30 | let g:job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'}) |
| 31 | call WaitFor('job_status(g:job) == "run"') |
| 32 | if job_status(g:job) != 'run' |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 33 | call assert_report('Cannot run the Vim server') |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 34 | return |
| 35 | endif |
| 36 | |
| 37 | " Takes a short while for the server to be active. |
Bram Moolenaar | 9d18961 | 2017-09-09 18:11:00 +0200 | [diff] [blame] | 38 | " When using valgrind it takes much longer. |
| 39 | call WaitFor('serverlist() =~ "' . name . '"', 5000) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 40 | call assert_match(name, serverlist()) |
| 41 | |
| 42 | call remote_foreground(name) |
| 43 | |
| 44 | call remote_send(name, ":let testvar = 'yes'\<CR>") |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 45 | call WaitFor('remote_expr("' . name . '", "testvar", "", 1) == "yes"') |
| 46 | call assert_equal('yes', remote_expr(name, "testvar", "", 2)) |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 47 | |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 48 | if has('unix') && has('gui') && !has('gui_running') |
Bram Moolenaar | 60964f6 | 2017-06-10 17:06:16 +0200 | [diff] [blame] | 49 | " 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] | 50 | " the GUI and check that the remote command still works. |
| 51 | " Need to wait for the GUI to start up, otherwise the send hangs in trying |
| 52 | " to send to the terminal window. |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 53 | if has('gui_athena') || has('gui_motif') |
| 54 | " For those GUIs, ignore the 'failed to create input context' error. |
| 55 | call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>") |
| 56 | else |
| 57 | call remote_send(name, ":gui -f\<CR>") |
| 58 | endif |
| 59 | " Wait for the server to be up and answering requests. |
Bram Moolenaar | 60964f6 | 2017-06-10 17:06:16 +0200 | [diff] [blame] | 60 | sleep 100m |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 61 | call WaitFor('remote_expr("' . name . '", "v:version", "", 1) != ""') |
Bram Moolenaar | 60964f6 | 2017-06-10 17:06:16 +0200 | [diff] [blame] | 62 | call assert_true(remote_expr(name, "v:version", "", 1) != "") |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 63 | |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 64 | call remote_send(name, ":let testvar = 'maybe'\<CR>") |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 65 | call WaitFor('remote_expr("' . name . '", "testvar", "", 1) == "maybe"') |
| 66 | call assert_equal('maybe', remote_expr(name, "testvar", "", 2)) |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 67 | endif |
| 68 | |
| 69 | call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241') |
| 70 | |
| 71 | " Expression evaluated locally. |
| 72 | if v:servername == '' |
| 73 | call remote_startserver('MYSELF') |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 74 | " May get MYSELF1 when running the test again. |
| 75 | call assert_match('MYSELF', v:servername) |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 76 | endif |
| 77 | let g:testvar = 'myself' |
| 78 | call assert_equal('myself', remote_expr(v:servername, 'testvar')) |
| 79 | |
| 80 | call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid') |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 81 | call assert_equal('got it', remote_read(g:myserverid, 2)) |
Bram Moolenaar | 7416f3e | 2017-03-18 18:10:13 +0100 | [diff] [blame] | 82 | |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 83 | call remote_send(name, ":call server2client(expand('<client>'), 'another')\<CR>", 'g:myserverid') |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 84 | let peek_result = 'nothing' |
| 85 | let r = remote_peek(g:myserverid, 'peek_result') |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 86 | " unpredictable whether the result is already avaialble. |
| 87 | if r > 0 |
| 88 | call assert_equal('another', peek_result) |
| 89 | elseif r == 0 |
| 90 | call assert_equal('nothing', peek_result) |
| 91 | else |
| 92 | call assert_report('remote_peek() failed') |
| 93 | endif |
| 94 | let g:peek_result = 'empty' |
| 95 | call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0') |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 96 | call assert_equal('another', g:peek_result) |
Bram Moolenaar | 81b9d0b | 2017-03-19 21:20:53 +0100 | [diff] [blame] | 97 | call assert_equal('another', remote_read(g:myserverid, 2)) |
Bram Moolenaar | 6caf606 | 2017-03-18 20:45:05 +0100 | [diff] [blame] | 98 | |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 99 | call remote_send(name, ":qa!\<CR>") |
| 100 | call WaitFor('job_status(g:job) == "dead"') |
| 101 | if job_status(g:job) != 'dead' |
Bram Moolenaar | 4220555 | 2017-03-18 19:42:22 +0100 | [diff] [blame] | 102 | call assert_report('Server did not exit') |
Bram Moolenaar | 15bf76d | 2017-03-18 16:18:37 +0100 | [diff] [blame] | 103 | call job_stop(g:job, 'kill') |
| 104 | endif |
| 105 | endfunc |
| 106 | |
| 107 | " Uncomment this line to get a debugging log |
| 108 | " call ch_logfile('channellog', 'w') |