2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
|
|
|
local eq, clear, eval, command, next_msg = t.eq, n.clear, n.eval, n.command, n.next_msg
|
|
|
|
local api = n.api
|
|
|
|
local exec_lua = n.exec_lua
|
2024-04-08 02:03:20 -07:00
|
|
|
local retry = t.retry
|
2024-04-20 08:44:13 -07:00
|
|
|
local assert_alive = n.assert_alive
|
|
|
|
local check_close = n.check_close
|
2014-10-08 09:56:01 -07:00
|
|
|
|
2023-01-16 12:43:13 -07:00
|
|
|
local testlog = 'Xtest-server-notify-log'
|
|
|
|
|
2014-10-08 09:56:01 -07:00
|
|
|
describe('notify', function()
|
|
|
|
local channel
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
clear()
|
2024-02-12 06:50:39 -07:00
|
|
|
channel = api.nvim_get_chan_info(0).id
|
2014-10-08 09:56:01 -07:00
|
|
|
end)
|
|
|
|
|
2023-01-16 12:43:13 -07:00
|
|
|
after_each(function()
|
2024-04-05 20:18:43 -07:00
|
|
|
check_close()
|
2023-01-16 12:43:13 -07:00
|
|
|
os.remove(testlog)
|
|
|
|
end)
|
|
|
|
|
2014-10-08 09:56:01 -07:00
|
|
|
describe('passing a valid channel id', function()
|
|
|
|
it('sends the notification/args to the corresponding channel', function()
|
|
|
|
eval('rpcnotify(' .. channel .. ', "test-event", 1, 2, 3)')
|
2018-03-08 16:24:38 -07:00
|
|
|
eq({ 'notification', 'test-event', { 1, 2, 3 } }, next_msg())
|
2017-04-08 14:12:26 -07:00
|
|
|
command('au FileType lua call rpcnotify(' .. channel .. ', "lua!")')
|
|
|
|
command('set filetype=lua')
|
2018-03-08 16:24:38 -07:00
|
|
|
eq({ 'notification', 'lua!', {} }, next_msg())
|
2014-10-08 09:56:01 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2024-05-17 07:37:39 -07:00
|
|
|
describe('channel id 0', function()
|
|
|
|
it('broadcasts the notification/args to all channels', function()
|
2014-10-08 09:56:01 -07:00
|
|
|
eval('rpcnotify(0, "event1", 1, 2, 3)')
|
|
|
|
eval('rpcnotify(0, "event2", 4, 5, 6)')
|
|
|
|
eval('rpcnotify(0, "event2", 7, 8, 9)')
|
2024-05-17 07:37:39 -07:00
|
|
|
eq({ 'notification', 'event1', { 1, 2, 3 } }, next_msg())
|
2018-03-08 16:24:38 -07:00
|
|
|
eq({ 'notification', 'event2', { 4, 5, 6 } }, next_msg())
|
|
|
|
eq({ 'notification', 'event2', { 7, 8, 9 } }, next_msg())
|
2024-05-17 07:37:39 -07:00
|
|
|
|
2014-10-08 09:56:01 -07:00
|
|
|
eval('rpcnotify(0, "event2", 10, 11, 12)')
|
|
|
|
eval('rpcnotify(0, "event1", 13, 14, 15)')
|
2024-05-17 07:37:39 -07:00
|
|
|
eq({ 'notification', 'event2', { 10, 11, 12 } }, next_msg())
|
2018-03-08 16:24:38 -07:00
|
|
|
eq({ 'notification', 'event1', { 13, 14, 15 } }, next_msg())
|
2014-10-08 09:56:01 -07:00
|
|
|
end)
|
2016-04-18 05:55:51 -07:00
|
|
|
|
|
|
|
it('does not crash for deeply nested variable', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('l', {})
|
2016-04-24 10:51:23 -07:00
|
|
|
local nest_level = 1000
|
2024-01-12 06:11:28 -07:00
|
|
|
command(('call map(range(%u), "extend(g:, {\'l\': [g:l]})")'):format(nest_level - 1))
|
2016-04-18 05:55:51 -07:00
|
|
|
eval('rpcnotify(' .. channel .. ', "event", g:l)')
|
2018-03-08 16:24:38 -07:00
|
|
|
local msg = next_msg()
|
2016-06-11 11:59:01 -07:00
|
|
|
eq('notification', msg[1])
|
|
|
|
eq('event', msg[2])
|
|
|
|
local act_ret = msg[3]
|
|
|
|
local act_nest_level = 0
|
|
|
|
while act_ret do
|
|
|
|
if type(act_ret) == 'table' then
|
|
|
|
local cur_act_ret = nil
|
|
|
|
for k, v in pairs(act_ret) do
|
|
|
|
eq(1, k)
|
|
|
|
cur_act_ret = v
|
|
|
|
end
|
|
|
|
if cur_act_ret then
|
|
|
|
act_nest_level = act_nest_level + 1
|
|
|
|
end
|
|
|
|
act_ret = cur_act_ret
|
|
|
|
else
|
|
|
|
eq(nil, act_ret)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
eq(nest_level, act_nest_level)
|
2016-04-18 05:55:51 -07:00
|
|
|
end)
|
2014-10-08 09:56:01 -07:00
|
|
|
end)
|
2018-07-14 09:23:08 -07:00
|
|
|
|
2020-12-15 02:15:51 -07:00
|
|
|
it('cancels stale events on channel close', function()
|
|
|
|
local catchan = eval("jobstart(['cat'], {'rpc': v:true})")
|
2021-09-01 07:29:38 -07:00
|
|
|
local catpath = eval('exepath("cat")')
|
|
|
|
eq(
|
|
|
|
{ id = catchan, argv = { catpath }, stream = 'job', mode = 'rpc', client = {} },
|
|
|
|
exec_lua(
|
|
|
|
[[
|
2020-12-15 02:15:51 -07:00
|
|
|
vim.rpcnotify(..., "nvim_call_function", 'chanclose', {..., 'rpc'})
|
|
|
|
return vim.api.nvim_get_chan_info(...)
|
|
|
|
]],
|
|
|
|
catchan
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-09-01 07:29:38 -07:00
|
|
|
assert_alive()
|
2020-12-15 02:15:51 -07:00
|
|
|
eq(
|
|
|
|
{ false, 'Invalid channel: ' .. catchan },
|
|
|
|
exec_lua([[ return {pcall(vim.rpcrequest, ..., 'nvim_eval', '1+1')}]], catchan)
|
|
|
|
)
|
|
|
|
retry(nil, 3000, function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({}, api.nvim_get_chan_info(catchan))
|
2020-12-15 02:15:51 -07:00
|
|
|
end) -- cat be dead :(
|
|
|
|
end)
|
2014-10-08 09:56:01 -07:00
|
|
|
end)
|