File: integration_test.rb

package info (click to toggle)
ruby-sinatra 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,200 kB
  • sloc: ruby: 7,675; makefile: 5
file content (65 lines) | stat: -rw-r--r-- 1,472 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require File.expand_path('../helper', __FILE__)
require 'rbconfig'
require 'open-uri'
require 'timeout'

class IntegrationTest < Test::Unit::TestCase
  def app_file
    File.expand_path('../integration/app.rb', __FILE__)
  end

  def port
    5000 + (Process.pid % 1000)
  end

  def command
    cmd = ['exec']
    if RbConfig.respond_to? :ruby
      cmd << RbConfig.ruby.inspect
    else
      file, dir = RbConfig::CONFIG.values_at('ruby_install_name', 'bindir')
      cmd << File.expand_path(file, dir).inspect
    end
    cmd << "-I" << File.expand_path('../../lib', __FILE__).inspect
    cmd << app_file.inspect << '-p' << port << '2>&1'
    cmd.join(" ")
  end

  def display_output(pipe)
    out = ""
    loop { out <<  pipe.read_nonblock(1) }
  rescue
    $stderr.puts command, out unless out.empty?
  end

  def kill(pid, signal = "TERM")
    Process.kill(signal, pid)
  rescue NotImplementedError
    system "kill -s #{signal} #{pid}"
  end

  def with_server
    pipe = IO.popen(command)
    error = nil
    Timeout.timeout(120) do
      begin
        yield
      rescue Errno::ECONNREFUSED => e
        error = e
        sleep 0.1
        retry
      end
    end
    kill(pipe.pid) if pipe
  rescue Timeout::Error => e
    display_output pipe
    kill(pipe.pid, "KILL") if pipe
    raise error || e
  end

  it 'starts a top level application' do
    with_server do
      assert_equal open("http://127.0.0.1:#{port}/app_file").read, app_file
    end
  end
end