server_requests_spec.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. -- Test server -> client RPC scenarios. Note: unlike `rpcnotify`, to evaluate
  2. -- `rpcrequest` calls we need the client event loop to be running.
  3. local t = require('test.testutil')
  4. local n = require('test.functional.testnvim')()
  5. local clear, eval = n.clear, n.eval
  6. local eq, neq, run, stop = t.eq, t.neq, n.run, n.stop
  7. local nvim_prog, command, fn = n.nvim_prog, n.command, n.fn
  8. local source, next_msg = n.source, n.next_msg
  9. local ok = t.ok
  10. local api = n.api
  11. local spawn, merge_args = n.spawn, n.merge_args
  12. local set_session = n.set_session
  13. local pcall_err = t.pcall_err
  14. local assert_alive = n.assert_alive
  15. describe('server -> client', function()
  16. local cid
  17. before_each(function()
  18. clear()
  19. cid = api.nvim_get_chan_info(0).id
  20. end)
  21. it('handles unexpected closed stream while preparing RPC response', function()
  22. source([[
  23. let g:_nvim_args = [v:progpath, '--embed', '--headless', '-n', '-u', 'NONE', '-i', 'NONE', ]
  24. let ch1 = jobstart(g:_nvim_args, {'rpc': v:true})
  25. let child1_ch = rpcrequest(ch1, "nvim_get_chan_info", 0).id
  26. call rpcnotify(ch1, 'nvim_eval', 'rpcrequest('.child1_ch.', "nvim_get_api_info")')
  27. let ch2 = jobstart(g:_nvim_args, {'rpc': v:true})
  28. let child2_ch = rpcrequest(ch2, "nvim_get_chan_info", 0).id
  29. call rpcnotify(ch2, 'nvim_eval', 'rpcrequest('.child2_ch.', "nvim_get_api_info")')
  30. call jobstop(ch1)
  31. ]])
  32. assert_alive()
  33. end)
  34. describe('simple call', function()
  35. it('works', function()
  36. local function on_setup()
  37. eq({ 4, 5, 6 }, eval('rpcrequest(' .. cid .. ', "scall", 1, 2, 3)'))
  38. stop()
  39. end
  40. local function on_request(method, args)
  41. eq('scall', method)
  42. eq({ 1, 2, 3 }, args)
  43. command('let g:result = [4, 5, 6]')
  44. return eval('g:result')
  45. end
  46. run(on_request, nil, on_setup)
  47. end)
  48. end)
  49. describe('empty string handling in arrays', function()
  50. -- Because the msgpack encoding for an empty string was interpreted as an
  51. -- error, msgpack arrays with an empty string looked like
  52. -- [..., '', 0, ..., 0] after the conversion, regardless of the array
  53. -- elements following the empty string.
  54. it('works', function()
  55. local function on_setup()
  56. eq({ 1, 2, '', 3, 'asdf' }, eval('rpcrequest(' .. cid .. ', "nstring")'))
  57. stop()
  58. end
  59. local function on_request()
  60. -- No need to evaluate the args, we are only interested in
  61. -- a response that contains an array with an empty string.
  62. return { 1, 2, '', 3, 'asdf' }
  63. end
  64. run(on_request, nil, on_setup)
  65. end)
  66. end)
  67. describe('recursive call', function()
  68. it('works', function()
  69. local function on_setup()
  70. api.nvim_set_var('result1', 0)
  71. api.nvim_set_var('result2', 0)
  72. api.nvim_set_var('result3', 0)
  73. api.nvim_set_var('result4', 0)
  74. command('let g:result1 = rpcrequest(' .. cid .. ', "rcall", 2)')
  75. eq(4, api.nvim_get_var('result1'))
  76. eq(8, api.nvim_get_var('result2'))
  77. eq(16, api.nvim_get_var('result3'))
  78. eq(32, api.nvim_get_var('result4'))
  79. stop()
  80. end
  81. local function on_request(method, args)
  82. eq('rcall', method)
  83. local _n = unpack(args) * 2
  84. if _n <= 16 then
  85. local cmd
  86. if _n == 4 then
  87. cmd = 'let g:result2 = rpcrequest(' .. cid .. ', "rcall", ' .. _n .. ')'
  88. elseif _n == 8 then
  89. cmd = 'let g:result3 = rpcrequest(' .. cid .. ', "rcall", ' .. _n .. ')'
  90. elseif _n == 16 then
  91. cmd = 'let g:result4 = rpcrequest(' .. cid .. ', "rcall", ' .. _n .. ')'
  92. end
  93. command(cmd)
  94. end
  95. return _n
  96. end
  97. run(on_request, nil, on_setup)
  98. end)
  99. end)
  100. describe('requests and notifications interleaved', function()
  101. it('does not delay notifications during pending request', function()
  102. local received = false
  103. local function on_setup()
  104. eq('retval', fn.rpcrequest(cid, 'doit'))
  105. stop()
  106. end
  107. local function on_request(method)
  108. if method == 'doit' then
  109. fn.rpcnotify(cid, 'headsup')
  110. eq(true, received)
  111. return 'retval'
  112. end
  113. end
  114. local function on_notification(method)
  115. if method == 'headsup' then
  116. received = true
  117. end
  118. end
  119. run(on_request, on_notification, on_setup)
  120. end)
  121. -- This tests the following scenario:
  122. --
  123. -- server->client [request ] (1)
  124. -- client->server [request ] (2) triggered by (1)
  125. -- server->client [notification] (3) triggered by (2)
  126. -- server->client [response ] (4) response to (2)
  127. -- client->server [request ] (4) triggered by (3)
  128. -- server->client [request ] (5) triggered by (4)
  129. -- client->server [response ] (6) response to (1)
  130. --
  131. -- If the above scenario ever happens, the client connection will be closed
  132. -- because (6) is returned after request (5) is sent, and nvim
  133. -- only deals with one server->client request at a time. (In other words,
  134. -- the client cannot send a response to a request that is not at the top
  135. -- of nvim's request stack).
  136. pending('will close connection if not properly synchronized', function()
  137. local function on_setup()
  138. eq('notified!', eval('rpcrequest(' .. cid .. ', "notify")'))
  139. end
  140. local function on_request(method)
  141. if method == 'notify' then
  142. eq(1, eval('rpcnotify(' .. cid .. ', "notification")'))
  143. return 'notified!'
  144. elseif method == 'nested' then
  145. -- do some busywork, so the first request will return
  146. -- before this one
  147. for _ = 1, 5 do
  148. assert_alive()
  149. end
  150. eq(1, eval('rpcnotify(' .. cid .. ', "nested_done")'))
  151. return 'done!'
  152. end
  153. end
  154. local function on_notification(method)
  155. if method == 'notification' then
  156. eq('done!', eval('rpcrequest(' .. cid .. ', "nested")'))
  157. elseif method == 'nested_done' then
  158. ok(false, 'never sent', 'sent')
  159. end
  160. end
  161. run(on_request, on_notification, on_setup)
  162. -- ignore disconnect failure, otherwise detected by after_each
  163. clear()
  164. end)
  165. end)
  166. describe('recursive (child) nvim client', function()
  167. before_each(function()
  168. command(
  169. "let vim = rpcstart('"
  170. .. nvim_prog
  171. .. "', ['-u', 'NONE', '-i', 'NONE', '--cmd', 'set noswapfile', '--embed', '--headless'])"
  172. )
  173. neq(0, eval('vim'))
  174. end)
  175. after_each(function()
  176. command('call rpcstop(vim)')
  177. end)
  178. it('can send/receive notifications and make requests', function()
  179. command("call rpcnotify(vim, 'vim_set_current_line', 'SOME TEXT')")
  180. -- Wait for the notification to complete.
  181. command("call rpcrequest(vim, 'vim_eval', '0')")
  182. eq('SOME TEXT', eval("rpcrequest(vim, 'vim_get_current_line')"))
  183. end)
  184. it('can communicate buffers, tabpages, and windows', function()
  185. eq({ 1 }, eval("rpcrequest(vim, 'nvim_list_tabpages')"))
  186. -- Window IDs start at 1000 (LOWEST_WIN_ID in window.h)
  187. eq({ 1000 }, eval("rpcrequest(vim, 'nvim_list_wins')"))
  188. local buf = eval("rpcrequest(vim, 'nvim_list_bufs')")[1]
  189. eq(1, buf)
  190. eval("rpcnotify(vim, 'buffer_set_line', " .. buf .. ", 0, 'SOME TEXT')")
  191. command("call rpcrequest(vim, 'vim_eval', '0')") -- wait
  192. eq('SOME TEXT', eval("rpcrequest(vim, 'buffer_get_line', " .. buf .. ', 0)'))
  193. -- Call get_lines(buf, range [0,0], strict_indexing)
  194. eq({ 'SOME TEXT' }, eval("rpcrequest(vim, 'buffer_get_lines', " .. buf .. ', 0, 1, 1)'))
  195. end)
  196. it('returns an error if the request failed', function()
  197. eq(
  198. "Vim:Error invoking 'does-not-exist' on channel 3:\nInvalid method: does-not-exist",
  199. pcall_err(eval, "rpcrequest(vim, 'does-not-exist')")
  200. )
  201. end)
  202. end)
  203. describe('jobstart()', function()
  204. local jobid
  205. before_each(function()
  206. local channel = api.nvim_get_chan_info(0).id
  207. api.nvim_set_var('channel', channel)
  208. source([[
  209. function! s:OnEvent(id, data, event)
  210. call rpcnotify(g:channel, a:event, 0, a:data)
  211. endfunction
  212. let g:job_opts = {
  213. \ 'on_stderr': function('s:OnEvent'),
  214. \ 'on_exit': function('s:OnEvent'),
  215. \ 'user': 0,
  216. \ 'rpc': v:true
  217. \ }
  218. ]])
  219. api.nvim_set_var('args', {
  220. nvim_prog,
  221. '-ll',
  222. 'test/functional/api/rpc_fixture.lua',
  223. package.path,
  224. package.cpath,
  225. })
  226. jobid = eval('jobstart(g:args, g:job_opts)')
  227. neq(0, jobid)
  228. end)
  229. after_each(function()
  230. pcall(fn.jobstop, jobid)
  231. end)
  232. if t.skip(t.is_os('win')) then
  233. return
  234. end
  235. it('rpc and text stderr can be combined', function()
  236. local status, rv = pcall(fn.rpcrequest, jobid, 'poll')
  237. if not status then
  238. error(string.format('missing nvim Lua module? (%s)', rv))
  239. end
  240. eq('ok', rv)
  241. fn.rpcnotify(jobid, 'ping')
  242. eq({ 'notification', 'pong', {} }, next_msg())
  243. eq('done!', fn.rpcrequest(jobid, 'write_stderr', 'fluff\n'))
  244. eq({ 'notification', 'stderr', { 0, { 'fluff', '' } } }, next_msg())
  245. pcall(fn.rpcrequest, jobid, 'exit')
  246. eq({ 'notification', 'stderr', { 0, { '' } } }, next_msg())
  247. eq({ 'notification', 'exit', { 0, 0 } }, next_msg())
  248. end)
  249. end)
  250. describe('connecting to another (peer) nvim', function()
  251. local nvim_argv = merge_args(n.nvim_argv, { '--headless' })
  252. local function connect_test(server, mode, address)
  253. local serverpid = fn.getpid()
  254. local client = spawn(nvim_argv, false, nil, true)
  255. set_session(client)
  256. local clientpid = fn.getpid()
  257. neq(serverpid, clientpid)
  258. local id = fn.sockconnect(mode, address, { rpc = true })
  259. ok(id > 0)
  260. fn.rpcrequest(id, 'nvim_set_current_line', 'hello')
  261. local client_id = fn.rpcrequest(id, 'nvim_get_chan_info', 0).id
  262. set_session(server)
  263. eq(serverpid, fn.getpid())
  264. eq('hello', api.nvim_get_current_line())
  265. -- method calls work both ways
  266. fn.rpcrequest(client_id, 'nvim_set_current_line', 'howdy!')
  267. eq(id, fn.rpcrequest(client_id, 'nvim_get_chan_info', 0).id)
  268. set_session(client)
  269. eq(clientpid, fn.getpid())
  270. eq('howdy!', api.nvim_get_current_line())
  271. server:close()
  272. client:close()
  273. end
  274. it('via named pipe', function()
  275. local server = spawn(nvim_argv)
  276. set_session(server)
  277. local address = fn.serverlist()[1]
  278. local first = string.sub(address, 1, 1)
  279. ok(first == '/' or first == '\\')
  280. connect_test(server, 'pipe', address)
  281. end)
  282. it('via ipv4 address', function()
  283. local server = spawn(nvim_argv)
  284. set_session(server)
  285. local status, address = pcall(fn.serverstart, '127.0.0.1:')
  286. if not status then
  287. pending('no ipv4 stack')
  288. end
  289. eq('127.0.0.1:', string.sub(address, 1, 10))
  290. connect_test(server, 'tcp', address)
  291. end)
  292. it('via ipv6 address', function()
  293. local server = spawn(nvim_argv)
  294. set_session(server)
  295. local status, address = pcall(fn.serverstart, '::1:')
  296. if not status then
  297. pending('no ipv6 stack')
  298. end
  299. eq('::1:', string.sub(address, 1, 4))
  300. connect_test(server, 'tcp', address)
  301. end)
  302. it('via hostname', function()
  303. local server = spawn(nvim_argv)
  304. set_session(server)
  305. local address = fn.serverstart('localhost:')
  306. eq('localhost:', string.sub(address, 1, 10))
  307. connect_test(server, 'tcp', address)
  308. end)
  309. it('does not crash on receiving UI events', function()
  310. local server = spawn(nvim_argv)
  311. set_session(server)
  312. local address = fn.serverlist()[1]
  313. local client = spawn(nvim_argv, false, nil, true)
  314. set_session(client)
  315. local id = fn.sockconnect('pipe', address, { rpc = true })
  316. fn.rpcrequest(id, 'nvim_ui_attach', 80, 24, {})
  317. assert_alive()
  318. server:close()
  319. client:close()
  320. end)
  321. it('via stdio, with many small flushes does not crash #23781', function()
  322. source([[
  323. let chan = jobstart([v:progpath, '--embed', '--headless', '-n', '-u', 'NONE', '-i', 'NONE'], { 'rpc':v:false })
  324. call chansend(chan, 0Z94)
  325. sleep 50m
  326. call chansend(chan, 0Z00)
  327. call chansend(chan, 0Z01)
  328. call chansend(chan, 0ZAC)
  329. call chansend(chan, 0Z6E76696D5F636F6D6D616E64)
  330. call chansend(chan, 0Z91)
  331. call chansend(chan, 0ZA5)
  332. call chansend(chan, 0Z71616C6C21)
  333. let g:statuses = jobwait([chan])
  334. ]])
  335. eq(eval('g:statuses'), { 0 })
  336. assert_alive()
  337. end)
  338. end)
  339. describe('connecting to its own pipe address', function()
  340. it('does not deadlock', function()
  341. local address = fn.serverlist()[1]
  342. local first = string.sub(address, 1, 1)
  343. ok(first == '/' or first == '\\')
  344. local serverpid = fn.getpid()
  345. local id = fn.sockconnect('pipe', address, { rpc = true })
  346. fn.rpcrequest(id, 'nvim_set_current_line', 'hello')
  347. eq('hello', api.nvim_get_current_line())
  348. eq(serverpid, fn.rpcrequest(id, 'nvim_eval', 'getpid()'))
  349. eq(id, fn.rpcrequest(id, 'nvim_get_chan_info', 0).id)
  350. end)
  351. end)
  352. end)