comparison src/testdir/test_channel.vim @ 17831:4ab97fdf7ff7 v8.1.1912

patch 8.1.1912: more functions can be used as methods Commit: https://github.com/vim/vim/commit/570497ac409ad448574bb6210cb9c6e573483759 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Aug 22 22:55:13 2019 +0200 patch 8.1.1912: more functions can be used as methods Problem: More functions can be used as methods. Solution: Make channel and job functions usable as a method.
author Bram Moolenaar <Bram@vim.org>
date Thu, 22 Aug 2019 23:00:03 +0200
parents 0da9bc55c31a
children 068337e86133
comparison
equal deleted inserted replaced
17830:e8805fbb60a3 17831:4ab97fdf7ff7
60 endif 60 endif
61 61
62 " check that getjob without a job is handled correctly 62 " check that getjob without a job is handled correctly
63 call assert_equal('no process', string(ch_getjob(handle))) 63 call assert_equal('no process', string(ch_getjob(handle)))
64 64
65 let dict = ch_info(handle) 65 let dict = handle->ch_info()
66 call assert_true(dict.id != 0) 66 call assert_true(dict.id != 0)
67 call assert_equal('open', dict.status) 67 call assert_equal('open', dict.status)
68 call assert_equal(a:port, string(dict.port)) 68 call assert_equal(a:port, string(dict.port))
69 call assert_equal('open', dict.sock_status) 69 call assert_equal('open', dict.sock_status)
70 call assert_equal('socket', dict.sock_io) 70 call assert_equal('socket', dict.sock_io)
146 146
147 " Collect garbage, tests that our handle isn't collected. 147 " Collect garbage, tests that our handle isn't collected.
148 call test_garbagecollect_now() 148 call test_garbagecollect_now()
149 149
150 " check setting options (without testing the effect) 150 " check setting options (without testing the effect)
151 call ch_setoptions(handle, {'callback': 's:NotUsed'}) 151 eval handle->ch_setoptions({'callback': 's:NotUsed'})
152 call ch_setoptions(handle, {'timeout': 1111}) 152 call ch_setoptions(handle, {'timeout': 1111})
153 call ch_setoptions(handle, {'mode': 'json'}) 153 call ch_setoptions(handle, {'mode': 'json'})
154 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475") 154 call assert_fails("call ch_setoptions(handle, {'waittime': 111})", "E475")
155 call ch_setoptions(handle, {'callback': ''}) 155 call ch_setoptions(handle, {'callback': ''})
156 call ch_setoptions(handle, {'drop': 'never'}) 156 call ch_setoptions(handle, {'drop': 'never'})
225 225
226 " Test that we can open two channels. 226 " Test that we can open two channels.
227 func Ch_two_channels(port) 227 func Ch_two_channels(port)
228 let handle = ch_open('localhost:' . a:port, s:chopt) 228 let handle = ch_open('localhost:' . a:port, s:chopt)
229 call assert_equal(v:t_channel, type(handle)) 229 call assert_equal(v:t_channel, type(handle))
230 if ch_status(handle) == "fail" 230 if handle->ch_status() == "fail"
231 call assert_report("Can't open channel") 231 call assert_report("Can't open channel")
232 return 232 return
233 endif 233 endif
234 234
235 call assert_equal('got it', ch_evalexpr(handle, 'hello!')) 235 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
247 247
248 call ch_close(newhandle) 248 call ch_close(newhandle)
249 endfunc 249 endfunc
250 250
251 func Test_two_channels() 251 func Test_two_channels()
252 call ch_log('Test_two_channels()') 252 eval 'Test_two_channels()'->ch_log()
253 call s:run_server('Ch_two_channels') 253 call s:run_server('Ch_two_channels')
254 endfunc 254 endfunc
255 255
256 " Test that a server crash is handled gracefully. 256 " Test that a server crash is handled gracefully.
257 func Ch_server_crash(port) 257 func Ch_server_crash(port)
319 unlet g:Ch_zero_reply 319 unlet g:Ch_zero_reply
320 let g:Ch_zero_reply = a:msg 320 let g:Ch_zero_reply = a:msg
321 endfunc 321 endfunc
322 322
323 func Ch_channel_zero(port) 323 func Ch_channel_zero(port)
324 let handle = ch_open('localhost:' . a:port, s:chopt) 324 let handle = ('localhost:' .. a:port)->ch_open(s:chopt)
325 if ch_status(handle) == "fail" 325 if ch_status(handle) == "fail"
326 call assert_report("Can't open channel") 326 call assert_report("Can't open channel")
327 return 327 return
328 endif 328 endif
329 329
476 call ch_sendraw(job, "echo something\n") 476 call ch_sendraw(job, "echo something\n")
477 let msg = ch_readraw(job) 477 let msg = ch_readraw(job)
478 call assert_equal("something\n", substitute(msg, "\r", "", 'g')) 478 call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
479 479
480 call ch_sendraw(job, "double this\n") 480 call ch_sendraw(job, "double this\n")
481 let g:handle = job_getchannel(job) 481 let g:handle = job->job_getchannel()
482 call WaitFor('ch_canread(g:handle)') 482 call WaitFor('g:handle->ch_canread()')
483 unlet g:handle 483 unlet g:handle
484 let msg = ch_readraw(job) 484 let msg = ch_readraw(job)
485 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g')) 485 call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
486 486
487 let g:Ch_reply = "" 487 let g:Ch_reply = ""
488 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'}) 488 call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
489 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))}) 489 call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
490 490
491 let reply = ch_evalraw(job, "quit\n", {'timeout': 100}) 491 let reply = job->ch_evalraw("quit\n", {'timeout': 100})
492 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g')) 492 call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
493 finally 493 finally
494 call job_stop(job) 494 call job_stop(job)
495 endtry 495 endtry
496 496
497 let g:Ch_job = job 497 let g:Ch_job = job
498 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))}) 498 call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
499 let info = job_info(job) 499 let info = job->job_info()
500 call assert_equal("dead", info.status) 500 call assert_equal("dead", info.status)
501 call assert_equal("term", info.stoponexit) 501 call assert_equal("term", info.stoponexit)
502 call assert_equal(2, len(info.cmd)) 502 call assert_equal(2, len(info.cmd))
503 call assert_equal("test_channel_pipe.py", info.cmd[1]) 503 call assert_equal("test_channel_pipe.py", info.cmd[1])
504 504
532 endfor 532 endfor
533 call assert_equal(len(cmd), len(blob)) 533 call assert_equal(len(cmd), len(blob))
534 call ch_sendraw(job, blob) 534 call ch_sendraw(job, blob)
535 535
536 " Read a blob with the reply. 536 " Read a blob with the reply.
537 let msg = ch_readblob(job) 537 let msg = job->ch_readblob()
538 let expected = 'something' 538 let expected = 'something'
539 for i in range(0, len(expected) - 1) 539 for i in range(0, len(expected) - 1)
540 call assert_equal(char2nr(expected[i]), msg[i]) 540 call assert_equal(char2nr(expected[i]), msg[i])
541 endfor 541 endfor
542 542
556 let job = job_start([s:python, "test_channel_pipe.py"]) 556 let job = job_start([s:python, "test_channel_pipe.py"])
557 call assert_equal("run", job_status(job)) 557 call assert_equal("run", job_status(job))
558 try 558 try
559 let handle = job_getchannel(job) 559 let handle = job_getchannel(job)
560 call ch_sendraw(handle, "echo something\n") 560 call ch_sendraw(handle, "echo something\n")
561 call assert_equal("something", ch_readraw(handle)) 561 call assert_equal("something", handle->ch_readraw())
562 562
563 call ch_sendraw(handle, "echoerr wrong\n") 563 call ch_sendraw(handle, "echoerr wrong\n")
564 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'})) 564 call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
565 565
566 call ch_sendraw(handle, "double this\n") 566 call ch_sendraw(handle, "double this\n")
567 call assert_equal("this", ch_readraw(handle)) 567 call assert_equal("this", ch_readraw(handle))
568 call assert_equal("AND this", ch_readraw(handle)) 568 call assert_equal("AND this", ch_readraw(handle))
569 569
570 call ch_sendraw(handle, "split this line\n") 570 call ch_sendraw(handle, "split this line\n")
571 call assert_equal("this linethis linethis line", ch_read(handle)) 571 call assert_equal("this linethis linethis line", handle->ch_read())
572 572
573 let reply = ch_evalraw(handle, "quit\n") 573 let reply = ch_evalraw(handle, "quit\n")
574 call assert_equal("Goodbye!", reply) 574 call assert_equal("Goodbye!", reply)
575 finally 575 finally
576 call job_stop(job) 576 call job_stop(job)
871 let job = job_start('sort', options) 871 let job = job_start('sort', options)
872 872
873 if !a:use_buffer 873 if !a:use_buffer
874 call assert_equal("run", job_status(job)) 874 call assert_equal("run", job_status(job))
875 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n") 875 call ch_sendraw(job, "ccc\naaa\nddd\nbbb\neee\n")
876 call ch_close_in(job) 876 eval job->ch_close_in()
877 endif 877 endif
878 878
879 call WaitForAssert({-> assert_equal("dead", job_status(job))}) 879 call WaitForAssert({-> assert_equal("dead", job_status(job))})
880 880
881 sp sortout 881 sp sortout
915 call assert_equal("run", job_status(job)) 915 call assert_equal("run", job_status(job))
916 try 916 try
917 let handle = job_getchannel(job) 917 let handle = job_getchannel(job)
918 call ch_sendraw(handle, "echo line one\n") 918 call ch_sendraw(handle, "echo line one\n")
919 call ch_sendraw(handle, "echo line two\n") 919 call ch_sendraw(handle, "echo line two\n")
920 exe ch_getbufnr(handle, "out") . 'sbuf' 920 exe handle->ch_getbufnr("out") .. 'sbuf'
921 call WaitFor('line("$") >= 3') 921 call WaitFor('line("$") >= 3')
922 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$')) 922 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
923 bwipe! 923 bwipe!
924 finally 924 finally
925 call job_stop(job) 925 call job_stop(job)
1247 endfunc 1247 endfunc
1248 1248
1249 func Test_close_and_exit_cb() 1249 func Test_close_and_exit_cb()
1250 let g:retdict = {'ret': {}} 1250 let g:retdict = {'ret': {}}
1251 func g:retdict.close_cb(ch) dict 1251 func g:retdict.close_cb(ch) dict
1252 let self.ret['close_cb'] = job_status(ch_getjob(a:ch)) 1252 let self.ret['close_cb'] = a:ch->ch_getjob()->job_status()
1253 endfunc 1253 endfunc
1254 func g:retdict.exit_cb(job, status) dict 1254 func g:retdict.exit_cb(job, status) dict
1255 let self.ret['exit_cb'] = job_status(a:job) 1255 let self.ret['exit_cb'] = job_status(a:job)
1256 endfunc 1256 endfunc
1257 1257
1304 endfunc 1304 endfunc
1305 1305
1306 " Test that "unlet handle" in a handler doesn't crash Vim. 1306 " Test that "unlet handle" in a handler doesn't crash Vim.
1307 func Ch_unlet_handle(port) 1307 func Ch_unlet_handle(port)
1308 let s:channelfd = ch_open('localhost:' . a:port, s:chopt) 1308 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
1309 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')}) 1309 eval s:channelfd->ch_sendexpr("test", {'callback': function('s:UnletHandler')})
1310 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)}) 1310 call WaitForAssert({-> assert_equal('what?', g:Ch_unletResponse)})
1311 endfunc 1311 endfunc
1312 1312
1313 func Test_unlet_handle() 1313 func Test_unlet_handle()
1314 call ch_log('Test_unlet_handle()') 1314 call ch_log('Test_unlet_handle()')
1318 """""""""" 1318 """"""""""
1319 1319
1320 let g:Ch_unletResponse = '' 1320 let g:Ch_unletResponse = ''
1321 func Ch_CloseHandler(handle, msg) 1321 func Ch_CloseHandler(handle, msg)
1322 let g:Ch_unletResponse = a:msg 1322 let g:Ch_unletResponse = a:msg
1323 call ch_close(s:channelfd) 1323 eval s:channelfd->ch_close()
1324 endfunc 1324 endfunc
1325 1325
1326 " Test that "unlet handle" in a handler doesn't crash Vim. 1326 " Test that "unlet handle" in a handler doesn't crash Vim.
1327 func Ch_close_handle(port) 1327 func Ch_close_handle(port)
1328 let s:channelfd = ch_open('localhost:' . a:port, s:chopt) 1328 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
1353 unlet s:chopt.waittime 1353 unlet s:chopt.waittime
1354 if ch_status(channel) == "fail" 1354 if ch_status(channel) == "fail"
1355 call assert_report("Can't open channel") 1355 call assert_report("Can't open channel")
1356 return 1356 return
1357 endif 1357 endif
1358 call assert_equal('got it', ch_evalexpr(channel, 'hello!')) 1358 call assert_equal('got it', channel->ch_evalexpr('hello!'))
1359 call ch_close(channel) 1359 call ch_close(channel)
1360 endfunc 1360 endfunc
1361 1361
1362 func Test_open_delay() 1362 func Test_open_delay()
1363 call ch_log('Test_open_delay()') 1363 call ch_log('Test_open_delay()')
1394 function MyExitCb(job, status) 1394 function MyExitCb(job, status)
1395 let g:Ch_job_exit_ret = 'done' 1395 let g:Ch_job_exit_ret = 'done'
1396 endfunc 1396 endfunc
1397 1397
1398 function Ch_test_exit_callback(port) 1398 function Ch_test_exit_callback(port)
1399 call job_setoptions(g:currentJob, {'exit_cb': 'MyExitCb'}) 1399 eval g:currentJob->job_setoptions({'exit_cb': 'MyExitCb'})
1400 let g:Ch_exit_job = g:currentJob 1400 let g:Ch_exit_job = g:currentJob
1401 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb']) 1401 call assert_equal('MyExitCb', job_info(g:currentJob)['exit_cb'])
1402 endfunc 1402 endfunc
1403 1403
1404 func Test_exit_callback() 1404 func Test_exit_callback()
1426 call Resume() 1426 call Resume()
1427 endfunction 1427 endfunction
1428 1428
1429 func Test_exit_callback_interval() 1429 func Test_exit_callback_interval()
1430 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0} 1430 let g:exit_cb_val = {'start': reltime(), 'end': 0, 'process': 0}
1431 let job = job_start([s:python, '-c', 'import time;time.sleep(0.5)'], {'exit_cb': 'MyExitTimeCb'}) 1431 let job = [s:python, '-c', 'import time;time.sleep(0.5)']->job_start({'exit_cb': 'MyExitTimeCb'})
1432 let g:exit_cb_val.process = job_info(job).process 1432 let g:exit_cb_val.process = job_info(job).process
1433 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0') 1433 call WaitFor('type(g:exit_cb_val.end) != v:t_number || g:exit_cb_val.end != 0')
1434 let elapsed = reltimefloat(g:exit_cb_val.end) 1434 let elapsed = reltimefloat(g:exit_cb_val.end)
1435 call assert_true(elapsed > 0.5) 1435 call assert_true(elapsed > 0.5)
1436 call assert_true(elapsed < 1.0) 1436 call assert_true(elapsed < 1.0)
1505 endfunc 1505 endfunc
1506 1506
1507 func Test_job_stop_immediately() 1507 func Test_job_stop_immediately()
1508 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)']) 1508 let g:job = job_start([s:python, '-c', 'import time;time.sleep(10)'])
1509 try 1509 try
1510 call job_stop(g:job) 1510 eval g:job->job_stop()
1511 call WaitForAssert({-> assert_equal('dead', job_status(g:job))}) 1511 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
1512 finally 1512 finally
1513 call job_stop(g:job, 'kill') 1513 call job_stop(g:job, 'kill')
1514 unlet g:job 1514 unlet g:job
1515 endtry 1515 endtry
1819 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1, 1819 \ {'mode': 'raw', 'drop': 'never', 'noblock': 1,
1820 \ 'callback': {ch, msg -> execute('let g:out .= msg')}}) 1820 \ 'callback': {ch, msg -> execute('let g:out .= msg')}})
1821 1821
1822 let outlen = 79999 1822 let outlen = 79999
1823 let want = repeat('X', outlen) . "\n" 1823 let want = repeat('X', outlen) . "\n"
1824 call ch_sendraw(job, want) 1824 eval job->ch_sendraw(want)
1825 call WaitFor({-> len(g:out) >= outlen}, 10000) 1825 call WaitFor({-> len(g:out) >= outlen}, 10000)
1826 call WaitForAssert({-> assert_equal("dead", job_status(job))}) 1826 call WaitForAssert({-> assert_equal("dead", job_status(job))})
1827 call assert_equal(want, substitute(g:out, '\r', '', 'g')) 1827 call assert_equal(want, substitute(g:out, '\r', '', 'g'))
1828 finally 1828 finally
1829 call job_stop(job) 1829 call job_stop(job)
1917 call delete('Xtesterr') 1917 call delete('Xtesterr')
1918 endfunc 1918 endfunc
1919 1919
1920 " Do this last, it stops any channel log. 1920 " Do this last, it stops any channel log.
1921 func Test_zz_nl_err_to_out_pipe() 1921 func Test_zz_nl_err_to_out_pipe()
1922 call ch_logfile('Xlog') 1922 eval 'Xlog'->ch_logfile()
1923 call ch_log('Test_zz_nl_err_to_out_pipe()') 1923 call ch_log('Test_zz_nl_err_to_out_pipe()')
1924 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'}) 1924 let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
1925 call assert_equal("run", job_status(job)) 1925 call assert_equal("run", job_status(job))
1926 try 1926 try
1927 let handle = job_getchannel(job) 1927 let handle = job_getchannel(job)