server_requests_spec.lua 13 KB

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