robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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