diff options
| author | mo khan <mo@mokhan.ca> | 2013-08-28 07:20:13 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2013-08-28 07:20:13 -0600 |
| commit | db1191ec0e7305d684383f8974e2fa437f77ad5a (patch) | |
| tree | 5e27b197dff849d22d8e1f50eb75aa499b16bd06 /code/spyglass/test | |
Diffstat (limited to 'code/spyglass/test')
| -rw-r--r-- | code/spyglass/test/66613524f6c67942d7d19386f51c551cconfig.ru | 9 | ||||
| -rw-r--r-- | code/spyglass/test/basic_rack_test.rb | 15 | ||||
| -rw-r--r-- | code/spyglass/test/boots_test.rb | 13 | ||||
| -rw-r--r-- | code/spyglass/test/config.ru | 10 | ||||
| -rw-r--r-- | code/spyglass/test/helper.rb | 33 | ||||
| -rw-r--r-- | code/spyglass/test/log/.gitkeep | 0 | ||||
| -rw-r--r-- | code/spyglass/test/sinatra_test.rb | 25 | ||||
| -rw-r--r-- | code/spyglass/test/timeout_test.rb | 51 |
8 files changed, 156 insertions, 0 deletions
diff --git a/code/spyglass/test/66613524f6c67942d7d19386f51c551cconfig.ru b/code/spyglass/test/66613524f6c67942d7d19386f51c551cconfig.ru new file mode 100644 index 0000000..2aa2c98 --- /dev/null +++ b/code/spyglass/test/66613524f6c67942d7d19386f51c551cconfig.ru @@ -0,0 +1,9 @@ + require 'sinatra' + + get '/zing' do + redirect 'http://example.com' + end + + at_exit { File.unlink('/var/folders/0t/wmb8l4z555q1vy_n8wmg0jl00000gn/T/lifeline20130730-5833-ursook') rescue nil } + + run Sinatra::Application diff --git a/code/spyglass/test/basic_rack_test.rb b/code/spyglass/test/basic_rack_test.rb new file mode 100644 index 0000000..bf4c4ed --- /dev/null +++ b/code/spyglass/test/basic_rack_test.rb @@ -0,0 +1,15 @@ +require 'helper' +require 'excon' + +class BasicRackTest < MiniTest::Unit::TestCase + def setup + spyglass + end + + def test_it_responds + response = Excon.get("http://0.0.0.0:#{PORT}/fuzzy") + + assert_equal 200, response.status, "Didn't get the right response code" + assert_match /Hello world/, response.body, "Didn't get the right response body" + end +end diff --git a/code/spyglass/test/boots_test.rb b/code/spyglass/test/boots_test.rb new file mode 100644 index 0000000..e4b4945 --- /dev/null +++ b/code/spyglass/test/boots_test.rb @@ -0,0 +1,13 @@ +require 'helper' + +class BootTest < MiniTest::Unit::TestCase + def setup + spyglass + end + + def test_it_boots + # If this returns `nil` then our process is still running, meaning + # it hasn't crashed! + refute Process.waitpid(@pid, Process::WNOHANG) + end +end diff --git a/code/spyglass/test/config.ru b/code/spyglass/test/config.ru new file mode 100644 index 0000000..fbec204 --- /dev/null +++ b/code/spyglass/test/config.ru @@ -0,0 +1,10 @@ +require 'rack/lint' + +class HelloWorld + def call(env) + [200, {"Content-Type" => 'text/plain'}, ["Hello world!"]] + end +end + +use Rack::Lint +run HelloWorld.new diff --git a/code/spyglass/test/helper.rb b/code/spyglass/test/helper.rb new file mode 100644 index 0000000..64615cc --- /dev/null +++ b/code/spyglass/test/helper.rb @@ -0,0 +1,33 @@ +require 'bundler/setup' +require 'minitest/autorun' + +class MiniTest::Unit::TestCase + ROOT = File.dirname(__FILE__) + '/..' + TEST_CONFIG_RU = File.open("#{ROOT}/test/config.ru", 'r+') + PORT = 5656 + LOG = File.join('test', 'log', 'spyglass.log') + warn "Spyglass output can be found in #{LOG}" + + def spyglass(args = {}) + string_args = args.map { |key, value| "--#{key}=#{value}" } + config_ru = @ad_hoc_config_ru || TEST_CONFIG_RU + + cmd = "ruby -I\"#{ROOT}/lib\" -rubygems \"#{ROOT}/bin/spyglass\" --vverbose -c \"#{config_ru.path}\" -p#{PORT} #{string_args.join(' ')}" + @pid = Process.spawn(cmd, :err => :out, :out => LOG) + sleep 1.5 + end + + def config_ru(content) + # This ensures that we have a unique filename for each test run + filename = File.join(ROOT, 'test', Digest::MD5.hexdigest($PROGRAM_NAME) + 'config.ru') + @ad_hoc_config_ru = File.open(filename, 'w') + @ad_hoc_config_ru.write content + @ad_hoc_config_ru.flush + end + + def teardown + @pid && Process.kill(:QUIT, @pid) + @ad_hoc_config_ru && File.unlink(@ad_hoc_config_ru.path) + rescue Errno::ESRCH + end +end diff --git a/code/spyglass/test/log/.gitkeep b/code/spyglass/test/log/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/code/spyglass/test/log/.gitkeep diff --git a/code/spyglass/test/sinatra_test.rb b/code/spyglass/test/sinatra_test.rb new file mode 100644 index 0000000..0179413 --- /dev/null +++ b/code/spyglass/test/sinatra_test.rb @@ -0,0 +1,25 @@ +require 'helper' +require 'excon' + +class SinatraTest < MiniTest::Unit::TestCase + def setup + config_ru <<-RU + require 'sinatra' + + get '/zing' do + redirect 'http://example.com' + end + + run Sinatra::Application + RU + + spyglass + end + + def test_it_responds + response = Excon.get("http://0.0.0.0:#{PORT}/zing") + + assert_equal 302, response.status, "Didn't get the right response code" + assert_match /example/, response.headers['Location'], "Didn't get the right location header" + end +end diff --git a/code/spyglass/test/timeout_test.rb b/code/spyglass/test/timeout_test.rb new file mode 100644 index 0000000..9cbe134 --- /dev/null +++ b/code/spyglass/test/timeout_test.rb @@ -0,0 +1,51 @@ +require 'helper' +require 'excon' +require 'tempfile' + +class TimeoutTest < MiniTest::Unit::TestCase + def setup + @lifeline = Tempfile.open('lifeline') + @uri = "http://0.0.0.0:#{PORT}/zing" + + config_ru <<-RU + require 'sinatra' + + get '/zing' do + redirect 'http://example.com' + end + + at_exit { File.unlink('#{@lifeline.path}') rescue nil } + + run Sinatra::Application + RU + spyglass :timeout => 3 + end + + def test_times_out_after_timeout_has_expired + Excon.get(@uri) + sleep 3.5 + + # When the Master process loads the Sinatra it will set up the at_exit + # hook defined above. That process should exit after 3 seconds, removing + # our @lifeline file. If the file still exists at this point then the + # timeout didn't happen properly. + refute File.file?(@lifeline.path), "Spyglass didn't time out properly" + end + + def test_doesnt_time_out_if_requests_keep_coming + Excon.get(@uri) + sleep 1 + Excon.get(@uri) + sleep 1 + + Excon.get(@uri) + sleep 1.5 + Excon.get(@uri) + + # When the Master process loads the Sinatra it will set up the at_exit + # hook defined above. Since we're keeping the server saturated with + # requests, definitely not letting it sit idle for 3 seconds, the @lifeline + # file should still exist when we get here. + assert File.file?(@lifeline.path), "Spyglass didn't time out properly" + end +end |
