wait_spec.lua 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local call = n.call
  4. local clear = n.clear
  5. local command = n.command
  6. local eval = n.eval
  7. local eq = t.eq
  8. local feed = n.feed
  9. local feed_command = n.feed_command
  10. local next_msg = n.next_msg
  11. local api = n.api
  12. local source = n.source
  13. local pcall_err = t.pcall_err
  14. before_each(function()
  15. clear()
  16. local channel = api.nvim_get_chan_info(0).id
  17. api.nvim_set_var('channel', channel)
  18. end)
  19. describe('wait()', function()
  20. it('waits and returns 0 when condition is satisfied', function()
  21. source([[
  22. let g:_awake = 0
  23. call timer_start(100, { -> nvim_command('let g:_awake = 1') })
  24. ]])
  25. eq(0, eval('g:_awake'))
  26. eq(0, eval('wait(1500, { -> g:_awake })'))
  27. eq(1, eval('g:_awake'))
  28. eq(0, eval('wait(0, 1)'))
  29. end)
  30. it('returns -1 on timeout', function()
  31. eq(-1, eval('wait(0, 0)'))
  32. eq(-1, eval('wait(50, 0)'))
  33. end)
  34. it('returns -2 when interrupted', function()
  35. feed_command(
  36. 'call rpcnotify(g:channel, "ready") | ' .. 'call rpcnotify(g:channel, "wait", wait(-1, 0))'
  37. )
  38. eq({ 'notification', 'ready', {} }, next_msg())
  39. feed('<c-c>')
  40. eq({ 'notification', 'wait', { -2 } }, next_msg())
  41. end)
  42. it('returns -3 on error', function()
  43. command('silent! let ret = wait(-1, "error")')
  44. eq(-3, eval('ret'))
  45. command('let ret = 0 | silent! let ret = wait(-1, { -> error })')
  46. eq(-3, eval('ret'))
  47. end)
  48. it('evaluates the condition on given interval', function()
  49. source([[
  50. function Count()
  51. let g:counter += 1
  52. return g:counter
  53. endfunction
  54. ]])
  55. -- XXX: flaky (#11137)
  56. t.retry(nil, nil, function()
  57. api.nvim_set_var('counter', 0)
  58. eq(-1, call('wait', 20, 'Count() >= 5', 99999))
  59. end)
  60. api.nvim_set_var('counter', 0)
  61. eq(0, call('wait', 10000, 'Count() >= 5', 5))
  62. eq(5, api.nvim_get_var('counter'))
  63. end)
  64. it('validates args', function()
  65. eq('Vim:E475: Invalid value for argument 1', pcall_err(call, 'wait', '', 1))
  66. eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, -1))
  67. eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, 0))
  68. eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, ''))
  69. end)
  70. end)