/
test_channel.vim
2681 lines (2322 loc) · 81.7 KB
/
test_channel.vim
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" Test for channel and job functions.
" When +channel is supported then +job is too, so we don't check for that.
source check.vim
CheckFeature channel
source shared.vim
source screendump.vim
source view_util.vim
let s:python = PythonProg()
if s:python == ''
" Can't run this test without Python.
throw 'Skipped: Python command missing'
endif
" Uncomment the next line to see what happens. Output is in
" src/testdir/channellog.
" Add ch_log() calls where you want to see what happens.
" call ch_logfile('channellog', 'w')
func SetUp()
if g:testfunc =~ '_ipv6()$'
let s:localhost = '[::1]:'
let s:testscript = 'test_channel_6.py'
elseif g:testfunc =~ '_unix()$'
let s:localhost = 'unix:Xtestsocket'
let s:testscript = 'test_channel_unix.py'
else
let s:localhost = 'localhost:'
let s:testscript = 'test_channel.py'
endif
let s:chopt = {}
call ch_log(g:testfunc)
" Most tests use job_start(), which can be flaky
let g:test_is_flaky = 1
endfunc
" Run "testfunc" after starting the server and stop the server afterwards.
func s:run_server(testfunc, ...)
call RunServer(s:testscript, a:testfunc, a:000)
endfunc
" Returns the address of the test server.
func s:address(port)
if s:localhost =~ '^unix:'
return s:localhost
else
return s:localhost . a:port
end
endfunc
" Return a list of open files.
" Can be used to make sure no resources leaked.
" Returns an empty list on systems where this is not supported.
func s:get_resources()
let pid = getpid()
if executable('lsof')
return systemlist('lsof -p ' . pid . ' | awk ''$4~/^[0-9]*[rwu]$/&&$5=="REG"{print$NF}''')
elseif isdirectory('/proc/' . pid . '/fd/')
return systemlist('readlink /proc/' . pid . '/fd/* | grep -v ''^/dev/''')
else
return []
endif
endfunc
let g:Ch_responseMsg = ''
func Ch_requestHandler(handle, msg)
let g:Ch_responseHandle = a:handle
let g:Ch_responseMsg = a:msg
endfunc
func Ch_communicate(port)
" Avoid dropping messages, since we don't use a callback here.
let s:chopt.drop = 'never'
" Also add the noblock flag to try it out.
let s:chopt.noblock = 1
let handle = ch_open(s:address(a:port), s:chopt)
if ch_status(handle) == "fail"
call assert_report("Can't open channel")
return
endif
" check that getjob without a job is handled correctly
call assert_equal('no process', string(ch_getjob(handle)))
let dict = handle->ch_info()
call assert_true(dict.id != 0)
call assert_equal('open', dict.status)
if has_key(dict, 'port')
" Channels using Unix sockets have no 'port' entry.
call assert_equal(a:port, string(dict.port))
end
call assert_equal('open', dict.sock_status)
call assert_equal('socket', dict.sock_io)
" Simple string request and reply.
call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
" Malformed command should be ignored.
call assert_equal('ok', ch_evalexpr(handle, 'malformed1'))
call assert_equal('ok', ch_evalexpr(handle, 'malformed2'))
call assert_equal('ok', ch_evalexpr(handle, 'malformed3'))
" split command should work
call assert_equal('ok', ch_evalexpr(handle, 'split'))
call WaitFor('exists("g:split")')
call assert_equal(123, g:split)
" string with ][ should work
call assert_equal('this][that', ch_evalexpr(handle, 'echo this][that'))
" nothing to read now
call assert_equal(0, ch_canread(handle))
" sending three messages quickly then reading should work
for i in range(3)
call ch_sendexpr(handle, 'echo hello ' . i)
endfor
call assert_equal('hello 0', ch_read(handle)[1])
call assert_equal('hello 1', ch_read(handle)[1])
call assert_equal('hello 2', ch_read(handle)[1])
" Request that triggers sending two ex commands. These will usually be
" handled before getting the response, but it's not guaranteed, thus wait a
" tiny bit for the commands to get executed.
call assert_equal('ok', ch_evalexpr(handle, 'make change'))
call WaitForAssert({-> assert_equal("added2", getline("$"))})
call assert_equal('added1', getline(line('$') - 1))
" Request command "echoerr 'this is an error'".
" This will throw an exception, catch it here.
let caught = 'no'
try
call assert_equal('ok', ch_evalexpr(handle, 'echoerr'))
catch /this is an error/
let caught = 'yes'
endtry
if caught != 'yes'
call assert_report("Expected exception from error message")
endif
" Request command "foo bar", which fails silently.
call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
call WaitForAssert({-> assert_match("E492:.*foo bar", v:errmsg)})
call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
call WaitForAssert({-> assert_equal('added more', getline('$'))})
" Send a request with a specific handler.
call ch_sendexpr(handle, 'hello!', {'callback': 'Ch_requestHandler'})
call WaitFor('exists("g:Ch_responseHandle")')
if !exists('g:Ch_responseHandle')
call assert_report('g:Ch_responseHandle was not set')
else
call assert_equal(handle, g:Ch_responseHandle)
unlet g:Ch_responseHandle
endif
call assert_equal('got it', g:Ch_responseMsg)
let g:Ch_responseMsg = ''
call ch_sendexpr(handle, 'hello!', {'callback': function('Ch_requestHandler')})
call WaitFor('exists("g:Ch_responseHandle")')
if !exists('g:Ch_responseHandle')
call assert_report('g:Ch_responseHandle was not set')
else
call assert_equal(handle, g:Ch_responseHandle)
unlet g:Ch_responseHandle
endif
call assert_equal('got it', g:Ch_responseMsg)
" Using lambda.
let g:Ch_responseMsg = ''
call ch_sendexpr(handle, 'hello!', {'callback': {a, b -> Ch_requestHandler(a, b)}})
call WaitFor('exists("g:Ch_responseHandle")')
if !exists('g:Ch_responseHandle')
call assert_report('g:Ch_responseHandle was not set')
else
call assert_equal(handle, g:Ch_responseHandle)
unlet g:Ch_responseHandle
endif
call assert_equal('got it', g:Ch_responseMsg)
" Collect garbage, tests that our handle isn't collected.
call test_garbagecollect_now()
" check setting options (without testing the effect)
eval handle->ch_setoptions({'callback': 's:NotUsed'})
call ch_setoptions(handle, {'timeout': 1111})
call ch_setoptions(handle, {'mode': 'json'})
call assert_fails("call ch_setoptions(handle, {'waittime': 111})", 'E475:')
call ch_setoptions(handle, {'callback': ''})
call ch_setoptions(handle, {'drop': 'never'})
call ch_setoptions(handle, {'drop': 'auto'})
call assert_fails("call ch_setoptions(handle, {'drop': 'bad'})", 'E475:')
call assert_equal(0, ch_setoptions(handle, test_null_dict()))
call assert_equal(0, ch_setoptions(test_null_channel(), {'drop' : 'never'}))
" Send an eval request that works.
call assert_equal('ok', ch_evalexpr(handle, 'eval-works'))
sleep 10m
call assert_equal([-1, 'foo123'], ch_evalexpr(handle, 'eval-result'))
" Send an eval request with special characters.
call assert_equal('ok', ch_evalexpr(handle, 'eval-special'))
sleep 10m
call assert_equal([-2, "foo\x7f\x10\x01bar"], ch_evalexpr(handle, 'eval-result'))
" Send an eval request to get a line with special characters.
call setline(3, "a\nb\<CR>c\x01d\x7fe")
call assert_equal('ok', ch_evalexpr(handle, 'eval-getline'))
sleep 10m
call assert_equal([-3, "a\nb\<CR>c\x01d\x7fe"], ch_evalexpr(handle, 'eval-result'))
" Send an eval request that fails.
call assert_equal('ok', ch_evalexpr(handle, 'eval-fails'))
sleep 10m
call assert_equal([-4, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
" Send an eval request that works but can't be encoded.
call assert_equal('ok', ch_evalexpr(handle, 'eval-error'))
sleep 10m
call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
" Send a bad eval request. There will be no response.
call assert_equal('ok', ch_evalexpr(handle, 'eval-bad'))
sleep 10m
call assert_equal([-5, 'ERROR'], ch_evalexpr(handle, 'eval-result'))
" Send an expr request
call assert_equal('ok', ch_evalexpr(handle, 'an expr'))
call WaitForAssert({-> assert_equal('three', getline('$'))})
call assert_equal('one', getline(line('$') - 2))
call assert_equal('two', getline(line('$') - 1))
" Request a redraw, we don't check for the effect.
call assert_equal('ok', ch_evalexpr(handle, 'redraw'))
call assert_equal('ok', ch_evalexpr(handle, 'redraw!'))
call assert_equal('ok', ch_evalexpr(handle, 'empty-request'))
" Reading while there is nothing available.
call assert_equal(v:none, ch_read(handle, {'timeout': 0}))
if exists('*reltimefloat')
let start = reltime()
call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
let elapsed = reltime(start)
call assert_inrange(0.3, 0.6, reltimefloat(reltime(start)))
endif
" Send without waiting for a response, then wait for a response.
call ch_sendexpr(handle, 'wait a bit')
let resp = ch_read(handle)
call assert_equal(type([]), type(resp))
call assert_equal(type(11), type(resp[0]))
call assert_equal('waited', resp[1])
" make the server quit, can't check if this works, should not hang.
call ch_sendexpr(handle, '!quit!')
endfunc
func Test_communicate()
call s:run_server('Ch_communicate')
endfunc
func Test_communicate_ipv6()
CheckIPv6
call Test_communicate()
endfunc
func Test_communicate_unix()
CheckUnix
call Test_communicate()
call delete('Xtestsocket')
endfunc
" Test that we can open two channels.
func Ch_two_channels(port)
let handle = ch_open(s:address(a:port), s:chopt)
call assert_equal(v:t_channel, type(handle))
if handle->ch_status() == "fail"
call assert_report("Can't open channel")
return
endif
call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
let newhandle = ch_open(s:address(a:port), s:chopt)
if ch_status(newhandle) == "fail"
call assert_report("Can't open second channel")
return
endif
call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
call ch_close(handle)
call assert_equal('got it', ch_evalexpr(newhandle, 'hello!'))
call ch_close(newhandle)
call assert_fails("call ch_close(newhandle)", 'E906:')
endfunc
func Test_two_channels()
eval 'Test_two_channels()'->ch_log()
call s:run_server('Ch_two_channels')
endfunc
func Test_two_channels_ipv6()
CheckIPv6
call Test_two_channels()
endfunc
func Test_two_channels_unix()
CheckUnix
call Test_two_channels()
call delete('Xtestsocket')
endfunc
" Test that a server crash is handled gracefully.
func Ch_server_crash(port)
let handle = ch_open(s:address(a:port), s:chopt)
if ch_status(handle) == "fail"
call assert_report("Can't open channel")
return
endif
call ch_evalexpr(handle, '!crash!')
sleep 10m
endfunc
func Test_server_crash()
call s:run_server('Ch_server_crash')
endfunc
func Test_server_crash_ipv6()
CheckIPv6
call Test_server_crash()
endfunc
func Test_server_crash_unix()
CheckUnix
call Test_server_crash()
call delete('Xtestsocket')
endfunc
"""""""""
func Ch_handler(chan, msg)
call ch_log('Ch_handler()')
unlet g:Ch_reply
let g:Ch_reply = a:msg
endfunc
func Ch_channel_handler(port)
let handle = ch_open(s:address(a:port), s:chopt)
if ch_status(handle) == "fail"
call assert_report("Can't open channel")
return
endif
" Test that it works while waiting on a numbered message.
call assert_equal('ok', ch_evalexpr(handle, 'call me'))
call WaitForAssert({-> assert_equal('we called you', g:Ch_reply)})
" Test that it works while not waiting on a numbered message.
call ch_sendexpr(handle, 'call me again')
call WaitForAssert({-> assert_equal('we did call you', g:Ch_reply)})
endfunc
func Test_channel_handler()
let g:Ch_reply = ""
let s:chopt.callback = 'Ch_handler'
call s:run_server('Ch_channel_handler')
let g:Ch_reply = ""
let s:chopt.callback = function('Ch_handler')
call s:run_server('Ch_channel_handler')
endfunc
func Test_channel_handler_ipv6()
CheckIPv6
call Test_channel_handler()
endfunc
func Test_channel_handler_unix()
CheckUnix
call Test_channel_handler()
call delete('Xtestsocket')
endfunc
"""""""""
let g:Ch_reply = ''
func Ch_zeroHandler(chan, msg)
unlet g:Ch_reply
let g:Ch_reply = a:msg
endfunc
let g:Ch_zero_reply = ''
func Ch_oneHandler(chan, msg)
unlet g:Ch_zero_reply
let g:Ch_zero_reply = a:msg
endfunc
func Ch_channel_zero(port)
let handle = (s:address(a:port))->ch_open(s:chopt)
if ch_status(handle) == "fail"
call assert_report("Can't open channel")
return
endif
" Check that eval works.
call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
" Check that eval works if a zero id message is sent back.
let g:Ch_reply = ''
call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
if s:has_handler
call WaitForAssert({-> assert_equal('zero index', g:Ch_reply)})
else
sleep 20m
call assert_equal('', g:Ch_reply)
endif
" Check that handler works if a zero id message is sent back.
let g:Ch_reply = ''
let g:Ch_zero_reply = ''
call ch_sendexpr(handle, 'send zero', {'callback': 'Ch_oneHandler'})
call WaitForAssert({-> assert_equal('sent zero', g:Ch_zero_reply)})
if s:has_handler
call assert_equal('zero index', g:Ch_reply)
else
call assert_equal('', g:Ch_reply)
endif
endfunc
func Test_zero_reply()
" Run with channel handler
let s:has_handler = 1
let s:chopt.callback = 'Ch_zeroHandler'
call s:run_server('Ch_channel_zero')
unlet s:chopt.callback
" Run without channel handler
let s:has_handler = 0
call s:run_server('Ch_channel_zero')
endfunc
func Test_zero_reply_ipv6()
CheckIPv6
call Test_zero_reply()
endfunc
func Test_zero_reply_unix()
CheckUnix
call Test_zero_reply()
call delete('Xtestsocket')
endfunc
"""""""""
let g:Ch_reply1 = ""
func Ch_handleRaw1(chan, msg)
unlet g:Ch_reply1
let g:Ch_reply1 = a:msg
endfunc
let g:Ch_reply2 = ""
func Ch_handleRaw2(chan, msg)
unlet g:Ch_reply2
let g:Ch_reply2 = a:msg
endfunc
let g:Ch_reply3 = ""
func Ch_handleRaw3(chan, msg)
unlet g:Ch_reply3
let g:Ch_reply3 = a:msg
endfunc
func Ch_raw_one_time_callback(port)
let handle = ch_open(s:address(a:port), s:chopt)
if ch_status(handle) == "fail"
call assert_report("Can't open channel")
return
endif
call ch_setoptions(handle, {'mode': 'raw'})
" The messages are sent raw, we do our own JSON strings here.
call ch_sendraw(handle, "[1, \"hello!\"]\n", {'callback': 'Ch_handleRaw1'})
call WaitForAssert({-> assert_equal("[1, \"got it\"]", g:Ch_reply1)})
call ch_sendraw(handle, "[2, \"echo something\"]\n", {'callback': 'Ch_handleRaw2'})
call ch_sendraw(handle, "[3, \"wait a bit\"]\n", {'callback': 'Ch_handleRaw3'})
call WaitForAssert({-> assert_equal("[2, \"something\"]", g:Ch_reply2)})
" wait for the 200 msec delayed reply
call WaitForAssert({-> assert_equal("[3, \"waited\"]", g:Ch_reply3)})
endfunc
func Test_raw_one_time_callback()
call s:run_server('Ch_raw_one_time_callback')
endfunc
func Test_raw_one_time_callback_ipv6()
CheckIPv6
call Test_raw_one_time_callback()
endfunc
func Test_raw_one_time_callback_unix()
CheckUnix
call Test_raw_one_time_callback()
call delete('Xtestsocket')
endfunc
"""""""""
" Test that trying to connect to a non-existing port fails quickly.
func Test_connect_waittime()
CheckFunction reltimefloat
" this is timing sensitive
let start = reltime()
let handle = ch_open('localhost:9876', s:chopt)
if ch_status(handle) != "fail"
" Oops, port does exists.
call ch_close(handle)
else
let elapsed = reltime(start)
call assert_inrange(0.0, 1.0, reltimefloat(elapsed))
endif
" We intend to use a socket that doesn't exist and wait for half a second
" before giving up. If the socket does exist it can fail in various ways.
" Check for "Connection reset by peer" to avoid flakiness.
let start = reltime()
try
let handle = ch_open('localhost:9867', {'waittime': 500})
if ch_status(handle) != "fail"
" Oops, port does exists.
call ch_close(handle)
else
" Failed connection should wait about 500 msec. Can be longer if the
" computer is busy with other things.
call assert_inrange(0.3, 1.5, reltimefloat(reltime(start)))
endif
catch
if v:exception !~ 'Connection reset by peer'
call assert_report("Caught exception: " . v:exception)
endif
endtry
endfunc
"""""""""
func Test_raw_pipe()
" Add a dummy close callback to avoid that messages are dropped when calling
" ch_canread().
" Also test the non-blocking option.
let job = job_start(s:python . " test_channel_pipe.py",
\ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
call assert_equal(v:t_job, type(job))
call assert_equal("run", job_status(job))
call assert_equal("open", ch_status(job))
call assert_equal("open", ch_status(job), {"part": "out"})
call assert_equal("open", ch_status(job), {"part": "err"})
call assert_fails('call ch_status(job, {"in_mode": "raw"})', 'E475:')
call assert_fails('call ch_status(job, {"part": "in"})', 'E475:')
let dict = ch_info(job)
call assert_true(dict.id != 0)
call assert_equal('open', dict.status)
call assert_equal('open', dict.out_status)
call assert_equal('RAW', dict.out_mode)
call assert_equal('pipe', dict.out_io)
call assert_equal('open', dict.err_status)
call assert_equal('RAW', dict.err_mode)
call assert_equal('pipe', dict.err_io)
try
" For a change use the job where a channel is expected.
call ch_sendraw(job, "echo something\n")
let msg = ch_readraw(job)
call assert_equal("something\n", substitute(msg, "\r", "", 'g'))
call ch_sendraw(job, "double this\n")
let g:handle = job->job_getchannel()
call WaitFor('g:handle->ch_canread()')
unlet g:handle
let msg = ch_readraw(job)
call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
let g:Ch_reply = ""
call ch_sendraw(job, "double this\n", {'callback': 'Ch_handler'})
call WaitForAssert({-> assert_equal("this\nAND this\n", substitute(g:Ch_reply, "\r", "", 'g'))})
call assert_fails("let i = ch_evalraw(job, '2 + 2', {'callback' : 'abc'})", 'E917:')
call assert_fails("let i = ch_evalexpr(job, '2 + 2')", 'E912:')
call assert_fails("let i = ch_evalraw(job, '2 + 2', {'drop' : ''})", 'E475:')
call assert_fails("let i = ch_evalraw(test_null_job(), '2 + 2')", 'E906:')
let reply = job->ch_evalraw("quit\n", {'timeout': 100})
call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
finally
call job_stop(job)
endtry
let g:Ch_job = job
call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
let info = job->job_info()
call assert_equal("dead", info.status)
call assert_equal("term", info.stoponexit)
call assert_equal(2, len(info.cmd))
call assert_equal("test_channel_pipe.py", info.cmd[1])
let found = 0
for j in job_info()
if j == job
let found += 1
endif
endfor
call assert_equal(1, found)
call assert_fails("call job_stop('abc')", 'E475:')
call assert_fails("call job_stop(job, [])", 'E730:')
call assert_fails("call job_stop(test_null_job())", 'E916:')
" Try to use the job and channel where a number is expected. This is not
" related to testing the raw pipe. This test is here just to reuse the
" already created job/channel.
let ch = job_getchannel(job)
call assert_fails('let i = job + 1', 'E910:')
call assert_fails('let j = ch + 1', 'E913:')
call assert_fails('echo 2.0 == job', 'E911:')
call assert_fails('echo 2.0 == ch', 'E914:')
endfunc
func Test_raw_pipe_blob()
" Add a dummy close callback to avoid that messages are dropped when calling
" ch_canread().
" Also test the non-blocking option.
let job = job_start(s:python . " test_channel_pipe.py",
\ {'mode': 'raw', 'drop': 'never', 'noblock': 1})
call assert_equal(v:t_job, type(job))
call assert_equal("run", job_status(job))
call assert_equal("open", ch_status(job))
call assert_equal("open", ch_status(job), {"part": "out"})
try
" Create a blob with the echo command and write it.
let blob = 0z00
let cmd = "echo something\n"
for i in range(0, len(cmd) - 1)
let blob[i] = char2nr(cmd[i])
endfor
call assert_equal(len(cmd), len(blob))
call ch_sendraw(job, blob)
" Read a blob with the reply.
let msg = job->ch_readblob()
let expected = 'something'
for i in range(0, len(expected) - 1)
call assert_equal(char2nr(expected[i]), msg[i])
endfor
let reply = ch_evalraw(job, "quit\n", {'timeout': 100})
call assert_equal("Goodbye!\n", substitute(reply, "\r", "", 'g'))
finally
call job_stop(job)
endtry
let g:Ch_job = job
call WaitForAssert({-> assert_equal("dead", job_status(g:Ch_job))})
let info = job_info(job)
call assert_equal("dead", info.status)
endfunc
func Test_nl_pipe()
let job = job_start([s:python, "test_channel_pipe.py"])
call assert_equal("run", job_status(job))
try
let handle = job_getchannel(job)
call ch_sendraw(handle, "echo something\n")
call assert_equal("something", handle->ch_readraw())
call ch_sendraw(handle, "echoerr wrong\n")
call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
call ch_sendraw(handle, "double this\n")
call assert_equal("this", ch_readraw(handle))
call assert_equal("AND this", ch_readraw(handle))
call ch_sendraw(handle, "split this line\n")
call assert_equal("this linethis linethis line", handle->ch_read())
let reply = ch_evalraw(handle, "quit\n")
call assert_equal("Goodbye!", reply)
finally
call job_stop(job)
endtry
endfunc
func Stop_g_job()
call job_stop(g:job)
if has('win32')
" On MS-Windows the server must close the file handle before we are able
" to delete the file.
call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
sleep 10m
endif
endfunc
func Test_nl_read_file()
call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
let g:job = job_start(s:python . " test_channel_pipe.py",
\ {'in_io': 'file', 'in_name': 'Xinput'})
call assert_equal("run", job_status(g:job))
try
let handle = job_getchannel(g:job)
call assert_equal("something", ch_readraw(handle))
call assert_equal("wrong", ch_readraw(handle, {'part': 'err'}))
call assert_equal("this", ch_readraw(handle))
call assert_equal("AND this", ch_readraw(handle))
finally
call Stop_g_job()
call delete('Xinput')
endtry
call assert_fails("echo ch_read(test_null_channel(), {'callback' : 'abc'})", 'E475:')
endfunc
func Test_nl_write_out_file()
let g:job = job_start(s:python . " test_channel_pipe.py",
\ {'out_io': 'file', 'out_name': 'Xoutput'})
call assert_equal("run", job_status(g:job))
try
let handle = job_getchannel(g:job)
call ch_sendraw(handle, "echo line one\n")
call ch_sendraw(handle, "echo line two\n")
call ch_sendraw(handle, "double this\n")
call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
finally
call Stop_g_job()
call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
call delete('Xoutput')
endtry
endfunc
func Test_nl_write_err_file()
let g:job = job_start(s:python . " test_channel_pipe.py",
\ {'err_io': 'file', 'err_name': 'Xoutput'})
call assert_equal("run", job_status(g:job))
try
let handle = job_getchannel(g:job)
call ch_sendraw(handle, "echoerr line one\n")
call ch_sendraw(handle, "echoerr line two\n")
call ch_sendraw(handle, "doubleerr this\n")
call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))})
finally
call Stop_g_job()
call delete('Xoutput')
endtry
endfunc
func Test_nl_write_both_file()
let g:job = job_start(s:python . " test_channel_pipe.py",
\ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
call assert_equal("run", job_status(g:job))
try
let handle = job_getchannel(g:job)
call ch_sendraw(handle, "echoerr line one\n")
call ch_sendraw(handle, "echo line two\n")
call ch_sendraw(handle, "double this\n")
call ch_sendraw(handle, "doubleerr that\n")
call WaitForAssert({-> assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))})
finally
call Stop_g_job()
call assert_equal(-1, match(s:get_resources(), '\(^\|/\)Xoutput$'))
call delete('Xoutput')
endtry
endfunc
func BufCloseCb(ch)
let g:Ch_bufClosed = 'yes'
endfunc
func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
let g:Ch_bufClosed = 'no'
let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
if a:use_name
let options['out_name'] = 'pipe-output'
if a:do_msg
let expected[0] = 'Reading from channel output...'
else
let options['out_msg'] = 0
call remove(expected, 0)
endif
else
sp pipe-output
let options['out_buf'] = bufnr('%')
quit
call remove(expected, 0)
endif
if a:nomod
let options['out_modifiable'] = 0
endif
let job = job_start(s:python . " test_channel_pipe.py", options)
call assert_equal("run", job_status(job))
try
let handle = job_getchannel(job)
call ch_sendraw(handle, "echo line one\n")
call ch_sendraw(handle, "echo line two\n")
call ch_sendraw(handle, "double this\n")
call ch_sendraw(handle, "quit\n")
sp pipe-output
call WaitFor('line("$") == ' . len(expected) . ' && g:Ch_bufClosed == "yes"')
call assert_equal(expected, getline(1, '$'))
if a:nomod
call assert_equal(0, &modifiable)
else
call assert_equal(1, &modifiable)
endif
call assert_equal('yes', g:Ch_bufClosed)
bwipe!
finally
call job_stop(job)
endtry
endfunc
func Test_pipe_to_buffer_name()
call Run_test_pipe_to_buffer(1, 0, 1)
endfunc
func Test_pipe_to_buffer_nr()
call Run_test_pipe_to_buffer(0, 0, 1)
endfunc
func Test_pipe_to_buffer_name_nomod()
call Run_test_pipe_to_buffer(1, 1, 1)
endfunc
func Test_pipe_to_buffer_name_nomsg()
call Run_test_pipe_to_buffer(1, 0, 1)
endfunc
func Test_close_output_buffer()
let g:test_is_flaky = 1
enew!
let test_lines = ['one', 'two']
call setline(1, test_lines)
let options = {'out_io': 'buffer'}
let options['out_name'] = 'buffer-output'
let options['out_msg'] = 0
split buffer-output
let job = job_start(s:python . " test_channel_write.py", options)
call assert_equal("run", job_status(job))
try
call WaitForAssert({-> assert_equal(3, line('$'))})
quit!
sleep 100m
" Make sure the write didn't happen to the wrong buffer.
call assert_equal(test_lines, getline(1, line('$')))
call assert_equal(-1, bufwinnr('buffer-output'))
sbuf buffer-output
call assert_notequal(-1, bufwinnr('buffer-output'))
sleep 100m
close " no more writes
bwipe!
finally
call job_stop(job)
endtry
endfunc
func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
let options = {'err_io': 'buffer'}
let expected = ['', 'line one', 'line two', 'this', 'AND this']
if a:use_name
let options['err_name'] = 'pipe-err'
if a:do_msg
let expected[0] = 'Reading from channel error...'
else
let options['err_msg'] = 0
call remove(expected, 0)
endif
else
sp pipe-err
let options['err_buf'] = bufnr('%')
quit
call remove(expected, 0)
endif
if a:nomod
let options['err_modifiable'] = 0
endif
let job = job_start(s:python . " test_channel_pipe.py", options)
call assert_equal("run", job_status(job))
try
let handle = job_getchannel(job)
call ch_sendraw(handle, "echoerr line one\n")
call ch_sendraw(handle, "echoerr line two\n")
call ch_sendraw(handle, "doubleerr this\n")
call ch_sendraw(handle, "quit\n")
sp pipe-err
call WaitForAssert({-> assert_equal(expected, getline(1, '$'))})
if a:nomod
call assert_equal(0, &modifiable)
else
call assert_equal(1, &modifiable)
endif
bwipe!
finally
call job_stop(job)
endtry
endfunc
func Test_pipe_err_to_buffer_name()
call Run_test_pipe_err_to_buffer(1, 0, 1)
endfunc
func Test_pipe_err_to_buffer_nr()
call Run_test_pipe_err_to_buffer(0, 0, 1)
endfunc
func Test_pipe_err_to_buffer_name_nomod()
call Run_test_pipe_err_to_buffer(1, 1, 1)
endfunc
func Test_pipe_err_to_buffer_name_nomsg()
call Run_test_pipe_err_to_buffer(1, 0, 0)
endfunc
func Test_pipe_both_to_buffer()
let job = job_start(s:python . " test_channel_pipe.py",
\ {'out_io': 'buffer', 'out_name': 'pipe-err', 'err_io': 'out'})
call assert_equal("run", job_status(job))
let handle = job_getchannel(job)
call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'out'))
call assert_equal(bufnr('pipe-err'), ch_getbufnr(handle, 'err'))
try
call ch_sendraw(handle, "echo line one\n")
call ch_sendraw(handle, "echoerr line two\n")
call ch_sendraw(handle, "double this\n")
call ch_sendraw(handle, "doubleerr that\n")
call ch_sendraw(handle, "quit\n")
sp pipe-err
call WaitForAssert({-> assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))})
bwipe!
finally
call job_stop(job)
endtry
endfunc
func Run_test_pipe_from_buffer(use_name)
sp pipe-input
call setline(1, ['echo one', 'echo two', 'echo three'])
let options = {'in_io': 'buffer', 'block_write': 1}
if a:use_name
let options['in_name'] = 'pipe-input'
else
let options['in_buf'] = bufnr('%')
endif
let job = job_start(s:python . " test_channel_pipe.py", options)
call assert_equal("run", job_status(job))
if has('unix') && !a:use_name
call assert_equal(bufnr('%'), ch_getbufnr(job, 'in'))
endif
try
let handle = job_getchannel(job)
call assert_equal('one', ch_read(handle))
call assert_equal('two', ch_read(handle))
call assert_equal('three', ch_read(handle))
bwipe!
finally
call job_stop(job)
endtry
endfunc
func Test_pipe_from_buffer_name()
call Run_test_pipe_from_buffer(1)
endfunc
func Test_pipe_from_buffer_nr()
call Run_test_pipe_from_buffer(0)
endfunc
func Run_pipe_through_sort(all, use_buffer)
CheckExecutable sort
let g:test_is_flaky = 1
let options = {'out_io': 'buffer', 'out_name': 'sortout'}
if a:use_buffer
split sortin
call setline(1, ['ccc', 'aaa', 'ddd', 'bbb', 'eee'])
let options.in_io = 'buffer'
let options.in_name = 'sortin'
endif
if !a:all