websocket_server_spec.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. require 'spec_helper'
  2. require 'support/script_helper'
  3. require 'timeout'
  4. describe 'websocket-server', type: :script do
  5. # Why not Rails.root.join here?
  6. # Because it's not avaialable in this spec (no 'rails_helper' = faster start-up)
  7. let(:app_root) { File.expand_path('../..', __dir__) }
  8. let(:ws_server) { File.expand_path('script/websocket-server.rb', app_root) }
  9. let(:pidfile) { File.expand_path('tmp/pids/websocket.pid', app_root) }
  10. let(:output_log) { File.expand_path('log/websocket-server_out.log', app_root) }
  11. let(:error_log) { File.expand_path('log/websocket-server_err.log', app_root) }
  12. context 'with IPv6 bind address (via -b option)', if: has_ipv6? do
  13. # This error is raised for invalid bind addresses
  14. let(:error_msg) { "`start_tcp_server': no acceptor" }
  15. let(:ipv6_addr) { '::1/128' }
  16. # Prevent port assignment conflicts during parallel test execution
  17. let(:port) { rand(60_000..65_000) }
  18. # Flush logs
  19. before do
  20. File.write(output_log, '')
  21. File.write(error_log, '')
  22. end
  23. it 'starts up successfully' do
  24. begin
  25. system("RAILS_ENV=test #{ws_server} start -db #{ipv6_addr} -p #{port} >/dev/null 2>&1")
  26. # Wait for daemon to start
  27. Timeout.timeout(20, Timeout::Error, 'WebSocket Server startup timed out') do
  28. loop { break if File.size(output_log) + File.size(error_log) > 0 }
  29. end
  30. expect(File.read(error_log)).not_to include(error_msg)
  31. ensure
  32. system("#{ws_server} stop >/dev/null 2>&1") if File.exist?(pidfile)
  33. end
  34. end
  35. end
  36. end