Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

tests

  • Loading branch information...
commit 059d59a39a41f13672f74c8794647067afe189ec 1 parent 7a0e6b5
Chris Continanza authored
8 Gemfile
@@ -2,3 +2,11 @@ source 'https://rubygems.org'
2 2
3 3 # Specify your gem's dependencies in rack-worker.gemspec
4 4 gemspec
  5 +
  6 +group :test do
  7 + gem 'rr'
  8 + gem 'turn'
  9 + gem 'webmock'
  10 + gem 'rack-test', :require => 'rack/test'
  11 + gem 'sinatra', :require => 'sinatra/base'
  12 +end
10 lib/rack/worker.rb
... ... @@ -1,4 +1,5 @@
1 1 require 'rack/worker/version'
  2 +require 'json'
2 3
3 4 module Rack
4 5 class Worker
@@ -40,8 +41,7 @@ def call(env)
40 41 else
41 42 unless cache.get("env-#{key}")
42 43 cache.set("env-#{key}", env.to_json)
43   - queue.enqueue("#{self.class.name}.process_request",
44   - @app.class.name, key)
  44 + queue.enqueue("#{self.class.name}.process_request", @app.name, key)
45 45 end
46 46 [202, {"Content-type" => "text/plain"}, []]
47 47 end
@@ -51,7 +51,7 @@ def call(env)
51 51 def self.process_request(classname, id)
52 52 env = cache.get("env-#{id}")
53 53 return unless env
54   - env = Yajl::Parser.parse(env)
  54 + env = JSON.parse(env)
55 55 app = classname_to_class(classname)
56 56 status, headers, body = app.call(env.merge('rack.worker_qc' => true))
57 57 set_response(id, status, headers, body)
@@ -68,11 +68,11 @@ def self.set_response(key, status, headers, body)
68 68 def get_response(key)
69 69 response = cache.get("response-#{key}")
70 70 return unless response
71   - Yajl::Parser.parse(response)
  71 + JSON.parse(response)
72 72 end
73 73
74 74 def key(env)
75   - env['REQUEST_PATH'] + '?' + env['QUERY_STRING']
  75 + (env['REQUEST_PATH'] || env['PATH_INFO']) + '?' + env['QUERY_STRING']
76 76 end
77 77 end
78 78 end
2  rack-worker.gemspec
@@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
14 14 gem.name = "rack-worker"
15 15 gem.require_paths = ["lib"]
16 16 gem.version = Rack::Worker::VERSION
  17 +
  18 + gem.add_dependency 'json'
17 19 end
14 test/test_helper.rb
... ... @@ -0,0 +1,14 @@
  1 +require 'bundler'
  2 +Bundler.require :test
  3 +require 'webmock/test_unit'
  4 +require "#{File.dirname(__FILE__)}/../lib/rack-worker"
  5 +
  6 +class Rack::Worker::TestCase < Test::Unit::TestCase
  7 + include Rack::Test::Methods
  8 + include RR::Adapters::TestUnit
  9 +
  10 + def teardown
  11 + super
  12 + WebMock.reset!
  13 + end
  14 +end
93 test/worker_test.rb
... ... @@ -0,0 +1,93 @@
  1 +require 'test_helper'
  2 +
  3 +module QueueTest
  4 + def test_processes_request_in_queue
  5 + Rack::Worker.cache = Object.new
  6 + mock(Rack::Worker.cache).get('response-/foo?') { false }
  7 + mock(Rack::Worker.cache).get('env-/foo?') { false }
  8 + mock(Rack::Worker.cache).get('env-/foo?') { {'rack.input' => []}.to_json }
  9 + mock(Rack::Worker.cache).set('env-/foo?', is_a(String))
  10 + mock(Rack::Worker.cache).set('response-/foo?', is_a(String))
  11 +
  12 + mock_queue = Object.new
  13 + def mock_queue.enqueue(function_call, *args)
  14 + eval("#{function_call} *args")
  15 + end
  16 + Rack::Worker.queue = mock_queue
  17 +
  18 + get '/foo'
  19 + assert_equal 202, last_response.status
  20 + end
  21 +end
  22 +
  23 +class WorkerTest < Rack::Worker::TestCase
  24 + include QueueTest
  25 + class TestClassApp
  26 + def self.call(env)
  27 + [200, {"Content-Type" => "text/test"}, ['Hello, world']]
  28 + end
  29 + end
  30 +
  31 + def app
  32 + Rack::Builder.new do
  33 + use Rack::Worker
  34 + run TestClassApp
  35 + end
  36 + end
  37 +
  38 + def test_returns_empty_202_and_queues_when_not_in_cache_or_queue
  39 + Rack::Worker.cache = Object.new
  40 + mock(Rack::Worker.cache).get('response-/foo?') { false }
  41 + mock(Rack::Worker.cache).get('env-/foo?') { false }
  42 + stub(Rack::Worker.cache).set
  43 +
  44 + Rack::Worker.queue = Object.new
  45 + mock(Rack::Worker.queue).enqueue('Rack::Worker.process_request', is_a(String), '/foo?')
  46 +
  47 + get '/foo'
  48 + assert_equal 202, last_response.status
  49 + assert_equal '', last_response.body
  50 + end
  51 +
  52 + def test_returns_empty_202_and_does_not_queue_when_not_in_cache_and_in_queue
  53 + Rack::Worker.cache = Object.new
  54 + mock(Rack::Worker.cache).get('response-/foo?') { false }
  55 + mock(Rack::Worker.cache).get('env-/foo?') { true }
  56 +
  57 + get '/foo'
  58 + assert_equal 202, last_response.status
  59 + assert_equal '', last_response.body
  60 + end
  61 +
  62 + def test_returns_response_in_queue
  63 + json = "{\"Hello\":\"World\"}"
  64 + Rack::Worker.cache = Object.new
  65 + mock(Rack::Worker.cache).get('response-/foo?') do
  66 + [302, {"Content-Type" => "application/json"}, [json]].to_json
  67 + end
  68 +
  69 + get '/foo'
  70 + assert_equal 302, last_response.status
  71 + assert_equal 'application/json', last_response.headers['Content-Type']
  72 + assert_equal json, last_response.body
  73 + end
  74 +
  75 +end
  76 +
  77 +class SinatraTest < Rack::Worker::TestCase
  78 + include QueueTest
  79 +
  80 + class TestClassApp < Sinatra::Base
  81 + get '*' do
  82 + headers 'Content-Type' => 'text/test'
  83 + 'Hello, world'
  84 + end
  85 + end
  86 +
  87 + def app
  88 + Rack::Builder.new do
  89 + use Rack::Worker
  90 + run TestClassApp
  91 + end
  92 + end
  93 +end

0 comments on commit 059d59a

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