websocket_server.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.configure do |config|
  3. config.around(:each, type: :system) do |example|
  4. server_required = example.metadata.fetch(:websocket, true)
  5. if server_required
  6. port = ENV['WS_PORT'] || 6042
  7. ensure_port_available!(port)
  8. websocket_server = Thread.new do
  9. WebsocketServer.run(
  10. p: port,
  11. b: '0.0.0.0',
  12. s: true,
  13. v: false,
  14. d: false,
  15. tls_options: {
  16. private_key_file: "#{Dir.home}/.localhost/localhost.key",
  17. cert_chain_file: "#{Dir.home}/.localhost/localhost.crt",
  18. }
  19. )
  20. end
  21. end
  22. example.run
  23. next if !server_required
  24. # returns immediately and thread may be still shutting down
  25. EventMachine.stop_event_loop
  26. # give thread time to terminate
  27. sleep 0.01 while websocket_server.status
  28. rescue => e
  29. # Handle any errors occuring within this hook, for example Net::ReadTimeout errors of the WS server.
  30. # Otherwise, they would not cause the retry to kick in, but abort the process.
  31. example.example.set_exception(e)
  32. end
  33. def ensure_port_available!(port)
  34. %w[0.0.0.0 127.0.0.1].each do |host|
  35. TCPServer.new(host, port).close # release port immediately
  36. end
  37. rescue Errno::EADDRINUSE
  38. raise "Couldn't start WebSocket server. Maybe another websocket server process is already running?"
  39. end
  40. end