Class: Bidi2pdfRails::TestHelpers::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/bidi2pdf_rails/test_helpers/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app: Rails.application, port: TestHelpers.configuration.server_port, host: TestHelpers.configuration.server_host, server_boot_timeout: TestHelpers.configuration.server_boot_timeout) ⇒ Server

Returns a new instance of Server.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bidi2pdf_rails/test_helpers/server.rb', line 12

def initialize(
  app: Rails.application,
  port: TestHelpers.configuration.server_port,
  host: TestHelpers.configuration.server_host,
  server_boot_timeout: TestHelpers.configuration.server_boot_timeout
)
  @app = app
  @port = port
  @host = host
  @server_boot_timeout = server_boot_timeout
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



10
11
12
# File 'lib/bidi2pdf_rails/test_helpers/server.rb', line 10

def app
  @app
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/bidi2pdf_rails/test_helpers/server.rb', line 10

def host
  @host
end

#server_boot_timeoutObject (readonly)

Returns the value of attribute server_boot_timeout.



10
11
12
# File 'lib/bidi2pdf_rails/test_helpers/server.rb', line 10

def server_boot_timeout
  @server_boot_timeout
end

Instance Method Details

#bootObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bidi2pdf_rails/test_helpers/server.rb', line 32

def boot
  return if @booted

  @booted = true

  that = self

  @server_thread = Thread.new do
    Rack::Handler::Puma.run that.app, Port: that.port, Silent: false
  rescue Errno::EADDRINUSE => e
    puts "Port #{that.port} is already/still in use."

    raise e
  end

  wait_until_server_is_ready
end

#portObject



24
25
26
# File 'lib/bidi2pdf_rails/test_helpers/server.rb', line 24

def port
  @port ||= find_available_port
end

#running?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/bidi2pdf_rails/test_helpers/server.rb', line 28

def running?
  @booted && @server_thread&.alive?
end

#shutdownObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/bidi2pdf_rails/test_helpers/server.rb', line 50

def shutdown
  return unless @booted

  @booted = false

  if @server_thread&.alive?
    @server_thread.exit
    @server_thread.join
  end
end