2016-04-23 16:53:11 -07:00
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
2023-01-16 12:43:13 -07:00
|
|
|
local assert_log = helpers.assert_log
|
2024-01-12 06:11:28 -07:00
|
|
|
local eq, clear, eval, command, next_msg =
|
|
|
|
helpers.eq, helpers.clear, helpers.eval, helpers.command, helpers.next_msg
|
2016-04-18 05:55:51 -07:00
|
|
|
local meths = helpers.meths
|
2020-12-15 02:15:51 -07:00
|
|
|
local exec_lua = helpers.exec_lua
|
|
|
|
local retry = helpers.retry
|
2021-09-01 07:29:38 -07:00
|
|
|
local assert_alive = helpers.assert_alive
|
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-01-12 06:11:28 -07:00
|
|
|
channel = meths.nvim_get_api_info()[1]
|
2014-10-08 09:56:01 -07:00
|
|
|
end)
|
|
|
|
|
2023-01-16 12:43:13 -07:00
|
|
|
after_each(function()
|
|
|
|
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()
|
2024-01-02 18:09:18 -07:00
|
|
|
eval('rpcnotify(' .. channel .. ', "test-event", 1, 2, 3)')
|
|
|
|
eq({ 'notification', 'test-event', { 1, 2, 3 } }, next_msg())
|
|
|
|
command('au FileType lua call rpcnotify(' .. channel .. ', "lua!")')
|
2017-04-08 14:12:26 -07:00
|
|
|
command('set filetype=lua')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'lua!', {} }, next_msg())
|
2014-10-08 09:56:01 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('passing 0 as the channel id', function()
|
|
|
|
it('sends the notification/args to all subscribed channels', function()
|
2024-01-12 06:11:28 -07:00
|
|
|
meths.nvim_subscribe('event2')
|
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-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'event2', { 4, 5, 6 } }, next_msg())
|
|
|
|
eq({ 'notification', 'event2', { 7, 8, 9 } }, next_msg())
|
2024-01-12 06:11:28 -07:00
|
|
|
meths.nvim_unsubscribe('event2')
|
|
|
|
meths.nvim_subscribe('event1')
|
2014-10-08 09:56:01 -07:00
|
|
|
eval('rpcnotify(0, "event2", 10, 11, 12)')
|
|
|
|
eval('rpcnotify(0, "event1", 13, 14, 15)')
|
2024-01-02 18:09:18 -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 05:44:54 -07:00
|
|
|
meths.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))
|
2024-01-02 18:09:18 -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
|
|
|
|
|
|
|
it('unsubscribe non-existing event #8745', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
clear { env = {
|
|
|
|
NVIM_LOG_FILE = testlog,
|
|
|
|
} }
|
2024-01-12 06:11:28 -07:00
|
|
|
meths.nvim_subscribe('event1')
|
|
|
|
meths.nvim_unsubscribe('doesnotexist')
|
2023-01-16 16:12:59 -07:00
|
|
|
assert_log("tried to unsubscribe unknown event 'doesnotexist'", testlog, 10)
|
2024-01-12 06:11:28 -07:00
|
|
|
meths.nvim_unsubscribe('event1')
|
2021-09-01 07:29:38 -07:00
|
|
|
assert_alive()
|
2018-07-14 09:23:08 -07:00
|
|
|
end)
|
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")')
|
2024-01-02 18:09:18 -07:00
|
|
|
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'})
|
|
|
|
vim.rpcnotify(..., "nvim_subscribe", "daily_rant")
|
|
|
|
return vim.api.nvim_get_chan_info(...)
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
catchan
|
|
|
|
)
|
|
|
|
)
|
2021-09-01 07:29:38 -07:00
|
|
|
assert_alive()
|
2024-01-02 18:09:18 -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 05:44:54 -07:00
|
|
|
eq({}, meths.nvim_get_chan_info(catchan))
|
2024-01-02 18:09:18 -07:00
|
|
|
end) -- cat be dead :(
|
2020-12-15 02:15:51 -07:00
|
|
|
end)
|
2014-10-08 09:56:01 -07:00
|
|
|
end)
|