/ test / test_socket_connections.vader
test_socket_connections.vader
  1  Before:
  2    let g:can_run_socket_tests = !has('win32')
  3    \ && (exists('*ch_close') || exists('*chanclose'))
  4  
  5    if g:can_run_socket_tests
  6      call ale#test#SetDirectory('/testplugin/test')
  7  
  8      let g:channel_id_received = 0
  9      let g:data_received = ''
 10  
 11      function! WaitForData(expected_data, timeout) abort
 12        let l:ticks = 0
 13  
 14        while l:ticks < a:timeout
 15          " Sleep first, so we can switch to the callback.
 16          let l:ticks += 10
 17          sleep 10ms
 18  
 19          if g:data_received is# a:expected_data
 20            break
 21          endif
 22        endwhile
 23      endfunction
 24  
 25      function! TestCallback(channel_id, data) abort
 26        let g:channel_id_received = a:channel_id
 27        let g:data_received .= a:data
 28      endfunction
 29  
 30      let g:port = 10347
 31      let g:pid_tcp = str2nr(system(
 32      \ 'python'
 33      \ . ' ' . ale#Escape(g:dir . '/script/dumb_tcp_server.py')
 34      \ . ' ' . g:port
 35      \))
 36      let g:pipe_path = tempname()
 37      let g:pid_pipe = str2nr(system(
 38      \ 'python'
 39      \ . ' ' . ale#Escape(g:dir . '/script/dumb_named_pipe_server.py')
 40      \ . ' ' . g:pipe_path
 41      \))
 42    endif
 43  
 44  After:
 45    if g:can_run_socket_tests
 46      call ale#test#RestoreDirectory()
 47  
 48      unlet! g:channel_id_received
 49      unlet! g:data_received
 50      unlet! g:channel_id
 51  
 52      delfunction WaitForData
 53      delfunction TestCallback
 54  
 55      if has_key(g:, 'pid_tcp')
 56        call system('kill ' . g:pid_tcp)
 57      endif
 58  
 59      if has_key(g:, 'pid_pipe')
 60        call system('kill ' . g:pid_pipe)
 61      endif
 62  
 63      unlet! g:pid_tcp
 64      unlet! g:port
 65      unlet! g:pid_pipe
 66      unlet! g:pipe_path
 67    endif
 68  
 69    unlet! g:can_run_socket_tests
 70  
 71  Execute(Sending and receiving connections to tcp sockets should work):
 72    if g:can_run_socket_tests
 73      let g:channel_id = ale#socket#Open(
 74      \ '127.0.0.1:' . g:port,
 75      \ {'callback': function('TestCallback')}
 76      \)
 77  
 78      Assert g:channel_id >= 0, 'The socket was not opened!'
 79  
 80      call ale#socket#Send(g:channel_id, 'hello')
 81      call ale#socket#Send(g:channel_id, ' world')
 82  
 83      AssertEqual 1, ale#socket#IsOpen(g:channel_id)
 84  
 85      " Wait up to 1 second for the expected data to arrive.
 86      call WaitForData('hello world', 1000)
 87  
 88      AssertEqual g:channel_id, g:channel_id_received
 89      AssertEqual 'hello world', g:data_received
 90      AssertEqual '127.0.0.1:' . g:port, ale#socket#GetAddress(g:channel_id)
 91  
 92      call ale#socket#Close(g:channel_id)
 93  
 94      AssertEqual 0, ale#socket#IsOpen(g:channel_id)
 95      AssertEqual '', ale#socket#GetAddress(g:channel_id)
 96    endif
 97  
 98    " NeoVim versions which can't connect to sockets should just fail.
 99    if has('nvim') && !exists('*chanclose')
100      AssertEqual -1, ale#socket#Open(
101      \ '127.0.0.1:1111',
102      \ {'callback': function('function')}
103      \)
104    endif
105  
106  Execute(Sending and receiving connections to named pipe sockets should work):
107    if g:can_run_socket_tests && has('nvim-0.4')
108      let g:channel_id = ale#socket#Open(
109      \ g:pipe_path,
110      \ {'callback': function('TestCallback')}
111      \)
112  
113      Assert g:channel_id >= 0, 'The socket was not opened!'
114  
115      call ale#socket#Send(g:channel_id, 'hello')
116      call ale#socket#Send(g:channel_id, ' world')
117  
118      AssertEqual 1, ale#socket#IsOpen(g:channel_id)
119  
120      " Wait up to 1 second for the expected data to arrive.
121      call WaitForData('hello world', 1000)
122  
123      AssertEqual g:channel_id, g:channel_id_received
124      AssertEqual 'hello world', g:data_received
125      AssertEqual g:pipe_path, ale#socket#GetAddress(g:channel_id)
126  
127      call ale#socket#Close(g:channel_id)
128  
129      AssertEqual 0, ale#socket#IsOpen(g:channel_id)
130      AssertEqual '', ale#socket#GetAddress(g:channel_id)
131    endif
132  
133    " NeoVim versions which can't connect to sockets should just fail.
134    if has('nvim-0.4') && !exists('*chanclose')
135      AssertEqual -1, ale#socket#Open(
136      \ g:pipe_path,
137      \ {'callback': function('function')}
138      \)
139    endif