Module: TestUtils

Defined in:
common/test_utils.rb

Overview

A set of utils to start/stop the various servers that make up Aspace. Used for running tests. rubocop:disable Lint/HandleExceptions, Lint/RescueWithoutErrorClass

Class Method Summary collapse

Class Method Details

.build_config_string(config) ⇒ Object

rubocop:enable Metrics/MethodLength



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'common/test_utils.rb', line 47

def self.build_config_string(config)
  java_opts = ENV.fetch('JAVA_OPTS', '')
  config.each do |key, value|
    java_opts += " -Daspace.config.#{key}=#{value}"
  end

  # Pass through any system properties from the parent JVM too
  java.lang.System.getProperties.each do |property, value|
    next unless property =~ /aspace.config.(.*)/
    key = Regexp.last_match(1)
    java_opts += " -Daspace.config.#{key}=#{value}" unless config.key?(key)
  end

  ' ' + java_opts
end

.find_antObject



63
64
65
66
67
68
69
70
71
# File 'common/test_utils.rb', line 63

def self.find_ant
  fullpath = ''
  [nil, '..', '../..'].each do |root|
    base = ASUtils.find_base_directory(root)
    fullpath = File.join(File.realpath(base), 'build', 'run')
    break if File.exist? fullpath
  end
  fullpath
end

.free_port_from(port) ⇒ Object



132
133
134
135
136
137
138
139
# File 'common/test_utils.rb', line 132

def self.free_port_from(port)
  server = TCPServer.new('127.0.0.1', port)
  server.close
  port
rescue Errno::EADDRINUSE
  port += 1
  retry
end

.kill(pid) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'common/test_utils.rb', line 11

def self.kill(pid)
  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
    system("taskkill /pid #{pid} /f /t")
  else
    begin
      Process.kill(15, pid)
      Process.waitpid(pid)
    rescue
      # Already dead.
    end
  end
end

.start_backend(port, config = {}, config_file = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'common/test_utils.rb', line 73

def self.start_backend(port, config = {}, config_file = nil)
  db_url = config.delete(:db_url)
  java_opts = build_config_string(config)
  java_opts += " -Daspace.config=#{config_file}" if config_file

  # although we are testing, we need to pass the db we are using
  # through as aspace.db_url.dev because the backend:devserver
  # ant task is harcoded to used that build arg
  build_args = ['backend:devserver:integration',
                "-Daspace.backend.port=#{port}",
                '-Daspace_integration_test=1',
                "-Daspace.db_url.dev=#{db_url}"]
  java_opts += ' -Xmx1024m'

  puts "Spawning backend with opts: #{java_opts}"
  pid = Process.spawn({ 'JAVA_OPTS' => java_opts }, find_ant, *build_args)
  out = File.join(find_ant.gsub(/run/, ''), 'backend_test_log.out')

  TestUtils.wait_for_url("http://localhost:#{port}", out)
  puts "Backend started with pid: #{pid}"

  pid
end

.start_frontend(port, backend_url, config = {}, config_file = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'common/test_utils.rb', line 97

def self.start_frontend(port, backend_url, config = {}, config_file = nil)
  java_opts = "-Daspace.config.backend_url=#{backend_url}"
  java_opts += build_config_string(config)
  java_opts += " -Daspace.config=#{config_file}" if config_file

  build_args = ['frontend:devserver:integration',
                "-Daspace.frontend.port=#{port}"]
  java_opts += ' -Xmx1512m'

  puts "Spawning frontend with opts: #{java_opts}"
  pid = Process.spawn({ 'JAVA_OPTS' => java_opts, 'TEST_MODE' => 'true' },
                      find_ant, *build_args)

  TestUtils.wait_for_url("http://localhost:#{port}")
  puts "Frontend started with pid: #{pid}"

  pid
end

.start_public(port, backend_url, config = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'common/test_utils.rb', line 116

def self.start_public(port, backend_url, config = {})
  java_opts = "-Daspace.config.backend_url=#{backend_url}"
  config.each do |key, value|
    java_opts += " -Daspace.config.#{key}=#{value}"
  end

  pid = Process.spawn({ 'JAVA_OPTS' => java_opts, 'TEST_MODE' => 'true' },
                      find_ant, 'public:devserver:integration',
                      "-Daspace.public.port=#{port}")

  TestUtils.wait_for_url("http://localhost:#{port}")
  puts "Public started with pid: #{pid}"

  pid
end

.wait_for_url(url, out = nil) ⇒ Object

rubocop:disable Metrics/MethodLength



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

def self.wait_for_url(url, out = nil)
  100.times do |idx|
    begin
      uri = URI(url)
      req = Net::HTTP::Get.new(uri.request_uri)
      ASHTTP.start_uri(uri, open_timeout: 60, read_timeout: 60) do |http|
        http.request(req)
      end
      break
    rescue
      # Keep trying
      puts "Waiting for #{url} (#{$ERROR_INFO.inspect})"
      if idx == 10 && !out.nil? && File.file?(out)
        puts "Server is taking a long time to startup, dumping last 50 lines of log:"
        puts IO.readlines(out)[-50..-1]
      end
      sleep(5)
    end
  end
end