1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Add basic web testing, promote 'queues' set to core.

This commit is contained in:
Mike Perham 2012-03-10 13:07:19 -08:00
parent 65bf77217b
commit d2263cbfe1
6 changed files with 71 additions and 25 deletions

View file

@ -3,7 +3,6 @@ require 'redis'
require 'sidekiq/redis_connection'
require 'sidekiq/middleware/chain'
require 'sidekiq/middleware/client/resque_web_compatibility'
require 'sidekiq/middleware/client/unique_jobs'
module Sidekiq
@ -16,7 +15,6 @@ module Sidekiq
def self.default_middleware
Middleware::Chain.new do |m|
m.add Middleware::Client::UniqueJobs
m.add Middleware::Client::ResqueWebCompatibility
end
end
@ -44,7 +42,13 @@ module Sidekiq
pushed = false
Sidekiq.client_middleware.invoke(item, queue) do
Sidekiq.redis.rpush("queue:#{queue}", MultiJson.encode(item))
payload = MultiJson.encode(item)
Sidekiq.redis.with_connection do |conn|
conn.multi do
conn.sadd('queues', queue)
conn.rpush("queue:#{queue}", payload)
end
end
pushed = true
end
pushed

View file

@ -1,14 +0,0 @@
module Sidekiq
module Middleware
module Client
class ResqueWebCompatibility
def call(item, queue)
Sidekiq.redis.sadd('queues', queue)
yield
end
end
end
end
end

View file

@ -13,7 +13,6 @@ class TestClient < MiniTest::Unit::TestCase
Sidekiq.client_middleware.entries.clear
Sidekiq.client_middleware do |chain|
chain.add Sidekiq::Middleware::Client::UniqueJobs
chain.add Sidekiq::Middleware::Client::ResqueWebCompatibility
end
10.times { Sidekiq::Client.push('customqueue', 'class' => 'Foo', 'args' => [1, 2]) }
assert_equal 1, Sidekiq.redis.llen("queue:customqueue")

View file

@ -10,9 +10,8 @@ Sidekiq.hook_rails!
class TestExtensions < MiniTest::Unit::TestCase
describe 'sidekiq extensions' do
before do
Sidekiq.client_middleware.entries.clear
Sidekiq.instance_variable_set(:@redis, MiniTest::Mock.new)
@redis = Sidekiq.redis
Sidekiq.redis = { :url => 'redis://localhost/sidekiq_test' }
Sidekiq.redis.flushdb
end
class MyModel < ActiveRecord::Base
@ -22,9 +21,11 @@ class TestExtensions < MiniTest::Unit::TestCase
end
it 'allows delayed exection of ActiveRecord class methods' do
@redis.expect(:rpush, @redis, ['queue:default', "{\"class\":\"Sidekiq::Extensions::DelayedModel\",\"args\":[\"---\\n- !ruby/class 'TestExtensions::MyModel'\\n- :long_class_method\\n- []\\n\"]}"])
assert_equal [], Sidekiq::Client.registered_queues
assert_equal 0, Sidekiq.redis.llen('queue:default')
MyModel.delay.long_class_method
@redis.verify
assert_equal ['default'], Sidekiq::Client.registered_queues
assert_equal 1, Sidekiq.redis.llen('queue:default')
end
it 'allows delayed exection of ActiveRecord instance methods' do
@ -38,9 +39,11 @@ class TestExtensions < MiniTest::Unit::TestCase
end
it 'allows delayed delivery of ActionMailer mails' do
@redis.expect(:rpush, @redis, ['queue:default', "{\"class\":\"Sidekiq::Extensions::DelayedMailer\",\"args\":[\"---\\n- !ruby/class 'TestExtensions::UserMailer'\\n- :greetings\\n- - 1\\n - 2\\n\"]}"])
assert_equal [], Sidekiq::Client.registered_queues
assert_equal 0, Sidekiq.redis.llen('queue:default')
UserMailer.delay.greetings(1, 2)
@redis.verify
assert_equal ['default'], Sidekiq::Client.registered_queues
assert_equal 1, Sidekiq.redis.llen('queue:default')
end
end

View file

@ -1,10 +1,13 @@
require 'helper'
require 'sidekiq'
require 'sidekiq/worker'
require 'active_record'
require 'action_mailer'
require 'sidekiq/extensions/action_mailer'
require 'sidekiq/extensions/active_record'
Sidekiq.hook_rails!
class TestTesting < MiniTest::Unit::TestCase
describe 'sidekiq testing' do

51
test/test_web.rb Normal file
View file

@ -0,0 +1,51 @@
require 'helper'
require 'sidekiq'
require 'sidekiq/web'
require 'rack/test'
class TestWeb < MiniTest::Unit::TestCase
describe 'sidekiq web' do
include Rack::Test::Methods
def app
Sidekiq::Web
end
before do
Sidekiq.redis = { :url => 'redis://localhost/sidekiq_test' }
Sidekiq.redis.flushdb
end
class WebWorker
include Sidekiq::Worker
def perform(a, b)
a + b
end
end
it 'shows active queues' do
get '/'
assert_equal 200, last_response.status
assert_match last_response.body, /Sidekiq is down/
refute_match last_response.body, /default/
assert WebWorker.perform_async(1, 2)
get '/'
assert_equal 200, last_response.status
assert_match last_response.body, /Sidekiq is down/
assert_match last_response.body, /default/
refute_match last_response.body, /foo/
assert Sidekiq::Client.push(:foo, 'class' => WebWorker, 'args' => [1, 3])
get '/'
assert_equal 200, last_response.status
assert_match last_response.body, /Sidekiq is down/
assert_match last_response.body, /default/
assert_match last_response.body, /foo/
end
end
end