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