Skip to content
Browse files

tests

  • Loading branch information...
1 parent 7a0e6b5 commit 059d59a39a41f13672f74c8794647067afe189ec @csquared committed Apr 12, 2012
Showing with 122 additions and 5 deletions.
  1. +8 −0 Gemfile
  2. +5 −5 lib/rack/worker.rb
  3. +2 −0 rack-worker.gemspec
  4. +14 −0 test/test_helper.rb
  5. +93 −0 test/worker_test.rb
View
8 Gemfile
@@ -2,3 +2,11 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in rack-worker.gemspec
gemspec
+
+group :test do
+ gem 'rr'
+ gem 'turn'
+ gem 'webmock'
+ gem 'rack-test', :require => 'rack/test'
+ gem 'sinatra', :require => 'sinatra/base'
+end
View
10 lib/rack/worker.rb
@@ -1,4 +1,5 @@
require 'rack/worker/version'
+require 'json'
module Rack
class Worker
@@ -40,8 +41,7 @@ def call(env)
else
unless cache.get("env-#{key}")
cache.set("env-#{key}", env.to_json)
- queue.enqueue("#{self.class.name}.process_request",
- @app.class.name, key)
+ queue.enqueue("#{self.class.name}.process_request", @app.name, key)
end
[202, {"Content-type" => "text/plain"}, []]
end
@@ -51,7 +51,7 @@ def call(env)
def self.process_request(classname, id)
env = cache.get("env-#{id}")
return unless env
- env = Yajl::Parser.parse(env)
+ env = JSON.parse(env)
app = classname_to_class(classname)
status, headers, body = app.call(env.merge('rack.worker_qc' => true))
set_response(id, status, headers, body)
@@ -68,11 +68,11 @@ def self.set_response(key, status, headers, body)
def get_response(key)
response = cache.get("response-#{key}")
return unless response
- Yajl::Parser.parse(response)
+ JSON.parse(response)
end
def key(env)
- env['REQUEST_PATH'] + '?' + env['QUERY_STRING']
+ (env['REQUEST_PATH'] || env['PATH_INFO']) + '?' + env['QUERY_STRING']
end
end
end
View
2 rack-worker.gemspec
@@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
gem.name = "rack-worker"
gem.require_paths = ["lib"]
gem.version = Rack::Worker::VERSION
+
+ gem.add_dependency 'json'
end
View
14 test/test_helper.rb
@@ -0,0 +1,14 @@
+require 'bundler'
+Bundler.require :test
+require 'webmock/test_unit'
+require "#{File.dirname(__FILE__)}/../lib/rack-worker"
+
+class Rack::Worker::TestCase < Test::Unit::TestCase
+ include Rack::Test::Methods
+ include RR::Adapters::TestUnit
+
+ def teardown
+ super
+ WebMock.reset!
+ end
+end
View
93 test/worker_test.rb
@@ -0,0 +1,93 @@
+require 'test_helper'
+
+module QueueTest
+ def test_processes_request_in_queue
+ Rack::Worker.cache = Object.new
+ mock(Rack::Worker.cache).get('response-/foo?') { false }
+ mock(Rack::Worker.cache).get('env-/foo?') { false }
+ mock(Rack::Worker.cache).get('env-/foo?') { {'rack.input' => []}.to_json }
+ mock(Rack::Worker.cache).set('env-/foo?', is_a(String))
+ mock(Rack::Worker.cache).set('response-/foo?', is_a(String))
+
+ mock_queue = Object.new
+ def mock_queue.enqueue(function_call, *args)
+ eval("#{function_call} *args")
+ end
+ Rack::Worker.queue = mock_queue
+
+ get '/foo'
+ assert_equal 202, last_response.status
+ end
+end
+
+class WorkerTest < Rack::Worker::TestCase
+ include QueueTest
+ class TestClassApp
+ def self.call(env)
+ [200, {"Content-Type" => "text/test"}, ['Hello, world']]
+ end
+ end
+
+ def app
+ Rack::Builder.new do
+ use Rack::Worker
+ run TestClassApp
+ end
+ end
+
+ def test_returns_empty_202_and_queues_when_not_in_cache_or_queue
+ Rack::Worker.cache = Object.new
+ mock(Rack::Worker.cache).get('response-/foo?') { false }
+ mock(Rack::Worker.cache).get('env-/foo?') { false }
+ stub(Rack::Worker.cache).set
+
+ Rack::Worker.queue = Object.new
+ mock(Rack::Worker.queue).enqueue('Rack::Worker.process_request', is_a(String), '/foo?')
+
+ get '/foo'
+ assert_equal 202, last_response.status
+ assert_equal '', last_response.body
+ end
+
+ def test_returns_empty_202_and_does_not_queue_when_not_in_cache_and_in_queue
+ Rack::Worker.cache = Object.new
+ mock(Rack::Worker.cache).get('response-/foo?') { false }
+ mock(Rack::Worker.cache).get('env-/foo?') { true }
+
+ get '/foo'
+ assert_equal 202, last_response.status
+ assert_equal '', last_response.body
+ end
+
+ def test_returns_response_in_queue
+ json = "{\"Hello\":\"World\"}"
+ Rack::Worker.cache = Object.new
+ mock(Rack::Worker.cache).get('response-/foo?') do
+ [302, {"Content-Type" => "application/json"}, [json]].to_json
+ end
+
+ get '/foo'
+ assert_equal 302, last_response.status
+ assert_equal 'application/json', last_response.headers['Content-Type']
+ assert_equal json, last_response.body
+ end
+
+end
+
+class SinatraTest < Rack::Worker::TestCase
+ include QueueTest
+
+ class TestClassApp < Sinatra::Base
+ get '*' do
+ headers 'Content-Type' => 'text/test'
+ 'Hello, world'
+ end
+ end
+
+ def app
+ Rack::Builder.new do
+ use Rack::Worker
+ run TestClassApp
+ end
+ end
+end

0 comments on commit 059d59a

Please sign in to comment.
Something went wrong with that request. Please try again.