blob: f358117a8f8b69c01a16d30c0cbf91f5626a243d [file] [log] [blame]
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01001" Tests for the +clientserver feature.
2
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02003source check.vim
4CheckFeature job
5CheckFeature clientserver
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01006
7source shared.vim
8
9func Test_client_server()
10 let cmd = GetVimCommand()
11 if cmd == ''
12 return
13 endif
Bram Moolenaara683ec42017-03-25 20:14:34 +010014 if has('x11')
15 if empty($DISPLAY)
16 throw 'Skipped: $DISPLAY is not set'
17 endif
Bram Moolenaara2845b82017-03-25 15:20:06 +010018 try
19 call remote_send('xxx', '')
20 catch
21 if v:exception =~ 'E240:'
Bram Moolenaara683ec42017-03-25 20:14:34 +010022 throw 'Skipped: no connection to the X server'
Bram Moolenaara2845b82017-03-25 15:20:06 +010023 endif
24 " ignore other errors
25 endtry
26 endif
Bram Moolenaar42205552017-03-18 19:42:22 +010027
Bram Moolenaar42205552017-03-18 19:42:22 +010028 let name = 'XVIMTEST'
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010029 let cmd .= ' --servername ' . name
Bram Moolenaarab8b1c12017-11-04 19:24:31 +010030 let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'})
Bram Moolenaar50182fa2018-04-28 21:34:40 +020031 call WaitForAssert({-> assert_equal("run", job_status(job))})
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010032
33 " Takes a short while for the server to be active.
Bram Moolenaar9d189612017-09-09 18:11:00 +020034 " When using valgrind it takes much longer.
Bram Moolenaar50182fa2018-04-28 21:34:40 +020035 call WaitForAssert({-> assert_match(name, serverlist())})
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010036
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020037 eval name->remote_foreground()
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010038
39 call remote_send(name, ":let testvar = 'yes'\<CR>")
Bram Moolenaard97fbf12017-11-02 19:23:03 +010040 call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"')
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010041 call assert_equal('yes', remote_expr(name, "testvar", "", 2))
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010042
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010043 if has('unix') && has('gui') && !has('gui_running')
Bram Moolenaar60964f62017-06-10 17:06:16 +020044 " Running in a terminal and the GUI is available: Tell the server to open
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010045 " the GUI and check that the remote command still works.
46 " Need to wait for the GUI to start up, otherwise the send hangs in trying
47 " to send to the terminal window.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010048 if has('gui_athena') || has('gui_motif')
49 " For those GUIs, ignore the 'failed to create input context' error.
50 call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>")
51 else
52 call remote_send(name, ":gui -f\<CR>")
53 endif
54 " Wait for the server to be up and answering requests.
Bram Moolenaar60964f62017-06-10 17:06:16 +020055 sleep 100m
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020056 call WaitForAssert({-> assert_true(name->remote_expr("v:version", "", 1) != "")})
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010057
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010058 call remote_send(name, ":let testvar = 'maybe'\<CR>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +020059 call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))})
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010060 endif
61
62 call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241')
63
64 " Expression evaluated locally.
65 if v:servername == ''
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020066 eval 'MYSELF'->remote_startserver()
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010067 " May get MYSELF1 when running the test again.
68 call assert_match('MYSELF', v:servername)
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010069 endif
70 let g:testvar = 'myself'
71 call assert_equal('myself', remote_expr(v:servername, 'testvar'))
72
73 call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020074 call assert_equal('got it', g:myserverid->remote_read(2))
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010075
Bram Moolenaar196b4662019-09-06 21:34:30 +020076 call remote_send(name, ":eval expand('<client>')->server2client('another')\<CR>", 'g:myserverid')
Bram Moolenaar6caf6062017-03-18 20:45:05 +010077 let peek_result = 'nothing'
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020078 let r = g:myserverid->remote_peek('peek_result')
Bram Moolenaarab8b1c12017-11-04 19:24:31 +010079 " unpredictable whether the result is already available.
Bram Moolenaar6caf6062017-03-18 20:45:05 +010080 if r > 0
81 call assert_equal('another', peek_result)
82 elseif r == 0
83 call assert_equal('nothing', peek_result)
84 else
85 call assert_report('remote_peek() failed')
86 endif
87 let g:peek_result = 'empty'
88 call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0')
Bram Moolenaar6caf6062017-03-18 20:45:05 +010089 call assert_equal('another', g:peek_result)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010090 call assert_equal('another', remote_read(g:myserverid, 2))
Bram Moolenaar6caf6062017-03-18 20:45:05 +010091
Bram Moolenaara0d1fef2019-09-04 22:29:14 +020092 eval name->remote_send(":qa!\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +010093 try
Bram Moolenaar50182fa2018-04-28 21:34:40 +020094 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +010095 finally
96 if job_status(job) != 'dead'
97 call assert_report('Server did not exit')
98 call job_stop(job, 'kill')
99 endif
100 endtry
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100101endfunc
102
103" Uncomment this line to get a debugging log
104" call ch_logfile('channellog', 'w')