robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1-- Test server -> client RPC scenarios. Note: unlike `rpcnotify`, to evaluate 2-- `rpcrequest` calls we need the client event loop to be running. 3local t =require ( 'test.testutil' ) 4local n =require ( 'test.functional.testnvim' )() 5 6local describe , it , before_each , after_each , pending , finally = 7t . describe , t . it , t . before_each , t . after_each , t . pending , t . finally 8local clear , eval =n . clear , n . eval 9local eq , neq , run , stop =t . eq , t . neq , n . run , n . stop 10local nvim_prog , command , fn =n . nvim_prog , n . command , n . fn 11local source , next_msg =n . source , n . next_msg 12local ok =t . ok 13local api =n . api 14local new_session , set_session =n . new_session , n . set_session 15local pcall_err =t . pcall_err 16local assert_alive =n . assert_alive 17 18describe ( 'server -> client' , function () 19local cid 20 21before_each ( function () 22clear () 23cid =api . nvim_get_chan_info ( 0 ). id 24end ) 25 26it ( 'handles unexpected closed stream while preparing RPC response' , function () 27source ( [[ 28let g:_nvim_args = [v:progpath, '--embed', '--headless', '-n', '-u', 'NONE', '-i', 'NONE', ] 29let ch1 = jobstart(g:_nvim_args, {'rpc': v:true}) 30let child1_ch = rpcrequest(ch1, "nvim_get_chan_info", 0).id 31call rpcnotify(ch1, 'nvim_eval', 'rpcrequest('.child1_ch.', "nvim_get_api_info")') 32 33let ch2 = jobstart(g:_nvim_args, {'rpc': v:true}) 34let child2_ch = rpcrequest(ch2, "nvim_get_chan_info", 0).id 35call rpcnotify(ch2, 'nvim_eval', 'rpcrequest('.child2_ch.', "nvim_get_api_info")') 36 37call jobstop(ch1) 38]] ) 39assert_alive () 40end ) 41 42describe ( 'simple call' , function () 43it ( 'works' , function () 44local function on_setup () 45eq ( { 4 , 5 , 6 } , eval ( 'rpcrequest(' .. cid .. ', "scall", 1, 2, 3)' )) 46stop () 47end 48 49local function on_request ( method , args ) 50eq ( 'scall' , method ) 51eq ( { 1 , 2 , 3 } , args ) 52command ( 'let g:result = [4, 5, 6]' ) 53return eval ( 'g:result' ) 54end 55run ( on_request , nil , on_setup ) 56end ) 57end ) 58 59describe ( 'empty string handling in arrays' , function () 60-- Because the msgpack encoding for an empty string was interpreted as an 61-- error, msgpack arrays with an empty string looked like 62-- [..., '', 0, ..., 0] after the conversion, regardless of the array 63-- elements following the empty string. 64it ( 'works' , function () 65local function on_setup () 66eq ( { 1 , 2 , '' , 3 , 'asdf' } , eval ( 'rpcrequest(' .. cid .. ', "nstring")' )) 67stop () 68end 69 70local function on_request () 71-- No need to evaluate the args, we are only interested in 72-- a response that contains an array with an empty string. 73return { 1 , 2 , '' , 3 , 'asdf' } 74end 75run ( on_request , nil , on_setup ) 76end ) 77end ) 78 79describe ( 'recursive call' , function () 80it ( 'works' , function () 81local function on_setup () 82api . nvim_set_var ( 'result1' , 0 ) 83api . nvim_set_var ( 'result2' , 0 ) 84api . nvim_set_var ( 'result3' , 0 ) 85api . nvim_set_var ( 'result4' , 0 ) 86command ( 'let g:result1 = rpcrequest(' .. cid .. ', "rcall", 2)' ) 87eq ( 4 , api . nvim_get_var ( 'result1' )) 88eq ( 8 , api . nvim_get_var ( 'result2' )) 89eq ( 16 , api . nvim_get_var ( 'result3' )) 90eq ( 32 , api . nvim_get_var ( 'result4' )) 91stop () 92end 93 94local function on_request ( method , args ) 95eq ( 'rcall' , method ) 96local _n =unpack ( args ) * 2 97if _n <= 16 then 98local cmd 99if _n == 4 then 100cmd ='let g:result2 = rpcrequest(' .. cid .. ', "rcall", ' .. _n .. ')' 101elseif _n == 8 then 102cmd ='let g:result3 = rpcrequest(' .. cid .. ', "rcall", ' .. _n .. ')' 103elseif _n == 16 then 104cmd ='let g:result4 = rpcrequest(' .. cid .. ', "rcall", ' .. _n .. ')' 105end 106command ( cmd ) 107end 108return _n 109end 110run ( on_request , nil , on_setup ) 111end ) 112end ) 113 114describe ( 'requests and notifications interleaved' , function () 115it ( 'does not delay notifications during pending request' , function () 116local received =false 117local function on_setup () 118eq ( 'retval' , fn . rpcrequest ( cid , 'doit' )) 119stop () 120end 121local function on_request ( method ) 122if method == 'doit' then 123fn . rpcnotify ( cid , 'headsup' ) 124eq ( true , received ) 125return 'retval' 126end 127end 128local function on_notification ( method ) 129if method == 'headsup' then 130received =true 131end 132end 133run ( on_request , on_notification , on_setup ) 134end ) 135 136-- This tests the following scenario: 137-- 138-- server->client [request ] (1) 139-- client->server [request ] (2) triggered by (1) 140-- server->client [notification] (3) triggered by (2) 141-- server->client [response ] (4) response to (2) 142-- client->server [request ] (4) triggered by (3) 143-- server->client [request ] (5) triggered by (4) 144-- client->server [response ] (6) response to (1) 145-- 146-- If the above scenario ever happens, the client connection will be closed 147-- because (6) is returned after request (5) is sent, and nvim 148-- only deals with one server->client request at a time. (In other words, 149-- the client cannot send a response to a request that is not at the top 150-- of nvim's request stack). 151pending ( 'will close connection if not properly synchronized' , function () 152local function on_setup () 153eq ( 'notified!' , eval ( 'rpcrequest(' .. cid .. ', "notify")' )) 154end 155 156local function on_request ( method ) 157if method == 'notify' then 158eq ( 1 , eval ( 'rpcnotify(' .. cid .. ', "notification")' )) 159return 'notified!' 160elseif method == 'nested' then 161-- do some busywork, so the first request will return 162-- before this one 163for _ =1 , 5 do 164assert_alive () 165end 166eq ( 1 , eval ( 'rpcnotify(' .. cid .. ', "nested_done")' )) 167return 'done!' 168end 169end 170 171local function on_notification ( method ) 172if method == 'notification' then 173eq ( 'done!' , eval ( 'rpcrequest(' .. cid .. ', "nested")' )) 174elseif method == 'nested_done' then 175ok ( false , 'never sent' , 'sent' ) 176end 177end 178 179run ( on_request , on_notification , on_setup ) 180-- ignore disconnect failure, otherwise detected by after_each 181clear () 182end ) 183end ) 184 185describe ( 'recursive (child) nvim client' , function () 186before_each ( function () 187command ( 188"let vim = rpcstart('" 189.. nvim_prog 190.. "', ['-u', 'NONE', '-i', 'NONE', '--cmd', 'set noswapfile', '--embed', '--headless'])" 191) 192neq ( 0 , eval ( 'vim' )) 193end ) 194 195after_each ( function () 196command ( 'call rpcstop(vim)' ) 197end ) 198 199it ( 'can send/receive notifications and make requests' , function () 200command ( "call rpcnotify(vim, 'vim_set_current_line', 'SOME TEXT')" ) 201 202-- Wait for the notification to complete. 203command ( "call rpcrequest(vim, 'vim_eval', '0')" ) 204 205eq ( 'SOME TEXT' , eval ( "rpcrequest(vim, 'vim_get_current_line')" )) 206end ) 207 208it ( 'can communicate buffers, tabpages, and windows' , function () 209eq ( { 1 } , eval ( "rpcrequest(vim, 'nvim_list_tabpages')" )) 210-- Window IDs start at 1000 (LOWEST_WIN_ID in window.h) 211eq ( { 1000 } , eval ( "rpcrequest(vim, 'nvim_list_wins')" )) 212 213local buf =eval ( "rpcrequest(vim, 'nvim_list_bufs')" )[ 1 ] 214eq ( 1 , buf ) 215 216eval ( "rpcnotify(vim, 'buffer_set_line', " .. buf .. ", 0, 'SOME TEXT')" ) 217command ( "call rpcrequest(vim, 'vim_eval', '0')" ) -- wait 218 219eq ( 'SOME TEXT' , eval ( "rpcrequest(vim, 'buffer_get_line', " .. buf .. ', 0)' )) 220 221-- Call get_lines(buf, range [0,0], strict_indexing) 222eq ( { 'SOME TEXT' } , eval ( "rpcrequest(vim, 'buffer_get_lines', " .. buf .. ', 0, 1, 1)' )) 223end ) 224 225it ( 'returns an error if the request failed' , function () 226eq ( 227"Vim:Invoking 'does-not-exist' on channel 3:\nInvalid method: does-not-exist" , 228pcall_err ( eval , "rpcrequest(vim, 'does-not-exist')" ) 229) 230end ) 231end ) 232 233describe ( 'jobstart()' , function () 234local jobid 235before_each ( function () 236local channel =api . nvim_get_chan_info ( 0 ). id 237api . nvim_set_var ( 'channel' , channel ) 238source ( [[ 239function! s:OnEvent(id, data, event) 240call rpcnotify(g:channel, a:event, 0, a:data) 241endfunction 242let g:job_opts = { 243\ 'on_stderr': function('s:OnEvent'), 244\ 'on_exit': function('s:OnEvent'), 245\ 'user': 0, 246\ 'rpc': v:true 247\ } 248]] ) 249api . nvim_set_var ( 'args' , { 250nvim_prog , 251'-l' , 252'test/functional/api/rpc_fixture.lua' , 253package . path , 254package . cpath , 255} ) 256jobid =eval ( 'jobstart(g:args, g:job_opts)' ) 257neq ( 0 , jobid ) 258end ) 259 260after_each ( function () 261pcall ( fn . jobstop , jobid ) 262end ) 263 264if t . skip ( t . is_os ( 'win' )) then 265return 266end 267 268it ( 'rpc and text stderr can be combined' , function () 269local status , rv =pcall ( fn . rpcrequest , jobid , 'poll' ) 270if not status then 271error ( string . format ( 'missing nvim Lua module? (%s)' , rv )) 272end 273eq ( 'ok' , rv ) 274fn . rpcnotify ( jobid , 'ping' ) 275eq ( { 'notification' , 'pong' , {} } , next_msg ()) 276eq ( 'done!' , fn . rpcrequest ( jobid , 'write_stderr' , 'fluff\n' )) 277eq ( { 'notification' , 'stderr' , { 0 , { 'fluff' , '' } } } , next_msg ()) 278pcall ( fn . rpcrequest , jobid , 'exit' ) 279eq ( { 'notification' , 'stderr' , { 0 , { '' } } } , next_msg ()) 280eq ( { 'notification' , 'exit' , { 0 , 0 } } , next_msg ()) 281end ) 282end ) 283 284describe ( 'connecting to another (peer) nvim' , function () 285local function connect_test ( server , mode , address ) 286local serverpid =fn . getpid () 287local client =n . new_session ( true ) 288set_session ( client ) 289 290local clientpid =fn . getpid () 291neq ( serverpid , clientpid ) 292local id =fn . sockconnect ( mode , address , { rpc =true } ) 293ok ( id > 0 ) 294 295fn . rpcrequest ( id , 'nvim_set_current_line' , 'hello' ) 296local client_id =fn . rpcrequest ( id , 'nvim_get_chan_info' , 0 ). id 297 298set_session ( server ) 299eq ( serverpid , fn . getpid ()) 300eq ( 'hello' , api . nvim_get_current_line ()) 301 302-- Method calls work both ways. 303fn . rpcrequest ( client_id , 'nvim_set_current_line' , 'howdy!' ) 304eq ( id , fn . rpcrequest ( client_id , 'nvim_get_chan_info' , 0 ). id ) 305 306set_session ( client ) 307eq ( clientpid , fn . getpid ()) 308eq ( 'howdy!' , api . nvim_get_current_line ()) 309 310-- Sending notification and then closing channel immediately still works. 311-- Use a fast API here, as a deferred API call may be aborted by EOF. #13537 312n . exec_lua ( function () 313vim . rpcnotify ( id , 'nvim_input' , 'ccbye!<Esc>' ) 314vim . fn . chanclose ( id ) 315end ) 316 317set_session ( server ) 318eq ( serverpid , fn . getpid ()) 319-- Wait for the notification to be processed. 320t . retry ( nil , 1000 , function () 321eq ( 'bye!' , api . nvim_get_current_line ()) 322end ) 323 324server : close () 325client : close () 326end 327 328it ( 'via named pipe' , function () 329local server =n . new_session ( false ) 330set_session ( server ) 331local address =fn . serverlist ()[ 1 ] 332local first =string . sub ( address , 1 , 1 ) 333ok ( first == '/' ) 334connect_test ( server , 'pipe' , address ) 335end ) 336 337it ( 'via named pipe containing backslashes #39382' , function () 338t . skip ( not t . is_os ( 'win' ), 'N/A for non-Windows' ) 339local address =[[\\.\pipe\Xtest]] 340local server =new_session ( false , { 341args_rm ={ '--listen' } , 342args ={ '--listen' , address } , 343} ) 344set_session ( server ) 345eq ( vim . fs . normalize ( address ), api . nvim_get_vvar ( 'servername' )) 346connect_test ( server , 'pipe' , address ) 347end ) 348 349it ( 'via ipv4 address' , function () 350local server =n . new_session ( false ) 351set_session ( server ) 352local status , address =pcall ( fn . serverstart , '127.0.0.1:' ) 353if not status then 354pending ( 'no ipv4 stack' ) 355end 356eq ( '127.0.0.1:' , string . sub ( address , 1 , 10 )) 357connect_test ( server , 'tcp' , address ) 358end ) 359 360it ( 'via ipv6 address' , function () 361local server =n . new_session ( false ) 362set_session ( server ) 363local status , address =pcall ( fn . serverstart , '::1:' ) 364if not status then 365pending ( 'no ipv6 stack' ) 366end 367eq ( '::1:' , string . sub ( address , 1 , 4 )) 368connect_test ( server , 'tcp' , address ) 369end ) 370 371it ( 'via hostname' , function () 372local server =n . new_session ( false ) 373set_session ( server ) 374local address =fn . serverstart ( 'localhost:' ) 375eq ( 'localhost:' , string . sub ( address , 1 , 10 )) 376connect_test ( server , 'tcp' , address ) 377end ) 378 379local function start_server_and_client () 380local server =n . new_session ( false ) 381set_session ( server ) 382local address =fn . serverlist ()[ 1 ] 383local client =n . new_session ( true ) 384set_session ( client ) 385 386local id =fn . sockconnect ( 'pipe' , address , { rpc =true } ) 387 388finally ( function () 389server : close () 390client : close () 391end ) 392 393return id 394end 395 396it ( 'does not crash on receiving UI events' , function () 397local id =start_server_and_client () 398fn . rpcrequest ( id , 'nvim_ui_attach' , 80 , 24 , {} ) 399assert_alive () 400end ) 401 402it ( 'does not leak memory with channel closed before response' , function () 403local id =start_server_and_client () 404eq ( 405( 'ch %d was closed by the peer' ): format ( id ), 406pcall_err ( n . exec_lua , function () 407vim . rpcrequest ( id , 'nvim_command' , 'qall!' ) 408end ) 409) 410eq ( {} , api . nvim_get_chan_info ( id )) -- Channel is closed. 411end ) 412 413it ( 'response works with channel closed just after response #24214' , function () 414local id =start_server_and_client () 415eq ( 416'RESPONSE' , 417n . exec_lua ( function () 418local prepare =assert ( vim . uv . new_prepare ()) 419-- Block the event loop after writing the request but before polling for I/O 420-- so that response and EOF arrive at the same uv_run() call. 421prepare : start ( function () 422vim . uv . sleep ( 50 ) 423prepare : close () 424end ) 425return vim . rpcrequest ( 426id , 427'nvim_exec_lua' , 428[[vim.schedule(function() vim.cmd('qall!') end); return 'RESPONSE']] , 429{} 430) 431end ) 432) 433t . retry ( nil , nil , function () 434eq ( {} , api . nvim_get_chan_info ( id )) -- Channel is closed. 435end ) 436end ) 437 438it ( 'via stdio, with many small flushes does not crash #23781' , function () 439source ( [[ 440let chan = jobstart([v:progpath, '--embed', '--headless', '-n', '-u', 'NONE', '-i', 'NONE'], { 'rpc':v:false }) 441call chansend(chan, 0Z94) 442sleep 50m 443call chansend(chan, 0Z00) 444call chansend(chan, 0Z01) 445call chansend(chan, 0ZAC) 446call chansend(chan, 0Z6E76696D5F636F6D6D616E64) 447call chansend(chan, 0Z91) 448call chansend(chan, 0ZA5) 449call chansend(chan, 0Z71616C6C21) 450let g:statuses = jobwait([chan]) 451]] ) 452eq ( { 0 } , eval ( 'g:statuses' )) 453assert_alive () 454end ) 455end ) 456 457describe ( 'connecting to its own pipe address' , function () 458it ( 'does not deadlock' , function () 459local address =fn . serverlist ()[ 1 ] 460local first =string . sub ( address , 1 , 1 ) 461ok ( first == '/' or first == '\\' ) 462local serverpid =fn . getpid () 463 464local id =fn . sockconnect ( 'pipe' , address , { rpc =true } ) 465 466fn . rpcrequest ( id , 'nvim_set_current_line' , 'hello' ) 467eq ( 'hello' , api . nvim_get_current_line ()) 468eq ( serverpid , fn . rpcrequest ( id , 'nvim_eval' , 'getpid()' )) 469 470eq ( id , fn . rpcrequest ( id , 'nvim_get_chan_info' , 0 ). id ) 471end ) 472end ) 473end )