server_requests_spec.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 helpers = require('test.functional.helpers')(after_each)
  4. local clear, nvim, eval = helpers.clear, helpers.nvim, helpers.eval
  5. local eq, neq, run, stop = helpers.eq, helpers.neq, helpers.run, helpers.stop
  6. local nvim_prog, command, funcs = helpers.nvim_prog, helpers.command, helpers.funcs
  7. local source, next_msg = helpers.source, helpers.next_msg
  8. local ok = helpers.ok
  9. local meths = helpers.meths
  10. local spawn, merge_args = helpers.spawn, helpers.merge_args
  11. local set_session = helpers.set_session
  12. local pcall_err = helpers.pcall_err
  13. local assert_alive = helpers.assert_alive
  14. describe('server -> client', function()
  15. local cid
  16. before_each(function()
  17. clear()
  18. cid = nvim('get_api_info')[1]
  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_api_info")[0]
  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_api_info")[0]
  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. nvim('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. nvim('set_var', 'result1', 0)
  70. nvim('set_var', 'result2', 0)
  71. nvim('set_var', 'result3', 0)
  72. nvim('set_var', 'result4', 0)
  73. nvim('command', 'let g:result1 = rpcrequest('..cid..', "rcall", 2)')
  74. eq(4, nvim('get_var', 'result1'))
  75. eq(8, nvim('get_var', 'result2'))
  76. eq(16, nvim('get_var', 'result3'))
  77. eq(32, 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. nvim('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", funcs.rpcrequest(cid, "doit"))
  104. stop()
  105. end
  106. local function on_request(method)
  107. if method == "doit" then
  108. funcs.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("let vim = rpcstart('"..nvim_prog.."', ['-u', 'NONE', '-i', 'NONE', '--cmd', 'set noswapfile', '--embed', '--headless'])")
  168. neq(0, eval('vim'))
  169. end)
  170. after_each(function() command('call rpcstop(vim)') end)
  171. it('can send/receive notifications and make requests', function()
  172. nvim('command', "call rpcnotify(vim, 'vim_set_current_line', 'SOME TEXT')")
  173. -- Wait for the notification to complete.
  174. nvim('command', "call rpcrequest(vim, 'vim_eval', '0')")
  175. eq('SOME TEXT', eval("rpcrequest(vim, 'vim_get_current_line')"))
  176. end)
  177. it('can communicate buffers, tabpages, and windows', function()
  178. eq({1}, eval("rpcrequest(vim, 'nvim_list_tabpages')"))
  179. -- Window IDs start at 1000 (LOWEST_WIN_ID in vim.h)
  180. eq({1000}, eval("rpcrequest(vim, 'nvim_list_wins')"))
  181. local buf = eval("rpcrequest(vim, 'nvim_list_bufs')")[1]
  182. eq(1, buf)
  183. eval("rpcnotify(vim, 'buffer_set_line', "..buf..", 0, 'SOME TEXT')")
  184. nvim('command', "call rpcrequest(vim, 'vim_eval', '0')") -- wait
  185. eq('SOME TEXT', eval("rpcrequest(vim, 'buffer_get_line', "..buf..", 0)"))
  186. -- Call get_lines(buf, range [0,0], strict_indexing)
  187. eq({'SOME TEXT'}, eval("rpcrequest(vim, 'buffer_get_lines', "..buf..", 0, 1, 1)"))
  188. end)
  189. it('returns an error if the request failed', function()
  190. eq("Vim:Error invoking 'does-not-exist' on channel 3:\nInvalid method: does-not-exist",
  191. pcall_err(eval, "rpcrequest(vim, 'does-not-exist')"))
  192. end)
  193. end)
  194. describe('jobstart()', function()
  195. local jobid
  196. before_each(function()
  197. local channel = nvim('get_api_info')[1]
  198. nvim('set_var', 'channel', channel)
  199. source([[
  200. function! s:OnEvent(id, data, event)
  201. call rpcnotify(g:channel, a:event, 0, a:data)
  202. endfunction
  203. let g:job_opts = {
  204. \ 'on_stderr': function('s:OnEvent'),
  205. \ 'on_exit': function('s:OnEvent'),
  206. \ 'user': 0,
  207. \ 'rpc': v:true
  208. \ }
  209. ]])
  210. meths.set_var("args", {
  211. helpers.test_lua_prg,
  212. 'test/functional/api/rpc_fixture.lua',
  213. package.path,
  214. package.cpath,
  215. })
  216. jobid = eval("jobstart(g:args, g:job_opts)")
  217. neq(0, jobid)
  218. end)
  219. after_each(function()
  220. pcall(funcs.jobstop, jobid)
  221. end)
  222. if helpers.pending_win32(pending) then return end
  223. it('rpc and text stderr can be combined', function()
  224. local status, rv = pcall(funcs.rpcrequest, jobid, 'poll')
  225. if not status then
  226. error(string.format('missing nvim Lua module? (%s)', rv))
  227. end
  228. eq('ok', rv)
  229. funcs.rpcnotify(jobid, "ping")
  230. eq({'notification', 'pong', {}}, next_msg())
  231. eq("done!",funcs.rpcrequest(jobid, "write_stderr", "fluff\n"))
  232. eq({'notification', 'stderr', {0, {'fluff', ''}}}, next_msg())
  233. pcall(funcs.rpcrequest, jobid, "exit")
  234. eq({'notification', 'stderr', {0, {''}}}, next_msg())
  235. eq({'notification', 'exit', {0, 0}}, next_msg())
  236. end)
  237. end)
  238. describe('connecting to another (peer) nvim', function()
  239. local nvim_argv = merge_args(helpers.nvim_argv, {'--headless'})
  240. local function connect_test(server, mode, address)
  241. local serverpid = funcs.getpid()
  242. local client = spawn(nvim_argv, false, nil, true)
  243. set_session(client)
  244. local clientpid = funcs.getpid()
  245. neq(serverpid, clientpid)
  246. local id = funcs.sockconnect(mode, address, {rpc=true})
  247. ok(id > 0)
  248. funcs.rpcrequest(id, 'nvim_set_current_line', 'hello')
  249. local client_id = funcs.rpcrequest(id, 'nvim_get_api_info')[1]
  250. set_session(server)
  251. eq(serverpid, funcs.getpid())
  252. eq('hello', meths.get_current_line())
  253. -- method calls work both ways
  254. funcs.rpcrequest(client_id, 'nvim_set_current_line', 'howdy!')
  255. eq(id, funcs.rpcrequest(client_id, 'nvim_get_api_info')[1])
  256. set_session(client)
  257. eq(clientpid, funcs.getpid())
  258. eq('howdy!', meths.get_current_line())
  259. server:close()
  260. client:close()
  261. end
  262. it('via named pipe', function()
  263. local server = spawn(nvim_argv)
  264. set_session(server)
  265. local address = funcs.serverlist()[1]
  266. local first = string.sub(address,1,1)
  267. ok(first == '/' or first == '\\')
  268. connect_test(server, 'pipe', address)
  269. end)
  270. it('via ipv4 address', function()
  271. local server = spawn(nvim_argv)
  272. set_session(server)
  273. local status, address = pcall(funcs.serverstart, "127.0.0.1:")
  274. if not status then
  275. pending('no ipv4 stack')
  276. end
  277. eq('127.0.0.1:', string.sub(address,1,10))
  278. connect_test(server, 'tcp', address)
  279. end)
  280. it('via ipv6 address', function()
  281. local server = spawn(nvim_argv)
  282. set_session(server)
  283. local status, address = pcall(funcs.serverstart, '::1:')
  284. if not status then
  285. pending('no ipv6 stack')
  286. end
  287. eq('::1:', string.sub(address,1,4))
  288. connect_test(server, 'tcp', address)
  289. end)
  290. it('via hostname', function()
  291. local server = spawn(nvim_argv)
  292. set_session(server)
  293. local address = funcs.serverstart("localhost:")
  294. eq('localhost:', string.sub(address,1,10))
  295. connect_test(server, 'tcp', address)
  296. end)
  297. end)
  298. describe('connecting to its own pipe address', function()
  299. it('does not deadlock', function()
  300. local address = funcs.serverlist()[1]
  301. local first = string.sub(address,1,1)
  302. ok(first == '/' or first == '\\')
  303. local serverpid = funcs.getpid()
  304. local id = funcs.sockconnect('pipe', address, {rpc=true})
  305. funcs.rpcrequest(id, 'nvim_set_current_line', 'hello')
  306. eq('hello', meths.get_current_line())
  307. eq(serverpid, funcs.rpcrequest(id, "nvim_eval", "getpid()"))
  308. eq(id, funcs.rpcrequest(id, 'nvim_get_api_info')[1])
  309. end)
  310. end)
  311. end)