Class: CypressRails::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/cypress-rails/server.rb,
lib/cypress-rails/server/puma.rb,
lib/cypress-rails/server/timer.rb,
lib/cypress-rails/server/checker.rb,
lib/cypress-rails/server/middleware.rb

Defined Under Namespace

Modules: Puma Classes: Checker, Middleware, Timer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, host:, port:, reportable_errors: [Exception], extra_middleware: []) ⇒ Server

Returns a new instance of Server.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cypress-rails/server.rb', line 22

def initialize(app,
  host:,
  port:,
  reportable_errors: [Exception],
  extra_middleware: [])
  @app = app
  @extra_middleware = extra_middleware
  @server_thread = nil # suppress warnings
  @host = host
  @reportable_errors = reportable_errors
  @port = port
  @port ||= Server.ports[port_key]
  @port ||= find_available_port(host)
  @checker = Checker.new(@host, @port)
  @initializer_hooks = InitializerHooks.instance
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



20
21
22
# File 'lib/cypress-rails/server.rb', line 20

def app
  @app
end

#hostObject (readonly)

Returns the value of attribute host.



20
21
22
# File 'lib/cypress-rails/server.rb', line 20

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



20
21
22
# File 'lib/cypress-rails/server.rb', line 20

def port
  @port
end

Class Method Details

.portsObject



15
16
17
# File 'lib/cypress-rails/server.rb', line 15

def ports
  @ports ||= {}
end

Instance Method Details

#bootObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cypress-rails/server.rb', line 70

def boot
  unless responsive?
    Server.ports[port_key] = port

    @server_thread = Thread.new {
      Puma.create(middleware, port, host)
    }

    timer = Timer.new(60)
    until responsive?
      raise "Rack application timed out during boot" if timer.expired?

      @server_thread.join(0.1)
      @initializer_hooks.run(:after_server_start)
    end
  end

  self
end

#errorObject



43
44
45
# File 'lib/cypress-rails/server.rb', line 43

def error
  middleware.error
end

#reset_error!Object



39
40
41
# File 'lib/cypress-rails/server.rb', line 39

def reset_error!
  middleware.clear_error
end

#responsive?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
# File 'lib/cypress-rails/server.rb', line 51

def responsive?
  return false if @server_thread&.join(0)

  res = @checker.request { |http| http.get("/__identify__") }

  res.body == app.object_id.to_s if res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPRedirection)
rescue SystemCallError, Net::ReadTimeout, OpenSSL::SSL::SSLError
  false
end

#using_ssl?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/cypress-rails/server.rb', line 47

def using_ssl?
  @checker.ssl?
end

#wait_for_pending_requestsObject



61
62
63
64
65
66
67
68
# File 'lib/cypress-rails/server.rb', line 61

def wait_for_pending_requests
  timer = Timer.new(60)
  while pending_requests?
    raise "Requests did not finish in 60 seconds: #{middleware.pending_requests}" if timer.expired?

    sleep 0.01
  end
end