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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
|
" Test for channel functions.
scriptencoding utf-8
if !has('channel')
finish
endif
" This test requires the Python command to run the test server.
" This most likely only works on Unix and Windows.
if has('unix')
" We also need the job feature or the pkill command to make sure the server
" can be stopped.
if !(executable('python') && (has('job') || executable('pkill')))
finish
endif
let s:python = 'python'
elseif has('win32')
" Use Python Launcher for Windows (py.exe) if available.
if executable('py.exe')
let s:python = 'py.exe'
elseif executable('python.exe')
let s:python = 'python.exe'
else
finish
endif
else
" Can't run this test.
finish
endif
let s:chopt = {}
" Run "testfunc" after sarting the server and stop the server afterwards.
func s:run_server(testfunc, ...)
" The Python program writes the port number in Xportnr.
call delete("Xportnr")
if a:0 == 1
let arg = ' ' . a:1
else
let arg = ''
endif
let cmd = s:python . " test_channel.py" . arg
try
if has('job')
let s:job = job_start(cmd, {"stoponexit": "hup"})
call job_setoptions(s:job, {"stoponexit": "kill"})
elseif has('win32')
exe 'silent !start cmd /c start "test_channel" ' . cmd
else
exe 'silent !' . cmd . '&'
endif
" Wait for up to 2 seconds for the port number to be there.
let l = []
for i in range(200)
try
let l = readfile("Xportnr")
catch
endtry
if len(l) >= 1
break
endif
sleep 10m
endfor
call delete("Xportnr")
if len(l) == 0
" Can't make the connection, give up.
call assert_false(1, "Can't start test_channel.py")
return -1
endif
let port = l[0]
call call(function(a:testfunc), [port])
catch
call assert_false(1, "Caught exception: " . v:exception)
finally
call s:kill_server()
endtry
endfunc
func s:kill_server()
if has('job')
if exists('s:job')
call job_stop(s:job)
unlet s:job
endif
elseif has('win32')
call system('taskkill /IM ' . s:python . ' /T /F /FI "WINDOWTITLE eq test_channel"')
else
call system("pkill -f test_channel.py")
endif
endfunc
let s:responseMsg = ''
func s:RequestHandler(handle, msg)
let s:responseHandle = a:handle
let s:responseMsg = a:msg
endfunc
" Wait for up to a second for "expr" to become true.
func s:waitFor(expr)
for i in range(100)
try
if eval(a:expr)
return
endif
catch
endtry
sleep 10m
endfor
endfunc
func s:communicate(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
if has('job')
" check that getjob without a job is handled correctly
call assert_equal('no process', string(ch_getjob(handle)))
endif
let dict = ch_info(handle)
call assert_true(dict.id != 0)
call assert_equal('open', dict.status)
call assert_equal(a:port, string(dict.port))
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 s:waitFor('exists("g:split")')
call assert_equal(123, g:split)
" 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 s:waitFor('"added2" == getline("$")')
call assert_equal('added1', getline(line('$') - 1))
call assert_equal('added2', getline('$'))
" Request command "foo bar", which fails silently.
call assert_equal('ok', ch_evalexpr(handle, 'bad command'))
call s:waitFor('v:errmsg =~ "E492"')
call assert_match('E492:.*foo bar', v:errmsg)
call assert_equal('ok', ch_evalexpr(handle, 'do normal', {'timeout': 100}))
call s:waitFor('"added more" == getline("$")')
call assert_equal('added more', getline('$'))
" Send a request with a specific handler.
call ch_sendexpr(handle, 'hello!', {'callback': 's:RequestHandler'})
call s:waitFor('exists("s:responseHandle")')
if !exists('s:responseHandle')
call assert_false(1, 's:responseHandle was not set')
else
call assert_equal(handle, s:responseHandle)
unlet s:responseHandle
endif
call assert_equal('got it', s:responseMsg)
let s:responseMsg = ''
call ch_sendexpr(handle, 'hello!', {'callback': function('s:RequestHandler')})
call s:waitFor('exists("s:responseHandle")')
if !exists('s:responseHandle')
call assert_false(1, 's:responseHandle was not set')
else
call assert_equal(handle, s:responseHandle)
unlet s:responseHandle
endif
call assert_equal('got it', s:responseMsg)
" Collect garbage, tests that our handle isn't collected.
call garbagecollect()
" check setting options (without testing the effect)
call ch_setoptions(handle, {'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': ''})
" 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 s:waitFor('"three" == getline("$")')
call assert_equal('one', getline(line('$') - 2))
call assert_equal('two', getline(line('$') - 1))
call assert_equal('three', getline('$'))
" 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}))
let start = reltime()
call assert_equal(v:none, ch_read(handle, {'timeout': 333}))
let elapsed = reltime(start)
call assert_true(reltimefloat(elapsed) > 0.3)
call assert_true(reltimefloat(elapsed) < 0.6)
" 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 ch_log('Test_communicate()')
call s:run_server('s:communicate')
endfunc
" Test that we can open two channels.
func s:two_channels(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
call assert_equal('got it', ch_evalexpr(handle, 'hello!'))
let newhandle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(newhandle) == "fail"
call assert_false(1, "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)
endfunc
func Test_two_channels()
call ch_log('Test_two_channels()')
call s:run_server('s:two_channels')
endfunc
" Test that a server crash is handled gracefully.
func s:server_crash(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
call ch_evalexpr(handle, '!crash!')
sleep 10m
endfunc
func Test_server_crash()
call ch_log('Test_server_crash()')
call s:run_server('s:server_crash')
endfunc
"""""""""
let s:reply = ""
func s:Handler(chan, msg)
unlet s:reply
let s:reply = a:msg
endfunc
func s:channel_handler(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "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 s:waitFor('"we called you" == s:reply')
call assert_equal('we called you', s:reply)
" Test that it works while not waiting on a numbered message.
call ch_sendexpr(handle, 'call me again')
call s:waitFor('"we did call you" == s:reply')
call assert_equal('we did call you', s:reply)
endfunc
func Test_channel_handler()
call ch_log('Test_channel_handler()')
let s:chopt.callback = 's:Handler'
call s:run_server('s:channel_handler')
let s:chopt.callback = function('s:Handler')
call s:run_server('s:channel_handler')
unlet s:chopt.callback
endfunc
"""""""""
let s:ch_reply = ''
func s:ChHandler(chan, msg)
unlet s:ch_reply
let s:ch_reply = a:msg
endfunc
let s:zero_reply = ''
func s:OneHandler(chan, msg)
unlet s:zero_reply
let s:zero_reply = a:msg
endfunc
func s:channel_zero(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "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 s:ch_reply = ''
call assert_equal('sent zero', ch_evalexpr(handle, 'send zero'))
if s:has_handler
call s:waitFor('"zero index" == s:ch_reply')
call assert_equal('zero index', s:ch_reply)
else
sleep 20m
call assert_equal('', s:ch_reply)
endif
" Check that handler works if a zero id message is sent back.
let s:ch_reply = ''
let s:zero_reply = ''
call ch_sendexpr(handle, 'send zero', {'callback': 's:OneHandler'})
call s:waitFor('"sent zero" == s:zero_reply')
if s:has_handler
call assert_equal('zero index', s:ch_reply)
else
call assert_equal('', s:ch_reply)
endif
call assert_equal('sent zero', s:zero_reply)
endfunc
func Test_zero_reply()
call ch_log('Test_zero_reply()')
" Run with channel handler
let s:has_handler = 1
let s:chopt.callback = 's:ChHandler'
call s:run_server('s:channel_zero')
unlet s:chopt.callback
" Run without channel handler
let s:has_handler = 0
call s:run_server('s:channel_zero')
endfunc
"""""""""
let s:reply1 = ""
func s:HandleRaw1(chan, msg)
unlet s:reply1
let s:reply1 = a:msg
endfunc
let s:reply2 = ""
func s:HandleRaw2(chan, msg)
unlet s:reply2
let s:reply2 = a:msg
endfunc
let s:reply3 = ""
func s:HandleRaw3(chan, msg)
unlet s:reply3
let s:reply3 = a:msg
endfunc
func s:raw_one_time_callback(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
call ch_setoptions(handle, {'mode': 'raw'})
" The message are sent raw, we do our own JSON strings here.
call ch_sendraw(handle, "[1, \"hello!\"]", {'callback': 's:HandleRaw1'})
call s:waitFor('s:reply1 != ""')
call assert_equal("[1, \"got it\"]", s:reply1)
call ch_sendraw(handle, "[2, \"echo something\"]", {'callback': 's:HandleRaw2'})
call ch_sendraw(handle, "[3, \"wait a bit\"]", {'callback': 's:HandleRaw3'})
call s:waitFor('s:reply2 != ""')
call assert_equal("[2, \"something\"]", s:reply2)
" wait for the 200 msec delayed reply
call s:waitFor('s:reply3 != ""')
call assert_equal("[3, \"waited\"]", s:reply3)
endfunc
func Test_raw_one_time_callback()
call ch_log('Test_raw_one_time_callback()')
call s:run_server('s:raw_one_time_callback')
endfunc
"""""""""
" Test that trying to connect to a non-existing port fails quickly.
func Test_connect_waittime()
call ch_log('Test_connect_waittime()')
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_true(reltimefloat(elapsed) < 1.0)
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 flakyness.
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.
let elapsed = reltime(start)
call assert_true(reltimefloat(elapsed) > 0.3)
call assert_true(reltimefloat(elapsed) < 1.5)
endif
catch
if v:exception !~ 'Connection reset by peer'
call assert_false(1, "Caught exception: " . v:exception)
endif
endtry
endfunc
"""""""""
func Test_raw_pipe()
if !has('job')
return
endif
call ch_log('Test_raw_pipe()')
let job = job_start(s:python . " test_channel_pipe.py", {'mode': 'raw'})
call assert_equal("run", job_status(job))
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 msg = ch_readraw(job)
call assert_equal("this\nAND this\n", substitute(msg, "\r", "", 'g'))
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 s:job = job
call s:waitFor('"dead" == job_status(s:job)')
let info = job_info(job)
call assert_equal("dead", info.status)
call assert_equal("term", info.stoponexit)
endfunc
func Test_nl_pipe()
if !has('job')
return
endif
call ch_log('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", ch_readraw(handle))
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))
let reply = ch_evalraw(handle, "quit\n")
call assert_equal("Goodbye!", reply)
finally
call job_stop(job)
endtry
endfunc
func Test_nl_err_to_out_pipe()
if !has('job')
return
endif
call ch_logfile('Xlog')
call ch_log('Test_nl_err_to_out_pipe()')
let job = job_start(s:python . " test_channel_pipe.py", {'err_io': 'out'})
call assert_equal("run", job_status(job))
try
let handle = job_getchannel(job)
call ch_sendraw(handle, "echo something\n")
call assert_equal("something", ch_readraw(handle))
call ch_sendraw(handle, "echoerr wrong\n")
call assert_equal("wrong", ch_readraw(handle))
finally
call job_stop(job)
call ch_logfile('')
let loglines = readfile('Xlog')
call assert_true(len(loglines) > 10)
let found_test = 0
let found_send = 0
let found_recv = 0
let found_stop = 0
for l in loglines
if l =~ 'Test_nl_err_to_out_pipe'
let found_test = 1
endif
if l =~ 'SEND on.*echo something'
let found_send = 1
endif
if l =~ 'RECV on.*something'
let found_recv = 1
endif
if l =~ 'Stopping job with'
let found_stop = 1
endif
endfor
call assert_equal(1, found_test)
call assert_equal(1, found_send)
call assert_equal(1, found_recv)
call assert_equal(1, found_stop)
call delete('Xlog')
endtry
endfunc
func Test_nl_read_file()
if !has('job')
return
endif
call ch_log('Test_nl_read_file()')
call writefile(['echo something', 'echoerr wrong', 'double this'], 'Xinput')
let job = job_start(s:python . " test_channel_pipe.py",
\ {'in_io': 'file', 'in_name': 'Xinput'})
call assert_equal("run", job_status(job))
try
let handle = job_getchannel(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 job_stop(job)
call delete('Xinput')
endtry
endfunc
func Test_nl_write_out_file()
if !has('job')
return
endif
call ch_log('Test_nl_write_out_file()')
let job = job_start(s:python . " test_channel_pipe.py",
\ {'out_io': 'file', 'out_name': 'Xoutput'})
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 s:waitFor('len(readfile("Xoutput")) > 2')
call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
finally
call job_stop(job)
call delete('Xoutput')
endtry
endfunc
func Test_nl_write_err_file()
if !has('job')
return
endif
call ch_log('Test_nl_write_err_file()')
let job = job_start(s:python . " test_channel_pipe.py",
\ {'err_io': 'file', 'err_name': 'Xoutput'})
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 s:waitFor('len(readfile("Xoutput")) > 2')
call assert_equal(['line one', 'line two', 'this', 'AND this'], readfile('Xoutput'))
finally
call job_stop(job)
call delete('Xoutput')
endtry
endfunc
func Test_nl_write_both_file()
if !has('job')
return
endif
call ch_log('Test_nl_write_both_file()')
let job = job_start(s:python . " test_channel_pipe.py",
\ {'out_io': 'file', 'out_name': 'Xoutput', 'err_io': 'out'})
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, "echo line two\n")
call ch_sendraw(handle, "double this\n")
call ch_sendraw(handle, "doubleerr that\n")
call s:waitFor('len(readfile("Xoutput")) > 5')
call assert_equal(['line one', 'line two', 'this', 'AND this', 'that', 'AND that'], readfile('Xoutput'))
finally
call job_stop(job)
call delete('Xoutput')
endtry
endfunc
func Run_test_pipe_to_buffer(use_name)
if !has('job')
return
endif
call ch_log('Test_pipe_to_buffer()')
let options = {'out_io': 'buffer'}
if a:use_name
let options['out_name'] = 'pipe-output'
let firstline = 'Reading from channel output...'
else
sp pipe-output
let options['out_buf'] = bufnr('%')
quit
let firstline = ''
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 s:waitFor('line("$") >= 6')
if getline('$') == 'DETACH'
$del
endif
call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
bwipe!
finally
call job_stop(job)
endtry
endfunc
func Test_pipe_to_buffer_name()
call Run_test_pipe_to_buffer(1)
endfunc
func Test_pipe_to_buffer_nr()
call Run_test_pipe_to_buffer(0)
endfunc
func Run_test_pipe_err_to_buffer(use_name)
if !has('job')
return
endif
call ch_log('Test_pipe_err_to_buffer()')
let options = {'err_io': 'buffer'}
if a:use_name
let options['err_name'] = 'pipe-err'
let firstline = 'Reading from channel error...'
else
sp pipe-err
let options['err_buf'] = bufnr('%')
quit
let firstline = ''
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 s:waitFor('line("$") >= 5')
call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
bwipe!
finally
call job_stop(job)
endtry
endfunc
func Test_pipe_err_to_buffer_name()
call Run_test_pipe_err_to_buffer(1)
endfunc
func Test_pipe_err_to_buffer_nr()
call Run_test_pipe_err_to_buffer(0)
endfunc
func Test_pipe_both_to_buffer()
if !has('job')
return
endif
call ch_log('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))
try
let handle = job_getchannel(job)
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 s:waitFor('line("$") >= 7')
call 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)
if !has('job')
return
endif
call ch_log('Test_pipe_from_buffer()')
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))
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 Test_pipe_to_nameless_buffer()
if !has('job')
return
endif
call ch_log('Test_pipe_to_nameless_buffer()')
let job = job_start(s:python . " test_channel_pipe.py",
\ {'out_io': 'buffer'})
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")
exe ch_getbufnr(handle, "out") . 'sbuf'
call s:waitFor('line("$") >= 3')
call assert_equal(['Reading from channel output...', 'line one', 'line two'], getline(1, '$'))
bwipe!
finally
call job_stop(job)
endtry
endfunc
func Test_pipe_to_buffer_json()
if !has('job')
return
endif
call ch_log('Test_pipe_to_buffer_json()')
let job = job_start(s:python . " test_channel_pipe.py",
\ {'out_io': 'buffer', 'out_mode': 'json'})
call assert_equal("run", job_status(job))
try
let handle = job_getchannel(job)
call ch_sendraw(handle, "echo [0, \"hello\"]\n")
call ch_sendraw(handle, "echo [-2, 12.34]\n")
exe ch_getbufnr(handle, "out") . 'sbuf'
call s:waitFor('line("$") >= 3')
call assert_equal(['Reading from channel output...', '[0,"hello"]', '[-2,12.34]'], getline(1, '$'))
bwipe!
finally
call job_stop(job)
endtry
endfunc
" Wait a little while for the last line, minus "offset", to equal "line".
func s:wait_for_last_line(line, offset)
for i in range(100)
if getline(line('$') - a:offset) == a:line
break
endif
sleep 10m
endfor
endfunc
func Test_pipe_io_two_buffers()
if !has('job')
return
endif
call ch_log('Test_pipe_io_two_buffers()')
" Create two buffers, one to read from and one to write to.
split pipe-output
set buftype=nofile
split pipe-input
set buftype=nofile
let job = job_start(s:python . " test_channel_pipe.py",
\ {'in_io': 'buffer', 'in_name': 'pipe-input', 'in_top': 0,
\ 'out_io': 'buffer', 'out_name': 'pipe-output',
\ 'block_write': 1})
call assert_equal("run", job_status(job))
try
exe "normal Gaecho hello\<CR>"
exe bufwinnr('pipe-output') . "wincmd w"
call s:wait_for_last_line('hello', 0)
call assert_equal('hello', getline('$'))
exe bufwinnr('pipe-input') . "wincmd w"
exe "normal Gadouble this\<CR>"
exe bufwinnr('pipe-output') . "wincmd w"
call s:wait_for_last_line('AND this', 0)
call assert_equal('this', getline(line('$') - 1))
call assert_equal('AND this', getline('$'))
bwipe!
exe bufwinnr('pipe-input') . "wincmd w"
bwipe!
finally
call job_stop(job)
endtry
endfunc
func Test_pipe_io_one_buffer()
if !has('job')
return
endif
call ch_log('Test_pipe_io_one_buffer()')
" Create one buffer to read from and to write to.
split pipe-io
set buftype=nofile
let job = job_start(s:python . " test_channel_pipe.py",
\ {'in_io': 'buffer', 'in_name': 'pipe-io', 'in_top': 0,
\ 'out_io': 'buffer', 'out_name': 'pipe-io',
\ 'block_write': 1})
call assert_equal("run", job_status(job))
try
exe "normal Goecho hello\<CR>"
call s:wait_for_last_line('hello', 1)
call assert_equal('hello', getline(line('$') - 1))
exe "normal Gadouble this\<CR>"
call s:wait_for_last_line('AND this', 1)
call assert_equal('this', getline(line('$') - 2))
call assert_equal('AND this', getline(line('$') - 1))
bwipe!
finally
call job_stop(job)
endtry
endfunc
func Test_pipe_null()
if !has('job')
return
endif
call ch_log('Test_pipe_null()')
" We cannot check that no I/O works, we only check that the job starts
" properly.
let job = job_start(s:python . " test_channel_pipe.py something",
\ {'in_io': 'null'})
call assert_equal("run", job_status(job))
try
call assert_equal('something', ch_read(job))
finally
call job_stop(job)
endtry
let job = job_start(s:python . " test_channel_pipe.py err-out",
\ {'out_io': 'null'})
call assert_equal("run", job_status(job))
try
call assert_equal('err-out', ch_read(job, {"part": "err"}))
finally
call job_stop(job)
endtry
let job = job_start(s:python . " test_channel_pipe.py something",
\ {'err_io': 'null'})
call assert_equal("run", job_status(job))
try
call assert_equal('something', ch_read(job))
finally
call job_stop(job)
endtry
let job = job_start(s:python . " test_channel_pipe.py something",
\ {'out_io': 'null', 'err_io': 'out'})
call assert_equal("run", job_status(job))
call job_stop(job)
let job = job_start(s:python . " test_channel_pipe.py something",
\ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'})
call assert_equal("run", job_status(job))
call assert_equal('channel fail', string(job_getchannel(job)))
call assert_equal('fail', ch_status(job))
call job_stop(job)
endfunc
func Test_reuse_channel()
if !has('job')
return
endif
call ch_log('Test_reuse_channel()')
let job = job_start(s:python . " test_channel_pipe.py")
call assert_equal("run", job_status(job))
let handle = job_getchannel(job)
try
call ch_sendraw(handle, "echo something\n")
call assert_equal("something", ch_readraw(handle))
finally
call job_stop(job)
endtry
let job = job_start(s:python . " test_channel_pipe.py", {'channel': handle})
call assert_equal("run", job_status(job))
let handle = job_getchannel(job)
try
call ch_sendraw(handle, "echo again\n")
call assert_equal("again", ch_readraw(handle))
finally
call job_stop(job)
endtry
endfunc
func Test_out_cb()
if !has('job')
return
endif
call ch_log('Test_out_cb()')
let dict = {'thisis': 'dict: '}
func dict.outHandler(chan, msg) dict
let s:outmsg = self.thisis . a:msg
endfunc
func dict.errHandler(chan, msg) dict
let s:errmsg = self.thisis . a:msg
endfunc
let job = job_start(s:python . " test_channel_pipe.py",
\ {'out_cb': dict.outHandler,
\ 'out_mode': 'json',
\ 'err_cb': dict.errHandler,
\ 'err_mode': 'json'})
call assert_equal("run", job_status(job))
try
let s:outmsg = ''
let s:errmsg = ''
call ch_sendraw(job, "echo [0, \"hello\"]\n")
call ch_sendraw(job, "echoerr [0, \"there\"]\n")
call s:waitFor('s:outmsg != ""')
call assert_equal("dict: hello", s:outmsg)
call s:waitFor('s:errmsg != ""')
call assert_equal("dict: there", s:errmsg)
finally
call job_stop(job)
endtry
endfunc
""""""""""
let s:unletResponse = ''
func s:UnletHandler(handle, msg)
let s:unletResponse = a:msg
unlet s:channelfd
endfunc
" Test that "unlet handle" in a handler doesn't crash Vim.
func s:unlet_handle(port)
let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
call ch_sendexpr(s:channelfd, "test", {'callback': function('s:UnletHandler')})
call s:waitFor('"what?" == s:unletResponse')
call assert_equal('what?', s:unletResponse)
endfunc
func Test_unlet_handle()
call ch_log('Test_unlet_handle()')
call s:run_server('s:unlet_handle')
endfunc
""""""""""
let s:unletResponse = ''
func s:CloseHandler(handle, msg)
let s:unletResponse = a:msg
call ch_close(s:channelfd)
endfunc
" Test that "unlet handle" in a handler doesn't crash Vim.
func s:close_handle(port)
let s:channelfd = ch_open('localhost:' . a:port, s:chopt)
call ch_sendexpr(s:channelfd, "test", {'callback': function('s:CloseHandler')})
call s:waitFor('"what?" == s:unletResponse')
call assert_equal('what?', s:unletResponse)
endfunc
func Test_close_handle()
call ch_log('Test_close_handle()')
call s:run_server('s:close_handle')
endfunc
""""""""""
func Test_open_fail()
call ch_log('Test_open_fail()')
silent! let ch = ch_open("noserver")
echo ch
let d = ch
endfunc
""""""""""
func s:open_delay(port)
" Wait up to a second for the port to open.
let s:chopt.waittime = 1000
let channel = ch_open('localhost:' . a:port, s:chopt)
unlet s:chopt.waittime
if ch_status(channel) == "fail"
call assert_false(1, "Can't open channel")
return
endif
call assert_equal('got it', ch_evalexpr(channel, 'hello!'))
call ch_close(channel)
endfunc
func Test_open_delay()
call ch_log('Test_open_delay()')
" The server will wait half a second before creating the port.
call s:run_server('s:open_delay', 'delay')
endfunc
"""""""""
function MyFunction(a,b,c)
let s:call_ret = [a:a, a:b, a:c]
endfunc
function s:test_call(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
let s:call_ret = []
call assert_equal('ok', ch_evalexpr(handle, 'call-func'))
call s:waitFor('len(s:call_ret) > 0')
call assert_equal([1, 2, 3], s:call_ret)
endfunc
func Test_call()
call ch_log('Test_call()')
call s:run_server('s:test_call')
endfunc
"""""""""
let s:job_exit_ret = 'not yet'
function MyExitCb(job, status)
let s:job_exit_ret = 'done'
endfunc
function s:test_exit_callback(port)
call job_setoptions(s:job, {'exit_cb': 'MyExitCb'})
let s:exit_job = s:job
call assert_equal('MyExitCb', job_info(s:job)['exit_cb'])
endfunc
func Test_exit_callback()
if has('job')
call ch_log('Test_exit_callback()')
call s:run_server('s:test_exit_callback')
" wait up to a second for the job to exit
for i in range(100)
if s:job_exit_ret == 'done'
break
endif
sleep 10m
" calling job_status() triggers the callback
call job_status(s:exit_job)
endfor
call assert_equal('done', s:job_exit_ret)
call assert_equal('dead', job_info(s:exit_job).status)
unlet s:exit_job
endif
endfunc
"""""""""
let s:ch_close_ret = 'alive'
function MyCloseCb(ch)
let s:ch_close_ret = 'closed'
endfunc
function s:test_close_callback(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
call ch_setoptions(handle, {'close_cb': 'MyCloseCb'})
call assert_equal('', ch_evalexpr(handle, 'close me'))
call s:waitFor('"closed" == s:ch_close_ret')
call assert_equal('closed', s:ch_close_ret)
endfunc
func Test_close_callback()
call ch_log('Test_close_callback()')
call s:run_server('s:test_close_callback')
endfunc
function s:test_close_partial(port)
let handle = ch_open('localhost:' . a:port, s:chopt)
if ch_status(handle) == "fail"
call assert_false(1, "Can't open channel")
return
endif
let s:d = {}
func s:d.closeCb(ch) dict
let self.close_ret = 'closed'
endfunc
call ch_setoptions(handle, {'close_cb': s:d.closeCb})
call assert_equal('', ch_evalexpr(handle, 'close me'))
call s:waitFor('"closed" == s:d.close_ret')
call assert_equal('closed', s:d.close_ret)
unlet s:d
endfunc
func Test_close_partial()
call ch_log('Test_close_partial()')
call s:run_server('s:test_close_partial')
endfunc
func Test_job_start_invalid()
call assert_fails('call job_start($x)', 'E474:')
call assert_fails('call job_start("")', 'E474:')
endfunc
" This leaking memory.
func Test_partial_in_channel_cycle()
let d = {}
let d.a = function('string', [d])
try
let d.b = ch_open('nowhere:123', {'close_cb': d.a})
catch
call assert_exception('E901:')
endtry
unlet d
endfunc
" Uncomment this to see what happens, output is in src/testdir/channellog.
" call ch_logfile('channellog', 'w')
|