blob: d97f5ea20e0d71a5fa0c87866422ccf59cd4da1d [file] [log] [blame]
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01001" Tests for the +clientserver feature.
2
3if !has('job') || !has('clientserver')
4 finish
5endif
6
7source shared.vim
8
Bram Moolenaar42205552017-03-18 19:42:22 +01009let s:where = 0
10func Abort(id)
11 call assert_report('Test timed out at ' . s:where)
12 call FinishTesting()
13endfunc
14
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010015func Test_client_server()
16 let cmd = GetVimCommand()
17 if cmd == ''
18 return
19 endif
Bram Moolenaar42205552017-03-18 19:42:22 +010020
21 " Some of these commands may hang when failing.
22 call timer_start(10000, 'Abort')
23
24 let s:where = 1
25 let name = 'XVIMTEST'
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010026 let cmd .= ' --servername ' . name
27 let g:job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'})
28 call WaitFor('job_status(g:job) == "run"')
29 if job_status(g:job) != 'run'
Bram Moolenaar42205552017-03-18 19:42:22 +010030 call assert_report('Cannot run the Vim server')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010031 return
32 endif
Bram Moolenaar42205552017-03-18 19:42:22 +010033 let s:where = 2
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010034
35 " Takes a short while for the server to be active.
36 call WaitFor('serverlist() =~ "' . name . '"')
37 call assert_match(name, serverlist())
Bram Moolenaar42205552017-03-18 19:42:22 +010038 let s:where = 3
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010039
40 call remote_foreground(name)
Bram Moolenaar42205552017-03-18 19:42:22 +010041 let s:where = 4
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010042
43 call remote_send(name, ":let testvar = 'yes'\<CR>")
Bram Moolenaar42205552017-03-18 19:42:22 +010044 let s:where = 5
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010045 call WaitFor('remote_expr("' . name . '", "testvar") == "yes"')
Bram Moolenaar42205552017-03-18 19:42:22 +010046 let s:where = 6
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010047 call assert_equal('yes', remote_expr(name, "testvar"))
Bram Moolenaar42205552017-03-18 19:42:22 +010048 let s:where = 7
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010049
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010050 if has('unix') && has('gui') && !has('gui_running')
51 " Running in a terminal and the GUI is avaiable: Tell the server to open
52 " the GUI and check that the remote command still works.
53 " Need to wait for the GUI to start up, otherwise the send hangs in trying
54 " to send to the terminal window.
55 call remote_send(name, ":gui -f\<CR>")
Bram Moolenaar42205552017-03-18 19:42:22 +010056 let s:where = 8
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010057 sleep 500m
58 call remote_send(name, ":let testvar = 'maybe'\<CR>")
Bram Moolenaar42205552017-03-18 19:42:22 +010059 let s:where = 9
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010060 call WaitFor('remote_expr("' . name . '", "testvar") == "maybe"')
Bram Moolenaar42205552017-03-18 19:42:22 +010061 let s:where = 10
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010062 call assert_equal('maybe', remote_expr(name, "testvar"))
Bram Moolenaar42205552017-03-18 19:42:22 +010063 let s:where = 11
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010064 endif
65
66 call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241')
Bram Moolenaar42205552017-03-18 19:42:22 +010067 let s:where = 12
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010068
69 " Expression evaluated locally.
70 if v:servername == ''
71 call remote_startserver('MYSELF')
Bram Moolenaar42205552017-03-18 19:42:22 +010072 let s:where = 13
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010073 call assert_equal('MYSELF', v:servername)
74 endif
75 let g:testvar = 'myself'
76 call assert_equal('myself', remote_expr(v:servername, 'testvar'))
Bram Moolenaar42205552017-03-18 19:42:22 +010077 let s:where = 14
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010078
79 call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
Bram Moolenaar42205552017-03-18 19:42:22 +010080 let s:where = 15
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010081 call assert_equal('got it', remote_read(g:myserverid))
Bram Moolenaar42205552017-03-18 19:42:22 +010082 let s:where = 16
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010083
Bram Moolenaar6caf6062017-03-18 20:45:05 +010084 call remote_send(name, ":call server2client(expand('<client>'), 'another')\<CR>", 'g:myserverid')
85 let s:where = 151
86 let peek_result = 'nothing'
87 let r = remote_peek(g:myserverid, 'peek_result')
88 let s:where = 161
89 " unpredictable whether the result is already avaialble.
90 if r > 0
91 call assert_equal('another', peek_result)
92 elseif r == 0
93 call assert_equal('nothing', peek_result)
94 else
95 call assert_report('remote_peek() failed')
96 endif
97 let g:peek_result = 'empty'
98 call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0')
99 let s:where = 171
100 call assert_equal('another', g:peek_result)
101 let s:where = 181
102 call assert_equal('another', remote_read(g:myserverid))
103 let s:where = 191
104
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100105 call remote_send(name, ":qa!\<CR>")
Bram Moolenaar42205552017-03-18 19:42:22 +0100106 let s:where = 17
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100107 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar42205552017-03-18 19:42:22 +0100108 let s:where = 18
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100109 if job_status(g:job) != 'dead'
Bram Moolenaar42205552017-03-18 19:42:22 +0100110 call assert_report('Server did not exit')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +0100111 call job_stop(g:job, 'kill')
112 endif
113endfunc
114
115" Uncomment this line to get a debugging log
116" call ch_logfile('channellog', 'w')