1" Tests for the +clientserver feature.
2
3if !has('job') || !has('clientserver')
4  finish
5endif
6
7source shared.vim
8
9func Test_client_server()
10  let cmd = GetVimCommand()
11  if cmd == ''
12    return
13  endif
14  if has('x11')
15    if empty($DISPLAY)
16      throw 'Skipped: $DISPLAY is not set'
17    endif
18    try
19      call remote_send('xxx', '')
20    catch
21      if v:exception =~ 'E240:'
22	throw 'Skipped: no connection to the X server'
23      endif
24      " ignore other errors
25    endtry
26  endif
27
28  let name = 'XVIMTEST'
29  let cmd .= ' --servername ' . name
30  let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'})
31  call WaitForAssert({-> assert_equal("run", job_status(job))})
32
33  " Takes a short while for the server to be active.
34  " When using valgrind it takes much longer.
35  call WaitForAssert({-> assert_match(name, serverlist())})
36
37  call remote_foreground(name)
38
39  call remote_send(name, ":let testvar = 'yes'\<CR>")
40  call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"')
41  call assert_equal('yes', remote_expr(name, "testvar", "", 2))
42
43  if has('unix') && has('gui') && !has('gui_running')
44    " Running in a terminal and the GUI is available: Tell the server to open
45    " 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.
48    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.
55    sleep 100m
56    call WaitForAssert({-> assert_true(remote_expr(name, "v:version", "", 1) != "")})
57
58    call remote_send(name, ":let testvar = 'maybe'\<CR>")
59    call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))})
60  endif
61
62  call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241')
63
64  " Expression evaluated locally.
65  if v:servername == ''
66    call remote_startserver('MYSELF')
67    " May get MYSELF1 when running the test again.
68    call assert_match('MYSELF', v:servername)
69  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')
74  call assert_equal('got it', remote_read(g:myserverid, 2))
75
76  call remote_send(name, ":call server2client(expand('<client>'), 'another')\<CR>", 'g:myserverid')
77  let peek_result = 'nothing'
78  let r = remote_peek(g:myserverid, 'peek_result')
79  " unpredictable whether the result is already available.
80  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')
89  call assert_equal('another', g:peek_result)
90  call assert_equal('another', remote_read(g:myserverid, 2))
91
92  call remote_send(name, ":qa!\<CR>")
93  try
94    call WaitForAssert({-> assert_equal("dead", job_status(job))})
95  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
101endfunc
102
103" Uncomment this line to get a debugging log
104" call ch_logfile('channellog', 'w')
105