Module: TestUtils

Defined in:
common/test_utils.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) build_config_string(config)



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

def self.build_config_string(config)
  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|
    if property =~ /aspace.config.(.*)/
      key = $1
      if !config.has_key?(key)
        java_opts += " -Daspace.config.#{key}=#{value}"
      end
    end
  end

  java_opts
end

+ (Object) free_port_from(port)



137
138
139
140
141
142
143
144
145
146
147
# File 'common/test_utils.rb', line 137

def self.free_port_from(port)
  begin
    server = TCPServer.new('127.0.0.1', port)
    server.close

    port
  rescue Errno::EADDRINUSE
    port += 1
    retry
  end
end

+ (Object) get(url)



21
22
23
# File 'common/test_utils.rb', line 21

def self.get(url)
  Net::HTTP.get_response(url)
end

+ (Object) kill(pid)



7
8
9
10
11
12
13
14
15
16
17
18
# File 'common/test_utils.rb', line 7

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

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



67
68
69
70
71
72
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 67

def self.start_backend(port, config = {}, config_file = nil)
  base = File.dirname(__FILE__)

  java_opts = "-Xmx256M -XX:MaxPermSize=128M"
  java_opts += build_config_string(config)
  if config_file
    java_opts += " -Daspace.config=#{config_file}"
  end

  build_args = ["backend:devserver:integration",
          "-Daspace.backend.port=#{port}",
          "-Daspace_integration_test=1"]

  if ENV['GEM_HOME']
    build_args << "-Dgem_home=#{ENV['GEM_HOME']}"
  end

  if config[:solr_port]
    build_args.push("-Daspace.solr.port=#{config[:solr_port]}")
    java_opts += " -Daspace.config.solr_url=http://localhost:#{config[:solr_port]}"
  end

  pid = Process.spawn({:JAVA_OPTS => java_opts},
                      "#{base}/../build/run", *build_args)

  TestUtils.wait_for_url("http://localhost:#{port}")

  pid
end

+ (Object) start_frontend(port, backend_url, config = {})



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

def self.start_frontend(port, backend_url, config = {})
  base = File.dirname(__FILE__)

  java_opts = "-Xmx256M -XX:MaxPermSize=128M -Daspace.config.backend_url=#{backend_url}"
  java_opts += build_config_string(config)

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

  if ENV['GEM_HOME']
    build_args << "-Dgem_home=#{ENV['GEM_HOME']}"
  end

  pid = Process.spawn({:JAVA_OPTS => java_opts, :TEST_MODE => "true"},
                      "#{base}/../build/run", *build_args)

  TestUtils.wait_for_url("http://localhost:#{port}")

  pid
end

+ (Object) start_public(port, backend_url, config = {})



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'common/test_utils.rb', line 119

def self.start_public(port, backend_url, config = {})
  base = File.dirname(__FILE__)

  java_opts = "-Xmx256M -XX:MaxPermSize=128M -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"},
                      "#{base}/../build/run", "public:devserver:integration",
                      "-Daspace.public.port=#{port}")

  TestUtils.wait_for_url("http://localhost:#{port}")

  pid
end

+ (Object) wait_for_url(url)



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 26

def self.wait_for_url(url)
  100.times do
    begin
      uri = URI(url)
      req = Net::HTTP::Get.new(uri.request_uri)
      Net::HTTP.start(uri.host, uri.port, nil, nil, nil,
                      :open_timeout => 3,
                      :read_timeout => 3) do |http|
        http.request(req)
      end

      break
    rescue
      # Keep trying
      puts "Waiting for #{url} (#{$!.inspect})"
      sleep(5)
    end
  end
end