2018-07-02 22:49:47 +00:00
|
|
|
Before:
|
|
|
|
let g:can_run_socket_tests = !has('win32')
|
|
|
|
\ && (exists('*ch_close') || exists('*chanclose'))
|
|
|
|
|
|
|
|
if g:can_run_socket_tests
|
|
|
|
call ale#test#SetDirectory('/testplugin/test')
|
|
|
|
|
|
|
|
let g:channel_id_received = 0
|
|
|
|
let g:data_received = ''
|
|
|
|
|
|
|
|
function! WaitForData(expected_data, timeout) abort
|
|
|
|
let l:ticks = 0
|
|
|
|
|
|
|
|
while l:ticks < a:timeout
|
|
|
|
" Sleep first, so we can switch to the callback.
|
|
|
|
let l:ticks += 10
|
|
|
|
sleep 10ms
|
|
|
|
|
|
|
|
if g:data_received is# a:expected_data
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
endwhile
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! TestCallback(channel_id, data) abort
|
|
|
|
let g:channel_id_received = a:channel_id
|
|
|
|
let g:data_received .= a:data
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
let g:port = 10347
|
2021-01-26 20:43:17 +00:00
|
|
|
let g:pid_tcp = str2nr(system(
|
2018-07-02 22:49:47 +00:00
|
|
|
\ 'python'
|
2021-03-20 22:11:22 +00:00
|
|
|
\ . ' ' . ale#Escape(g:dir . '/script/dumb_tcp_server.py')
|
2018-07-02 22:49:47 +00:00
|
|
|
\ . ' ' . g:port
|
|
|
|
\))
|
2021-03-20 22:11:22 +00:00
|
|
|
let g:pipe_path = tempname()
|
2021-01-26 20:43:17 +00:00
|
|
|
let g:pid_pipe = str2nr(system(
|
|
|
|
\ 'python'
|
2021-03-20 22:11:22 +00:00
|
|
|
\ . ' ' . ale#Escape(g:dir . '/script/dumb_named_pipe_server.py')
|
2021-01-26 20:43:17 +00:00
|
|
|
\ . ' ' . g:pipe_path
|
|
|
|
\))
|
2018-07-02 22:49:47 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
After:
|
|
|
|
if g:can_run_socket_tests
|
|
|
|
call ale#test#RestoreDirectory()
|
|
|
|
|
|
|
|
unlet! g:channel_id_received
|
|
|
|
unlet! g:data_received
|
|
|
|
unlet! g:channel_id
|
|
|
|
|
|
|
|
delfunction WaitForData
|
|
|
|
delfunction TestCallback
|
|
|
|
|
2021-01-26 20:43:17 +00:00
|
|
|
if has_key(g:, 'pid_tcp')
|
|
|
|
call system('kill ' . g:pid_tcp)
|
2018-07-02 22:49:47 +00:00
|
|
|
endif
|
|
|
|
|
2021-01-26 20:43:17 +00:00
|
|
|
if has_key(g:, 'pid_pipe')
|
|
|
|
call system('kill ' . g:pid_pipe)
|
|
|
|
endif
|
|
|
|
|
|
|
|
unlet! g:pid_tcp
|
2018-07-02 22:49:47 +00:00
|
|
|
unlet! g:port
|
2021-01-26 20:43:17 +00:00
|
|
|
unlet! g:pid_pipe
|
|
|
|
unlet! g:pipe_path
|
2018-07-02 22:49:47 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
unlet! g:can_run_socket_tests
|
|
|
|
|
2021-01-26 20:43:17 +00:00
|
|
|
Execute(Sending and receiving connections to tcp sockets should work):
|
2018-07-02 22:49:47 +00:00
|
|
|
if g:can_run_socket_tests
|
|
|
|
let g:channel_id = ale#socket#Open(
|
|
|
|
\ '127.0.0.1:' . g:port,
|
|
|
|
\ {'callback': function('TestCallback')}
|
|
|
|
\)
|
|
|
|
|
|
|
|
Assert g:channel_id >= 0, 'The socket was not opened!'
|
|
|
|
|
|
|
|
call ale#socket#Send(g:channel_id, 'hello')
|
|
|
|
call ale#socket#Send(g:channel_id, ' world')
|
|
|
|
|
|
|
|
AssertEqual 1, ale#socket#IsOpen(g:channel_id)
|
|
|
|
|
|
|
|
" Wait up to 1 second for the expected data to arrive.
|
|
|
|
call WaitForData('hello world', 1000)
|
|
|
|
|
|
|
|
AssertEqual g:channel_id, g:channel_id_received
|
|
|
|
AssertEqual 'hello world', g:data_received
|
2018-07-02 23:30:26 +00:00
|
|
|
AssertEqual '127.0.0.1:' . g:port, ale#socket#GetAddress(g:channel_id)
|
2018-07-02 22:49:47 +00:00
|
|
|
|
|
|
|
call ale#socket#Close(g:channel_id)
|
|
|
|
|
|
|
|
AssertEqual 0, ale#socket#IsOpen(g:channel_id)
|
2018-07-02 23:30:26 +00:00
|
|
|
AssertEqual '', ale#socket#GetAddress(g:channel_id)
|
2018-07-02 22:49:47 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
" NeoVim versions which can't connect to sockets should just fail.
|
|
|
|
if has('nvim') && !exists('*chanclose')
|
|
|
|
AssertEqual -1, ale#socket#Open(
|
|
|
|
\ '127.0.0.1:1111',
|
|
|
|
\ {'callback': function('function')}
|
|
|
|
\)
|
|
|
|
endif
|
2021-01-26 20:43:17 +00:00
|
|
|
|
|
|
|
Execute(Sending and receiving connections to named pipe sockets should work):
|
2021-03-20 22:11:22 +00:00
|
|
|
if g:can_run_socket_tests && has('nvim-0.4')
|
2021-01-26 20:43:17 +00:00
|
|
|
let g:channel_id = ale#socket#Open(
|
|
|
|
\ g:pipe_path,
|
|
|
|
\ {'callback': function('TestCallback')}
|
|
|
|
\)
|
|
|
|
|
|
|
|
Assert g:channel_id >= 0, 'The socket was not opened!'
|
|
|
|
|
|
|
|
call ale#socket#Send(g:channel_id, 'hello')
|
|
|
|
call ale#socket#Send(g:channel_id, ' world')
|
|
|
|
|
|
|
|
AssertEqual 1, ale#socket#IsOpen(g:channel_id)
|
|
|
|
|
|
|
|
" Wait up to 1 second for the expected data to arrive.
|
|
|
|
call WaitForData('hello world', 1000)
|
|
|
|
|
|
|
|
AssertEqual g:channel_id, g:channel_id_received
|
|
|
|
AssertEqual 'hello world', g:data_received
|
|
|
|
AssertEqual g:pipe_path, ale#socket#GetAddress(g:channel_id)
|
|
|
|
|
|
|
|
call ale#socket#Close(g:channel_id)
|
|
|
|
|
|
|
|
AssertEqual 0, ale#socket#IsOpen(g:channel_id)
|
|
|
|
AssertEqual '', ale#socket#GetAddress(g:channel_id)
|
|
|
|
endif
|
|
|
|
|
|
|
|
" NeoVim versions which can't connect to sockets should just fail.
|
2021-03-20 22:11:22 +00:00
|
|
|
if has('nvim-0.4') && !exists('*chanclose')
|
2021-01-26 20:43:17 +00:00
|
|
|
AssertEqual -1, ale#socket#Open(
|
2021-03-20 22:11:22 +00:00
|
|
|
\ g:pipe_path,
|
2021-01-26 20:43:17 +00:00
|
|
|
\ {'callback': function('function')}
|
|
|
|
\)
|
|
|
|
endif
|