websocket_server_spec.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # rubocop:disable RSpec/DescribeClass
  2. require 'spec_helper'
  3. require 'support/script_helper'
  4. require 'timeout'
  5. describe 'websocket-server', type: :script do
  6. # Why not Rails.root.join here?
  7. # Because it's not avaialable in this spec (no 'rails_helper' = faster start-up)
  8. let(:app_root) { File.expand_path('../..', __dir__) }
  9. let(:ws_server) { File.expand_path('script/websocket-server.rb', app_root) }
  10. let(:pidfile) { File.expand_path('tmp/pids/websocket.pid', app_root) }
  11. let(:output_log) { File.expand_path('log/websocket-server_out.log', app_root) }
  12. let(:error_log) { File.expand_path('log/websocket-server_err.log', app_root) }
  13. context 'with IPv6 bind address (via -b option)', if: has_ipv6? do
  14. # This error is raised for invalid bind addresses
  15. let(:error_msg) { "`start_tcp_server': no acceptor" }
  16. let(:ipv6_addr) { '::1/128' }
  17. # Prevent port assignment conflicts during parallel test execution
  18. let(:port) { rand(60_000..65_000) }
  19. # Flush logs
  20. before do
  21. File.write(output_log, '')
  22. File.write(error_log, '')
  23. end
  24. it 'starts up successfully' do
  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. # rubocop:enable RSpec/DescribeClass