comparison src/testdir/test_channel.vim @ 8449:3d567b5839c5 v7.4.1515

commit https://github.com/vim/vim/commit/9fe885e49ade94e6277db0dd18a5bbc1c94c60c4 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Mar 8 16:06:55 2016 +0100 patch 7.4.1515 Problem: Channel test is a bit flaky. Solution: Instead of a fixed sleep time wait until an expression evaluates to true.
author Christian Brabandt <cb@256bit.org>
date Tue, 08 Mar 2016 16:15:04 +0100
parents 6f26b680c243
children d0717262d802
comparison
equal deleted inserted replaced
8448:ee0e4e622697 8449:3d567b5839c5
51 else 51 else
52 exe 'silent !' . cmd . '&' 52 exe 'silent !' . cmd . '&'
53 endif 53 endif
54 54
55 " Wait for up to 2 seconds for the port number to be there. 55 " Wait for up to 2 seconds for the port number to be there.
56 let cnt = 20
57 let l = [] 56 let l = []
58 while cnt > 0 57 for i in range(200)
59 try 58 try
60 let l = readfile("Xportnr") 59 let l = readfile("Xportnr")
61 catch 60 catch
62 endtry 61 endtry
63 if len(l) >= 1 62 if len(l) >= 1
64 break 63 break
65 endif 64 endif
66 sleep 100m 65 sleep 10m
67 let cnt -= 1 66 endfor
68 endwhile
69 call delete("Xportnr") 67 call delete("Xportnr")
70 68
71 if len(l) == 0 69 if len(l) == 0
72 " Can't make the connection, give up. 70 " Can't make the connection, give up.
73 call assert_false(1, "Can't start test_channel.py") 71 call assert_false(1, "Can't start test_channel.py")
100 func s:RequestHandler(handle, msg) 98 func s:RequestHandler(handle, msg)
101 let s:responseHandle = a:handle 99 let s:responseHandle = a:handle
102 let s:responseMsg = a:msg 100 let s:responseMsg = a:msg
103 endfunc 101 endfunc
104 102
103 " Wait for up to a second for "expr" to become true.
104 func s:waitFor(expr)
105 for i in range(100)
106 if eval(a:expr)
107 return
108 endif
109 sleep 10m
110 endfor
111 endfunc
112
105 func s:communicate(port) 113 func s:communicate(port)
106 let handle = ch_open('localhost:' . a:port, s:chopt) 114 let handle = ch_open('localhost:' . a:port, s:chopt)
107 if ch_status(handle) == "fail" 115 if ch_status(handle) == "fail"
108 call assert_false(1, "Can't open channel") 116 call assert_false(1, "Can't open channel")
109 return 117 return
118 126
119 " Request that triggers sending two ex commands. These will usually be 127 " Request that triggers sending two ex commands. These will usually be
120 " handled before getting the response, but it's not guaranteed, thus wait a 128 " handled before getting the response, but it's not guaranteed, thus wait a
121 " tiny bit for the commands to get executed. 129 " tiny bit for the commands to get executed.
122 call assert_equal('ok', ch_evalexpr(handle, 'make change')) 130 call assert_equal('ok', ch_evalexpr(handle, 'make change'))
123 sleep 10m 131 call s:waitFor('"added2" == getline("$")')
124 call assert_equal('added1', getline(line('$') - 1)) 132 call assert_equal('added1', getline(line('$') - 1))
125 call assert_equal('added2', getline('$')) 133 call assert_equal('added2', getline('$'))
126 134
127 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100})) 135 call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
128 sleep 10m 136 call s:waitFor('"added more" == getline("$")')
129 call assert_equal('added more', getline('$')) 137 call assert_equal('added more', getline('$'))
130 138
131 " Send a request with a specific handler. 139 " Send a request with a specific handler.
132 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'}) 140 call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
133 sleep 10m 141 call s:waitFor('exists("s:responseHandle")')
134 if !exists('s:responseHandle') 142 if !exists('s:responseHandle')
135 call assert_false(1, 's:responseHandle was not set') 143 call assert_false(1, 's:responseHandle was not set')
136 else 144 else
137 call assert_equal(handle, s:responseHandle) 145 call assert_equal(handle, s:responseHandle)
138 unlet s:responseHandle 146 unlet s:responseHandle
139 endif 147 endif
140 call assert_equal('got it', s:responseMsg) 148 call assert_equal('got it', s:responseMsg)
141 149
142 let s:responseMsg = '' 150 let s:responseMsg = ''
143 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')}) 151 call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
144 sleep 10m 152 call s:waitFor('exists("s:responseHandle")')
145 if !exists('s:responseHandle') 153 if !exists('s:responseHandle')
146 call assert_false(1, 's:responseHandle was not set') 154 call assert_false(1, 's:responseHandle was not set')
147 else 155 else
148 call assert_equal(handle, s:responseHandle) 156 call assert_equal(handle, s:responseHandle)
149 unlet s:responseHandle 157 unlet s:responseHandle
180 sleep 10m 188 sleep 10m
181 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result')) 189 call assert_equal([-3, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
182 190
183 " Send an expr request 191 " Send an expr request
184 call assert_equal('ok', ch_evalexpr(handle, 'an expr')) 192 call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
185 sleep 10m 193 call s:waitFor('"three" == getline("$")')
186 call assert_equal('one', getline(line('$') - 2)) 194 call assert_equal('one', getline(line('$') - 2))
187 call assert_equal('two', getline(line('$') - 1)) 195 call assert_equal('two', getline(line('$') - 1))
188 call assert_equal('three', getline('$')) 196 call assert_equal('three', getline('$'))
189 197
190 " Request a redraw, we don't check for the effect. 198 " Request a redraw, we don't check for the effect.
279 return 287 return
280 endif 288 endif
281 289
282 " Test that it works while waiting on a numbered message. 290 " Test that it works while waiting on a numbered message.
283 call assert_equal('ok', ch_evalexpr(handle, 'call me')) 291 call assert_equal('ok', ch_evalexpr(handle, 'call me'))
284 sleep 10m 292 call s:waitFor('"we called you" == s:reply')
285 call assert_equal('we called you', s:reply) 293 call assert_equal('we called you', s:reply)
286 294
287 " Test that it works while not waiting on a numbered message. 295 " Test that it works while not waiting on a numbered message.
288 call ch_sendexpr(handle, 'call me again') 296 call ch_sendexpr(handle, 'call me again')
289 sleep 10m 297 call s:waitFor('"we did call you" == s:reply')
290 call assert_equal('we did call you', s:reply) 298 call assert_equal('we did call you', s:reply)
291 endfunc 299 endfunc
292 300
293 func Test_channel_handler() 301 func Test_channel_handler()
294 call ch_log('Test_channel_handler()') 302 call ch_log('Test_channel_handler()')
324 call assert_equal('got it', ch_evalexpr(handle, 'hello!')) 332 call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
325 333
326 " Check that eval works if a zero id message is sent back. 334 " Check that eval works if a zero id message is sent back.
327 let s:ch_reply = '' 335 let s:ch_reply = ''
328 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero')) 336 call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
329 sleep 10m
330 if s:has_handler 337 if s:has_handler
338 call s:waitFor('"zero index" == s:ch_reply')
331 call assert_equal('zero index', s:ch_reply) 339 call assert_equal('zero index', s:ch_reply)
332 else 340 else
341 sleep 20m
333 call assert_equal('', s:ch_reply) 342 call assert_equal('', s:ch_reply)
334 endif 343 endif
335 344
336 " Check that handler works if a zero id message is sent back. 345 " Check that handler works if a zero id message is sent back.
337 let s:ch_reply = '' 346 let s:ch_reply = ''
338 let s:zero_reply = '' 347 let s:zero_reply = ''
339 call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'}) 348 call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'})
340 " Somehow the second message takes a bit of time. 349 call s:waitFor('"sent zero" == s:zero_reply')
341 for i in range(50)
342 if s:zero_reply == 'sent zero'
343 break
344 endif
345 sleep 10m
346 endfor
347 if s:has_handler 350 if s:has_handler
348 call assert_equal('zero index', s:ch_reply) 351 call assert_equal('zero index', s:ch_reply)
349 else 352 else
350 call assert_equal('', s:ch_reply) 353 call assert_equal('', s:ch_reply)
351 endif 354 endif
393 endif 396 endif
394 call ch_setoptions(handle, {'mode': 'raw'}) 397 call ch_setoptions(handle, {'mode': 'raw'})
395 398
396 " The message are sent raw, we do our own JSON strings here. 399 " The message are sent raw, we do our own JSON strings here.
397 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'}) 400 call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
398 for i in range(50) 401 call s:waitFor('s:reply1 != ""')
399 sleep 10m
400 if s:reply1 != ''
401 break
402 endif
403 endfor
404 call assert_equal("[1, \"got it\"]", s:reply1) 402 call assert_equal("[1, \"got it\"]", s:reply1)
405 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'}) 403 call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
406 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'}) 404 call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
407 for i in range(50) 405 call s:waitFor('s:reply2 != ""')
408 sleep 10m
409 if s:reply2 != ''
410 break
411 endif
412 endfor
413 call assert_equal("[2, \"something\"]", s:reply2) 406 call assert_equal("[2, \"something\"]", s:reply2)
414 " wait for up to 500 msec for the 200 msec delayed reply 407 " wait for the 200 msec delayed reply
415 for i in range(50) 408 call s:waitFor('s:reply3 != ""')
416 sleep 10m
417 if s:reply3 != ''
418 break
419 endif
420 endfor
421 call assert_equal("[3, \"waited\"]", s:reply3) 409 call assert_equal("[3, \"waited\"]", s:reply3)
422 endfunc 410 endfunc
423 411
424 func Test_raw_one_time_callback() 412 func Test_raw_one_time_callback()
425 call ch_log('Test_raw_one_time_callback()') 413 call ch_log('Test_raw_one_time_callback()')
570 try 558 try
571 let handle = job_getchannel(job) 559 let handle = job_getchannel(job)
572 call ch_sendraw(handle, "echo line one\n") 560 call ch_sendraw(handle, "echo line one\n")
573 call ch_sendraw(handle, "echo line two\n") 561 call ch_sendraw(handle, "echo line two\n")
574 call ch_sendraw(handle, "double this\n") 562 call ch_sendraw(handle, "double this\n")
575 for i in range(50) 563 call s:waitFor('len(readfile("Xoutput")) > 2')
576 sleep 10m
577 if len(readfile('Xoutput')) > 2
578 break
579 endif
580 endfor
581 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput')) 564 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
582 finally 565 finally
583 call job_stop(job) 566 call job_stop(job)
584 call delete('Xoutput') 567 call delete('Xoutput')
585 endtry 568 endtry
600 try 583 try
601 let handle = job_getchannel(job) 584 let handle = job_getchannel(job)
602 call ch_sendraw(handle, "echoerr line one\n") 585 call ch_sendraw(handle, "echoerr line one\n")
603 call ch_sendraw(handle, "echoerr line two\n") 586 call ch_sendraw(handle, "echoerr line two\n")
604 call ch_sendraw(handle, "doubleerr this\n") 587 call ch_sendraw(handle, "doubleerr this\n")
605 for i in range(50) 588 call s:waitFor('len(readfile("Xoutput")) > 2')
606 sleep 10m
607 if len(readfile('Xoutput')) > 2
608 break
609 endif
610 endfor
611 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput')) 589 call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
612 finally 590 finally
613 call job_stop(job) 591 call job_stop(job)
614 call delete('Xoutput') 592 call delete('Xoutput')
615 endtry 593 endtry
631 let handle = job_getchannel(job) 609 let handle = job_getchannel(job)
632 call ch_sendraw(handle, "echoerr line one\n") 610 call ch_sendraw(handle, "echoerr line one\n")
633 call ch_sendraw(handle, "echo line two\n") 611 call ch_sendraw(handle, "echo line two\n")
634 call ch_sendraw(handle, "double this\n") 612 call ch_sendraw(handle, "double this\n")
635 call ch_sendraw(handle, "doubleerr that\n") 613 call ch_sendraw(handle, "doubleerr that\n")
636 for i in range(50) 614 call s:waitFor('len(readfile("Xoutput")) > 5')
637 sleep 10m
638 if len(readfile('Xoutput')) > 5
639 break
640 endif
641 endfor
642 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput')) 615 call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
643 finally 616 finally
644 call job_stop(job) 617 call job_stop(job)
645 call delete('Xoutput') 618 call delete('Xoutput')
646 endtry 619 endtry
659 call ch_sendraw(handle, "echo line one\n") 632 call ch_sendraw(handle, "echo line one\n")
660 call ch_sendraw(handle, "echo line two\n") 633 call ch_sendraw(handle, "echo line two\n")
661 call ch_sendraw(handle, "double this\n") 634 call ch_sendraw(handle, "double this\n")
662 call ch_sendraw(handle, "quit\n") 635 call ch_sendraw(handle, "quit\n")
663 sp pipe-output 636 sp pipe-output
664 for i in range(100) 637 call s:waitFor('line("$") >= 6')
665 sleep 10m
666 if line('$') >= 6
667 break
668 endif
669 endfor
670 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$')) 638 call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
671 bwipe! 639 bwipe!
672 finally 640 finally
673 call job_stop(job) 641 call job_stop(job)
674 endtry 642 endtry
708 try 676 try
709 let handle = job_getchannel(job) 677 let handle = job_getchannel(job)
710 call ch_sendraw(handle, "echo line one\n") 678 call ch_sendraw(handle, "echo line one\n")
711 call ch_sendraw(handle, "echo line two\n") 679 call ch_sendraw(handle, "echo line two\n")
712 exe ch_getbufnr(handle, "out") . 'sbuf' 680 exe ch_getbufnr(handle, "out") . 'sbuf'
713 for i in range(100) 681 call s:waitFor('line("$") >= 3')
714 sleep 10m
715 if line('$') >= 3
716 break
717 endif
718 endfor
719 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$')) 682 call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
720 bwipe! 683 bwipe!
721 finally 684 finally
722 call job_stop(job) 685 call job_stop(job)
723 endtry 686 endtry
734 try 697 try
735 let handle = job_getchannel(job) 698 let handle = job_getchannel(job)
736 call ch_sendraw(handle, "echo [0, \"hello\"]\n") 699 call ch_sendraw(handle, "echo [0, \"hello\"]\n")
737 call ch_sendraw(handle, "echo [-2, 12.34]\n") 700 call ch_sendraw(handle, "echo [-2, 12.34]\n")
738 exe ch_getbufnr(handle, "out") . 'sbuf' 701 exe ch_getbufnr(handle, "out") . 'sbuf'
739 for i in range(100) 702 call s:waitFor('line("$") >= 3')
740 sleep 10m
741 if line('$') >= 3
742 break
743 endif
744 endfor
745 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$')) 703 call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
746 bwipe! 704 bwipe!
747 finally 705 finally
748 call job_stop(job) 706 call job_stop(job)
749 endtry 707 endtry
750 endfunc 708 endfunc
751 709
752 " Wait a little while for the last line, minus "offset", to equal "line". 710 " Wait a little while for the last line, minus "offset", to equal "line".
753 func Wait_for_last_line(line, offset) 711 func s:wait_for_last_line(line, offset)
754 for i in range(100) 712 for i in range(100)
755 sleep 10m
756 if getline(line('$') - a:offset) == a:line 713 if getline(line('$') - a:offset) == a:line
757 break 714 break
758 endif 715 endif
716 sleep 10m
759 endfor 717 endfor
760 endfunc 718 endfunc
761 719
762 func Test_pipe_io_two_buffers() 720 func Test_pipe_io_two_buffers()
763 if !has('job') 721 if !has('job')
776 \ 'out-io': 'buffer', 'out-name': 'pipe-output'}) 734 \ 'out-io': 'buffer', 'out-name': 'pipe-output'})
777 call assert_equal("run", job_status(job)) 735 call assert_equal("run", job_status(job))
778 try 736 try
779 exe "normal Gaecho hello\<CR>" 737 exe "normal Gaecho hello\<CR>"
780 exe bufwinnr('pipe-output') . "wincmd w" 738 exe bufwinnr('pipe-output') . "wincmd w"
781 call Wait_for_last_line('hello', 0) 739 call s:wait_for_last_line('hello', 0)
782 call assert_equal('hello', getline('$')) 740 call assert_equal('hello', getline('$'))
783 741
784 exe bufwinnr('pipe-input') . "wincmd w" 742 exe bufwinnr('pipe-input') . "wincmd w"
785 exe "normal Gadouble this\<CR>" 743 exe "normal Gadouble this\<CR>"
786 exe bufwinnr('pipe-output') . "wincmd w" 744 exe bufwinnr('pipe-output') . "wincmd w"
787 call Wait_for_last_line('AND this', 0) 745 call s:wait_for_last_line('AND this', 0)
788 call assert_equal('this', getline(line('$') - 1)) 746 call assert_equal('this', getline(line('$') - 1))
789 call assert_equal('AND this', getline('$')) 747 call assert_equal('AND this', getline('$'))
790 748
791 bwipe! 749 bwipe!
792 exe bufwinnr('pipe-input') . "wincmd w" 750 exe bufwinnr('pipe-input') . "wincmd w"
810 \ {'in-io': 'buffer', 'in-name': 'pipe-io', 'in-top': 0, 768 \ {'in-io': 'buffer', 'in-name': 'pipe-io', 'in-top': 0,
811 \ 'out-io': 'buffer', 'out-name': 'pipe-io'}) 769 \ 'out-io': 'buffer', 'out-name': 'pipe-io'})
812 call assert_equal("run", job_status(job)) 770 call assert_equal("run", job_status(job))
813 try 771 try
814 exe "normal Goecho hello\<CR>" 772 exe "normal Goecho hello\<CR>"
815 call Wait_for_last_line('hello', 1) 773 call s:wait_for_last_line('hello', 1)
816 call assert_equal('hello', getline(line('$') - 1)) 774 call assert_equal('hello', getline(line('$') - 1))
817 775
818 exe "normal Gadouble this\<CR>" 776 exe "normal Gadouble this\<CR>"
819 call Wait_for_last_line('AND this', 1) 777 call s:wait_for_last_line('AND this', 1)
820 call assert_equal('this', getline(line('$') - 2)) 778 call assert_equal('this', getline(line('$') - 2))
821 call assert_equal('AND this', getline(line('$') - 1)) 779 call assert_equal('AND this', getline(line('$') - 1))
822 780
823 bwipe! 781 bwipe!
824 finally 782 finally
836 794
837 " Test that "unlet handle" in a handler doesn't crash Vim. 795 " Test that "unlet handle" in a handler doesn't crash Vim.
838 func s:unlet_handle(port) 796 func s:unlet_handle(port)
839 let s:channelfd = ch_open('localhost:' . a:port, s:chopt) 797 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
840 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')}) 798 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
841 sleep 10m 799 call s:waitFor('"what?" == s:unletResponse')
842 call assert_equal('what?', s:unletResponse) 800 call assert_equal('what?', s:unletResponse)
843 endfunc 801 endfunc
844 802
845 func Test_unlet_handle() 803 func Test_unlet_handle()
846 call ch_log('Test_unlet_handle()') 804 call ch_log('Test_unlet_handle()')
857 815
858 " Test that "unlet handle" in a handler doesn't crash Vim. 816 " Test that "unlet handle" in a handler doesn't crash Vim.
859 func s:close_handle(port) 817 func s:close_handle(port)
860 let s:channelfd = ch_open('localhost:' . a:port, s:chopt) 818 let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
861 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')}) 819 call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
862 sleep 10m 820 call s:waitFor('"what?" == s:unletResponse')
863 call assert_equal('what?', s:unletResponse) 821 call assert_equal('what?', s:unletResponse)
864 endfunc 822 endfunc
865 823
866 func Test_close_handle() 824 func Test_close_handle()
867 call ch_log('Test_close_handle()') 825 call ch_log('Test_close_handle()')
909 if ch_status(handle) == "fail" 867 if ch_status(handle) == "fail"
910 call assert_false(1, "Can't open channel") 868 call assert_false(1, "Can't open channel")
911 return 869 return
912 endif 870 endif
913 871
872 let s:call_ret = []
914 call assert_equal('ok', ch_evalexpr(handle, 'call-func')) 873 call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
915 sleep 20m 874 call s:waitFor('len(s:call_ret) > 0')
916 call assert_equal([1, 2, 3], s:call_ret) 875 call assert_equal([1, 2, 3], s:call_ret)
917 endfunc 876 endfunc
918 877
919 func Test_call() 878 func Test_call()
920 call ch_log('Test_call()') 879 call ch_log('Test_call()')
967 return 926 return
968 endif 927 endif
969 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'}) 928 call ch_setoptions(handle, {'close-cb': 'MyCloseCb'})
970 929
971 call assert_equal('', ch_evalexpr(handle, 'close me')) 930 call assert_equal('', ch_evalexpr(handle, 'close me'))
972 sleep 20m 931 call s:waitFor('"closed" == s:ch_close_ret')
973 call assert_equal('closed', s:ch_close_ret) 932 call assert_equal('closed', s:ch_close_ret)
974 endfunc 933 endfunc
975 934
976 func Test_close_callback() 935 func Test_close_callback()
977 call ch_log('Test_close_callback()') 936 call ch_log('Test_close_callback()')