summaryrefslogtreecommitdiff
path: root/test/test_socket_connections.vader
diff options
context:
space:
mode:
authorw0rp <devw0rp@gmail.com>2018-07-02 23:49:47 +0100
committerw0rp <devw0rp@gmail.com>2018-07-02 23:49:47 +0100
commit01c68fedd63ccb9360134dba1cd2c722bc9f7006 (patch)
tree086ad02a035a20acc12cfe2a693cb8c9db901466 /test/test_socket_connections.vader
parentb637b35ea89a5a756477b9e2545a2f36299ac5fa (diff)
downloadale-01c68fedd63ccb9360134dba1cd2c722bc9f7006.zip
#830 Implement a socket wrapper API for use with LSP connections
Diffstat (limited to 'test/test_socket_connections.vader')
-rw-r--r--test/test_socket_connections.vader90
1 files changed, 90 insertions, 0 deletions
diff --git a/test/test_socket_connections.vader b/test/test_socket_connections.vader
new file mode 100644
index 00000000..71a1728b
--- /dev/null
+++ b/test/test_socket_connections.vader
@@ -0,0 +1,90 @@
+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
+ let g:pid = str2nr(system(
+ \ 'python'
+ \ . ' ' . ale#Escape(g:dir . '/dumb_tcp_server.py')
+ \ . ' ' . g:port
+ \))
+ 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
+
+ if has_key(g:, 'pid')
+ call system('kill ' . g:pid)
+ endif
+
+ unlet! g:pid
+ unlet! g:port
+ endif
+
+ unlet! g:can_run_socket_tests
+
+Execute(Sending and receiving connections to sockets should work):
+ 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
+
+ call ale#socket#Close(g:channel_id)
+
+ AssertEqual 0, ale#socket#IsOpen(g:channel_id)
+ 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