thread_spec.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local assert_alive = n.assert_alive
  5. local clear = n.clear
  6. local feed = n.feed
  7. local eq = t.eq
  8. local exec_lua = n.exec_lua
  9. local next_msg = n.next_msg
  10. local NIL = vim.NIL
  11. local pcall_err = t.pcall_err
  12. describe('thread', function()
  13. local screen
  14. before_each(function()
  15. clear()
  16. screen = Screen.new(50, 10)
  17. end)
  18. it('entry func is executed in protected mode', function()
  19. exec_lua [[
  20. local thread = vim.uv.new_thread(function()
  21. error('Error in thread entry func')
  22. end)
  23. vim.uv.thread_join(thread)
  24. ]]
  25. screen:expect([[
  26. |
  27. {1:~ }|*5
  28. {3: }|
  29. {9:Error in luv thread:} |
  30. {9:[string "<nvim>"]:2: Error in thread entry func} |
  31. {6:Press ENTER or type command to continue}^ |
  32. ]])
  33. feed('<cr>')
  34. assert_alive()
  35. end)
  36. it('callback is executed in protected mode', function()
  37. exec_lua [[
  38. local thread = vim.uv.new_thread(function()
  39. local timer = vim.uv.new_timer()
  40. local function ontimeout()
  41. timer:stop()
  42. timer:close()
  43. error('Error in thread callback')
  44. end
  45. timer:start(10, 0, ontimeout)
  46. vim.uv.run()
  47. end)
  48. vim.uv.thread_join(thread)
  49. ]]
  50. screen:expect([[
  51. |
  52. {1:~ }|*5
  53. {3: }|
  54. {9:Error in luv callback, thread:} |
  55. {9:[string "<nvim>"]:6: Error in thread callback} |
  56. {6:Press ENTER or type command to continue}^ |
  57. ]])
  58. feed('<cr>')
  59. assert_alive()
  60. end)
  61. describe('print', function()
  62. it('works', function()
  63. exec_lua [[
  64. local thread = vim.uv.new_thread(function()
  65. print('print in thread')
  66. end)
  67. vim.uv.thread_join(thread)
  68. ]]
  69. screen:expect([[
  70. ^ |
  71. {1:~ }|*8
  72. print in thread |
  73. ]])
  74. end)
  75. it('vim.inspect', function()
  76. exec_lua [[
  77. local thread = vim.uv.new_thread(function()
  78. print(vim.inspect({1,2}))
  79. end)
  80. vim.uv.thread_join(thread)
  81. ]]
  82. screen:expect([[
  83. ^ |
  84. {1:~ }|*8
  85. { 1, 2 } |
  86. ]])
  87. end)
  88. end)
  89. describe('vim.*', function()
  90. before_each(function()
  91. clear()
  92. exec_lua [[
  93. Thread_Test = {}
  94. Thread_Test.entry_func = function(async, entry_str, args)
  95. local decoded_args = vim.mpack.decode(args)
  96. assert(loadstring(entry_str))(async, decoded_args)
  97. end
  98. function Thread_Test:do_test()
  99. local async
  100. local on_async = self.on_async
  101. async = vim.uv.new_async(function(ret)
  102. on_async(ret)
  103. async:close()
  104. end)
  105. local thread =
  106. vim.uv.new_thread(self.entry_func, async, self.entry_str, self.args)
  107. vim.uv.thread_join(thread)
  108. end
  109. Thread_Test.new = function(entry, on_async, ...)
  110. self = {}
  111. setmetatable(self, {__index = Thread_Test})
  112. self.args = vim.mpack.encode({...})
  113. self.entry_str = string.dump(entry)
  114. self.on_async = on_async
  115. return self
  116. end
  117. ]]
  118. end)
  119. it('is_thread', function()
  120. exec_lua [[
  121. local entry = function(async)
  122. async:send(vim.is_thread())
  123. end
  124. local on_async = function(ret)
  125. vim.rpcnotify(1, 'result', ret)
  126. end
  127. local thread_test = Thread_Test.new(entry, on_async)
  128. thread_test:do_test()
  129. ]]
  130. eq({ 'notification', 'result', { true } }, next_msg())
  131. end)
  132. it('uv', function()
  133. exec_lua [[
  134. local entry = function(async)
  135. async:send(vim.uv.version())
  136. end
  137. local on_async = function(ret)
  138. vim.rpcnotify(1, ret)
  139. end
  140. local thread_test = Thread_Test.new(entry, on_async)
  141. thread_test:do_test()
  142. ]]
  143. local msg = next_msg()
  144. eq('notification', msg[1])
  145. assert(tonumber(msg[2]) >= 72961)
  146. end)
  147. it('mpack', function()
  148. exec_lua [[
  149. local entry = function(async)
  150. async:send(vim.mpack.encode({33, vim.NIL, 'text'}))
  151. end
  152. local on_async = function(ret)
  153. vim.rpcnotify(1, 'result', vim.mpack.decode(ret))
  154. end
  155. local thread_test = Thread_Test.new(entry, on_async)
  156. thread_test:do_test()
  157. ]]
  158. eq({ 'notification', 'result', { { 33, NIL, 'text' } } }, next_msg())
  159. end)
  160. it('json', function()
  161. exec_lua [[
  162. local entry = function(async)
  163. async:send(vim.json.encode({33, vim.NIL, 'text'}))
  164. end
  165. local on_async = function(ret)
  166. vim.rpcnotify(1, 'result', vim.json.decode(ret))
  167. end
  168. local thread_test = Thread_Test.new(entry, on_async)
  169. thread_test:do_test()
  170. ]]
  171. eq({ 'notification', 'result', { { 33, NIL, 'text' } } }, next_msg())
  172. end)
  173. it('diff', function()
  174. exec_lua [[
  175. local entry = function(async)
  176. async:send(vim.diff('Hello\n', 'Helli\n'))
  177. end
  178. local on_async = function(ret)
  179. vim.rpcnotify(1, 'result', ret)
  180. end
  181. local thread_test = Thread_Test.new(entry, on_async)
  182. thread_test:do_test()
  183. ]]
  184. eq({
  185. 'notification',
  186. 'result',
  187. {
  188. table.concat({
  189. '@@ -1 +1 @@',
  190. '-Hello',
  191. '+Helli',
  192. '',
  193. }, '\n'),
  194. },
  195. }, next_msg())
  196. end)
  197. end)
  198. end)
  199. describe('threadpool', function()
  200. before_each(clear)
  201. it('is_thread', function()
  202. eq(false, exec_lua [[return vim.is_thread()]])
  203. exec_lua [[
  204. local work_fn = function()
  205. return vim.is_thread()
  206. end
  207. local after_work_fn = function(ret)
  208. vim.rpcnotify(1, 'result', ret)
  209. end
  210. local work = vim.uv.new_work(work_fn, after_work_fn)
  211. work:queue()
  212. ]]
  213. eq({ 'notification', 'result', { true } }, next_msg())
  214. end)
  215. it('with invalid argument', function()
  216. local status = pcall_err(
  217. exec_lua,
  218. [[
  219. local work = vim.uv.new_thread(function() end, function() end)
  220. work:queue({})
  221. ]]
  222. )
  223. eq([[Error: thread arg not support type 'function' at 1]], status)
  224. end)
  225. it('with invalid return value', function()
  226. local screen = Screen.new(50, 10)
  227. exec_lua [[
  228. local work = vim.uv.new_work(function() return {} end, function() end)
  229. work:queue()
  230. ]]
  231. screen:expect([[
  232. |
  233. {1:~ }|*5
  234. {3: }|
  235. {9:Error in luv thread:} |
  236. {9:Error: thread arg not support type 'table' at 1} |
  237. {6:Press ENTER or type command to continue}^ |
  238. ]])
  239. end)
  240. describe('vim.*', function()
  241. before_each(function()
  242. clear()
  243. exec_lua [[
  244. Threadpool_Test = {}
  245. Threadpool_Test.work_fn = function(work_fn_str, args)
  246. local decoded_args = vim.mpack.decode(args)
  247. return assert(loadstring(work_fn_str))(decoded_args)
  248. end
  249. function Threadpool_Test:do_test()
  250. local work =
  251. vim.uv.new_work(self.work_fn, self.after_work)
  252. work:queue(self.work_fn_str, self.args)
  253. end
  254. Threadpool_Test.new = function(work_fn, after_work, ...)
  255. self = {}
  256. setmetatable(self, {__index = Threadpool_Test})
  257. self.args = vim.mpack.encode({...})
  258. self.work_fn_str = string.dump(work_fn)
  259. self.after_work = after_work
  260. return self
  261. end
  262. ]]
  263. end)
  264. it('uv', function()
  265. exec_lua [[
  266. local work_fn = function()
  267. return vim.uv.version()
  268. end
  269. local after_work_fn = function(ret)
  270. vim.rpcnotify(1, ret)
  271. end
  272. local threadpool_test = Threadpool_Test.new(work_fn, after_work_fn)
  273. threadpool_test:do_test()
  274. ]]
  275. local msg = next_msg()
  276. eq('notification', msg[1])
  277. assert(tonumber(msg[2]) >= 72961)
  278. end)
  279. it('mpack', function()
  280. exec_lua [[
  281. local work_fn = function()
  282. local var = vim.mpack.encode({33, vim.NIL, 'text'})
  283. return var
  284. end
  285. local after_work_fn = function(ret)
  286. vim.rpcnotify(1, 'result', vim.mpack.decode(ret))
  287. end
  288. local threadpool_test = Threadpool_Test.new(work_fn, after_work_fn)
  289. threadpool_test:do_test()
  290. ]]
  291. eq({ 'notification', 'result', { { 33, NIL, 'text' } } }, next_msg())
  292. end)
  293. it('json', function()
  294. exec_lua [[
  295. local work_fn = function()
  296. local var = vim.json.encode({33, vim.NIL, 'text'})
  297. return var
  298. end
  299. local after_work_fn = function(ret)
  300. vim.rpcnotify(1, 'result', vim.json.decode(ret))
  301. end
  302. local threadpool_test = Threadpool_Test.new(work_fn, after_work_fn)
  303. threadpool_test:do_test()
  304. ]]
  305. eq({ 'notification', 'result', { { 33, NIL, 'text' } } }, next_msg())
  306. end)
  307. it('work', function()
  308. exec_lua [[
  309. local work_fn = function()
  310. return vim.diff('Hello\n', 'Helli\n')
  311. end
  312. local after_work_fn = function(ret)
  313. vim.rpcnotify(1, 'result', ret)
  314. end
  315. local threadpool_test = Threadpool_Test.new(work_fn, after_work_fn)
  316. threadpool_test:do_test()
  317. ]]
  318. eq({
  319. 'notification',
  320. 'result',
  321. {
  322. table.concat({
  323. '@@ -1 +1 @@',
  324. '-Hello',
  325. '+Helli',
  326. '',
  327. }, '\n'),
  328. },
  329. }, next_msg())
  330. end)
  331. end)
  332. end)