neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

server_notifications_spec.lua (3468B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 
      4 local eq, clear, eval, command, next_msg = t.eq, n.clear, n.eval, n.command, n.next_msg
      5 local api = n.api
      6 local exec_lua = n.exec_lua
      7 local retry = t.retry
      8 local assert_alive = n.assert_alive
      9 local check_close = n.check_close
     10 
     11 local testlog = 'Xtest-server-notify-log'
     12 
     13 describe('notify', function()
     14  local channel
     15 
     16  before_each(function()
     17    clear()
     18    channel = api.nvim_get_chan_info(0).id
     19  end)
     20 
     21  after_each(function()
     22    check_close()
     23    os.remove(testlog)
     24  end)
     25 
     26  describe('passing a valid channel id', function()
     27    it('sends the notification/args to the corresponding channel', function()
     28      eval('rpcnotify(' .. channel .. ', "test-event", 1, 2, 3)')
     29      eq({ 'notification', 'test-event', { 1, 2, 3 } }, next_msg())
     30      command('au FileType lua call rpcnotify(' .. channel .. ', "lua!")')
     31      command('set filetype=lua')
     32      eq({ 'notification', 'lua!', {} }, next_msg())
     33    end)
     34  end)
     35 
     36  describe('channel id 0', function()
     37    it('broadcasts the notification/args to all channels', function()
     38      eval('rpcnotify(0, "event1", 1, 2, 3)')
     39      eval('rpcnotify(0, "event2", 4, 5, 6)')
     40      eval('rpcnotify(0, "event2", 7, 8, 9)')
     41      eq({ 'notification', 'event1', { 1, 2, 3 } }, next_msg())
     42      eq({ 'notification', 'event2', { 4, 5, 6 } }, next_msg())
     43      eq({ 'notification', 'event2', { 7, 8, 9 } }, next_msg())
     44 
     45      eval('rpcnotify(0, "event2", 10, 11, 12)')
     46      eval('rpcnotify(0, "event1", 13, 14, 15)')
     47      eq({ 'notification', 'event2', { 10, 11, 12 } }, next_msg())
     48      eq({ 'notification', 'event1', { 13, 14, 15 } }, next_msg())
     49    end)
     50 
     51    it('does not crash for deeply nested variable', function()
     52      api.nvim_set_var('l', {})
     53      local nest_level = 1000
     54      command(('call map(range(%u), "extend(g:, {\'l\': [g:l]})")'):format(nest_level - 1))
     55      eval('rpcnotify(' .. channel .. ', "event", g:l)')
     56      local msg = next_msg()
     57      eq('notification', msg[1])
     58      eq('event', msg[2])
     59      local act_ret = msg[3]
     60      local act_nest_level = 0
     61      while act_ret do
     62        if type(act_ret) == 'table' then
     63          local cur_act_ret = nil
     64          for k, v in pairs(act_ret) do
     65            eq(1, k)
     66            cur_act_ret = v
     67          end
     68          if cur_act_ret then
     69            act_nest_level = act_nest_level + 1
     70          end
     71          act_ret = cur_act_ret
     72        else
     73          eq(nil, act_ret)
     74        end
     75      end
     76      eq(nest_level, act_nest_level)
     77    end)
     78  end)
     79 
     80  it('cancels stale events on channel close #13537', function()
     81    local catchan = eval("jobstart(['cat'], {'rpc': v:true})")
     82    local catpath = eval('exepath("cat")')
     83    api.nvim_set_var('somevar', 0)
     84    eq(
     85      { id = catchan, argv = { catpath }, stream = 'job', mode = 'rpc', client = {} },
     86      exec_lua(function()
     87        vim.rpcnotify(catchan, 'nvim_call_function', 'chanclose', { catchan, 'rpc' })
     88        vim.rpcnotify(catchan, 'nvim_set_var', 'somevar', 1) -- Should be cancelled.
     89        return vim.api.nvim_get_chan_info(catchan)
     90      end)
     91    )
     92    assert_alive()
     93    eq(
     94      { false, 'Invalid channel: ' .. catchan },
     95      exec_lua(function()
     96        return { pcall(vim.rpcrequest, catchan, 'nvim_eval', '1+1') }
     97      end, catchan)
     98    )
     99    retry(nil, 3000, function()
    100      eq({}, api.nvim_get_chan_info(catchan))
    101    end) -- cat be dead :(
    102    eq(0, api.nvim_get_var('somevar'))
    103  end)
    104 end)