robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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