Files
neovim/test/functional/plugin/lsp/testutil.lua
tao 654303079b feat(lsp): skip invalid header lines #36402
Problem:
Some servers write log to stdout and there's no way to avoid it.
See https://github.com/neovim/neovim/pull/35743#pullrequestreview-3379705828

Solution:
We can extract `content-length` field byte by byte and skip invalid
lines via a simple state machine (name/colon/value/invalid), with minimal
performance impact.

I chose byte parsing here instead of pattern. Although it's a bit more complex,
it provides more stable performance and allows for more accurate error info when
needed.

Here is a bench result and script:

    parse header1 by pattern: 59.52377ms 45
    parse header1 by byte: 7.531128ms 45

    parse header2 by pattern: 26.06936ms 45
    parse header2 by byte: 5.235724ms 45

    parse header3 by pattern: 9.348495ms 45
    parse header3 by byte: 3.452389ms 45

    parse header4 by pattern: 9.73156ms 45
    parse header4 by byte: 3.638386ms 45

Script:

```lua
local strbuffer = require('string.buffer')

--- @param header string
local function get_content_length(header)
  for line in header:gmatch('(.-)\r?\n') do
    if line == '' then
      break
    end
    local key, value = line:match('^%s*(%S+)%s*:%s*(%d+)%s*$')
    if key and key:lower() == 'content-length' then
      return assert(tonumber(value))
    end
  end
  error('Content-Length not found in header: ' .. header)
end

--- @param header string
local function get_content_length_by_byte(header)
  local state = 'name'
  local i, len = 1, #header
  local j, name = 1, 'content-length'
  local buf = strbuffer.new()
  local digit = true
  while i <= len do
    local c = header:byte(i)
    if state == 'name' then
      if c >= 65 and c <= 90 then -- lower case
        c = c + 32
      end
      if (c == 32 or c == 9) and j == 1 then
        -- skip OWS for compatibility only
      elseif c == name:byte(j) then
        j = j + 1
      elseif c == 58 and j == 15 then
        state = 'colon'
      else
        state = 'invalid'
      end
    elseif state == 'colon' then
      if c ~= 32 and c ~= 9 then -- skip OWS normally
        state = 'value'
        i = i - 1
      end
    elseif state == 'value' then
      if c == 13 and header:byte(i + 1) == 10 then -- must end with \r\n
        local value = buf:get()
        return assert(digit and tonumber(value), 'value of Content-Length is not number: ' .. value)
      else
        buf:put(string.char(c))
      end
      if c < 48 and c ~= 32 and c ~= 9 or c > 57 then
        digit = false
      end
    elseif state == 'invalid' then
      if c == 10 then -- reset for next line
        state, j = 'name', 1
      end
    end
    i = i + 1
  end
  error('Content-Length not found in header: ' .. header)
end

--- @param fn fun(header: string): number
local function bench(label, header, fn, count)
  local start = vim.uv.hrtime()
  local value --- @type number
  for _ = 1, count do
    value = fn(header)
  end
  local elapsed = (vim.uv.hrtime() - start) / 1e6
  print(label .. ':', elapsed .. 'ms', value)
end

-- header starting with log lines
local header1 =
  'WARN: no common words file defined for Khmer - this language might not be correctly auto-detected\nWARN: no common words file defined for Japanese - this language might not be correctly auto-detected\nContent-Length: 45  \r\n\r\n'
-- header starting with content-type
local header2 = 'Content-Type: application/json-rpc; charset=utf-8\r\nContent-Length: 45  \r\n'
-- regular header
local header3 = '  Content-Length: 45\r\n'
-- regular header ending with content-type
local header4 = '  Content-Length: 45 \r\nContent-Type: application/json-rpc; charset=utf-8\r\n'

local count = 10000

collectgarbage('collect')
bench('parse header1 by pattern', header1, get_content_length, count)
collectgarbage('collect')
bench('parse header1 by byte', header1, get_content_length_by_byte, count)

collectgarbage('collect')
bench('parse header2 by pattern', header2, get_content_length, count)
collectgarbage('collect')
bench('parse header2 by byte', header2, get_content_length_by_byte, count)

collectgarbage('collect')
bench('parse header3 by pattern', header3, get_content_length, count)
collectgarbage('collect')
bench('parse header3 by byte', header3, get_content_length_by_byte, count)

collectgarbage('collect')
bench('parse header4 by pattern', header4, get_content_length, count)
collectgarbage('collect')
bench('parse header4 by byte', header4, get_content_length_by_byte, count)
```

Also, I removed an outdated test
accd392f4d/test/functional/plugin/lsp_spec.lua (L1950)
and tweaked the boilerplate in two other tests for reusability while keeping the final assertions the same.
accd392f4d/test/functional/plugin/lsp_spec.lua (L5704)
accd392f4d/test/functional/plugin/lsp_spec.lua (L5721)
2025-11-16 17:23:52 -08:00

260 lines
6.5 KiB
Lua

local n = require('test.functional.testnvim')()
local clear = n.clear
local exec_lua = n.exec_lua
local run = n.run
local stop = n.stop
local api = n.api
local NIL = vim.NIL
local M = {}
function M.clear_notrace()
-- problem: here be dragons
-- solution: don't look too closely for dragons
clear {
env = {
NVIM_LUA_NOTRACK = '1',
NVIM_APPNAME = 'nvim_lsp_test',
VIMRUNTIME = os.getenv 'VIMRUNTIME',
},
}
end
M.create_tcp_echo_server = function()
--- Create a TCP server that echos the first message it receives.
--- @param host string
--- @return integer
function _G._create_tcp_server(host)
local uv = vim.uv
local server = assert(uv.new_tcp())
local on_read = require('vim.lsp.rpc').create_read_loop(
function(body)
vim.rpcnotify(1, 'body', body)
end,
nil,
function(err, code)
vim.rpcnotify(1, 'error', err, code)
end
)
server:bind(host, 0)
server:listen(127, function(e)
assert(not e, e)
local socket = assert(uv.new_tcp())
server:accept(socket)
socket:read_start(function(err, chunk)
on_read(err, chunk)
socket:shutdown()
socket:close()
server:shutdown()
server:close()
end)
end)
return server:getsockname().port
end
function _G._send_msg_to_server(msg)
local port = _G._create_tcp_server('127.0.0.1')
local client = assert(vim.uv.new_tcp())
client:connect('127.0.0.1', port, function()
client:write(msg, function()
client:shutdown()
client:close()
end)
end)
end
end
M.create_server_definition = function()
function _G._create_server(opts)
opts = opts or {}
local server = {}
server.messages = {}
function server.cmd(dispatchers, _config)
local closing = false
local handlers = opts.handlers or {}
local srv = {}
function srv.request(method, params, callback)
table.insert(server.messages, {
method = method,
params = params,
})
local handler = handlers[method]
if handler then
handler(method, params, callback)
elseif method == 'initialize' then
callback(nil, {
capabilities = opts.capabilities or {},
})
elseif method == 'shutdown' then
callback(nil, nil)
end
local request_id = #server.messages
return true, request_id
end
function srv.notify(method, params)
table.insert(server.messages, {
method = method,
params = params,
})
if method == 'exit' then
dispatchers.on_exit(0, 15)
end
end
function srv.is_closing()
return closing
end
function srv.terminate()
closing = true
end
return srv
end
return server
end
end
-- Fake LSP server.
M.fake_lsp_code = 'test/functional/fixtures/fake-lsp-server.lua'
M.fake_lsp_logfile = 'Xtest-fake-lsp.log'
local function fake_lsp_server_setup(test_name, timeout_ms, options, settings)
exec_lua(function(fake_lsp_code, fake_lsp_logfile, timeout)
options = options or {}
settings = settings or {}
_G.lsp = require('vim.lsp')
_G.TEST_RPC_CLIENT_ID = _G.lsp.start_client {
cmd_env = {
NVIM_LOG_FILE = fake_lsp_logfile,
NVIM_LUA_NOTRACK = '1',
NVIM_APPNAME = 'nvim_lsp_test',
},
cmd = {
vim.v.progpath,
'-l',
fake_lsp_code,
test_name,
tostring(timeout),
},
handlers = setmetatable({}, {
__index = function(_t, _method)
return function(...)
return vim.rpcrequest(1, 'handler', ...)
end
end,
}),
workspace_folders = {
{
uri = 'file://' .. vim.uv.cwd(),
name = 'test_folder',
},
},
before_init = function(_params, _config)
vim.schedule(function()
vim.rpcrequest(1, 'setup')
end)
end,
on_init = function(client, result)
_G.TEST_RPC_CLIENT = client
vim.rpcrequest(1, 'init', result)
end,
flags = {
allow_incremental_sync = options.allow_incremental_sync or false,
debounce_text_changes = options.debounce_text_changes or 0,
},
settings = settings,
on_exit = function(...)
vim.rpcnotify(1, 'exit', ...)
end,
}
end, M.fake_lsp_code, M.fake_lsp_logfile, timeout_ms or 1e3)
end
--- @class test.lsp.Config
--- @field test_name string
--- @field timeout_ms? integer
--- @field options? table
--- @field settings? table
---
--- @field on_setup? fun()
--- @field on_init? fun(client: vim.lsp.Client, ...)
--- @field on_handler? fun(...)
--- @field on_exit? fun(code: integer, signal: integer)
--- @param config test.lsp.Config
function M.test_rpc_server(config)
if config.test_name then
M.clear_notrace()
fake_lsp_server_setup(
config.test_name,
config.timeout_ms or 1e3,
config.options,
config.settings
)
end
local client = setmetatable({}, {
__index = function(t, name)
-- Workaround for not being able to yield() inside __index for Lua 5.1 :(
-- Otherwise I would just return the value here.
return function(arg1, ...)
local ismethod = arg1 == t
return exec_lua(function(...)
local client = _G.TEST_RPC_CLIENT
if type(client[name]) == 'function' then
return client[name](ismethod and client or arg1, ...)
end
return client[name]
end, ...)
end
end,
})
--- @type integer, integer
local code, signal
local busy = 0
local exited = false
local function on_request(method, args)
busy = busy + 1
if method == 'setup' and config.on_setup then
config.on_setup()
end
if method == 'init' and config.on_init then
config.on_init(client, unpack(args))
end
if method == 'handler' and config.on_handler then
config.on_handler(unpack(args))
end
busy = busy - 1
if busy == 0 and exited then
stop()
end
return NIL
end
local function on_notify(method, args)
if method == 'exit' then
code, signal = unpack(args)
exited = true
if busy == 0 then
stop()
end
end
return NIL
end
-- TODO specify timeout?
-- run(on_request, on_notify, nil, 1000)
run(on_request, on_notify, nil)
if config.on_exit then
config.on_exit(code, signal)
end
stop()
if config.test_name then
api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
end
end
return M