Class: Brut::SpecSupport::E2ETestServer

Inherits:
Object
  • Object
show all
Includes:
SemanticLogger::Loggable
Defined in:
lib/brut/spec_support/e2e_test_server.rb

Overview

Manages running the app in test mode for the purposes of running End-to-End tests against it.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bin_dir:) ⇒ E2ETestServer

Create the test server, which will run various Brut dev commands from the given bin dir

Parameters:

  • bin_dir (Pathname)

    path to where the app's Brut-provide CLI apps are installed



18
19
20
21
# File 'lib/brut/spec_support/e2e_test_server.rb', line 18

def initialize(bin_dir:)
  @bin_dir = bin_dir
  @pid     = nil
end

Class Method Details

.instanceObject



10
11
12
# File 'lib/brut/spec_support/e2e_test_server.rb', line 10

def self.instance
  @instance ||= self.new(bin_dir: Brut.container.project_root / "bin")
end

Instance Method Details

#startObject

Starts the server. Returns when the server has started



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/brut/spec_support/e2e_test_server.rb', line 24

def start
  if !@pid.nil?
    logger.warn "Server is already running on pid '#{@pid}'"
    return
  end
  Bundler.with_unbundled_env do
    command = "#{@bin_dir}/test-server"
    logger.info "Starting test server via '#{command}'"
    @pid = Process.spawn(
      command,
      pgroup: true # We want this in its own process group, so we can 
                   # more reliably kill it later on
    )
    logger.info "Starting with pid '#{@pid}'"
  end
  if is_port_open?("0.0.0.0",6503)
    logger.info "Server is listening for requests on port 6503"
  else
    raise "Problem: server never started"
  end
end

#stopObject

Stops the server



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/brut/spec_support/e2e_test_server.rb', line 47

def stop
  if @pid.nil?
    logger.warn "Server is already stopped"
    return
  end
  logger.info "Stopping server nicely with TERM of pid '#{@pid}'"
  Process.kill("-TERM",@pid) # The '-' is to kill the process group, not just the pid
  begin
    Timeout.timeout(4) do
      Process.wait(@pid)
    end
  rescue Timeout::Error
    logger.warn "Server still active after 4 seconds. Trying KILL on pid '#{@pid}'"
    Process.kill("-KILL",@pid)
  end
  @pid = nil
end