Skip to content

Commit

Permalink
Allow connection to be set for mongo backend, refactor authentication…
Browse files Browse the repository at this point in the history
… for redis backend
  • Loading branch information
bkeepers committed Sep 22, 2011
1 parent f621a69 commit 07844c8
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/qu/backend/base.rb
Expand Up @@ -3,6 +3,7 @@
module Qu
module Backend
class Base
attr_accessor :connection

private

Expand Down
17 changes: 15 additions & 2 deletions lib/qu/backend/mongo.rb
Expand Up @@ -3,9 +3,22 @@
module Qu
module Backend
class Mongo < Base
def database
@database ||= ::Mongo::Connection.new.db('qu')
def connection
@connection ||= begin
uri = URI.parse(ENV['MONGOHQ_URL'].to_s)
database = uri.path.empty? ? 'qu' : uri.path[1..-1]
options = {}
if uri.password
options[:auths] = [{
'db_name' => database,
'username' => uri.user,
'password' => uri.password
}]
end
::Mongo::Connection.new(uri.host, uri.port, options).db(database)
end
end
alias_method :database, :connection

def clear(queue = nil)
queue ||= queues + ['failed']
Expand Down
6 changes: 3 additions & 3 deletions lib/qu/backend/redis.rb
Expand Up @@ -10,10 +10,10 @@ def initialize
self.namespace = :qu
end

def redis
@redis ||= Qu.connection ||
::Redis::Namespace.new(namespace, :redis => ::Redis.connect(:url => ENV['REDISTOGO_URL']))
def connection
@connection ||= ::Redis::Namespace.new(namespace, :redis => ::Redis.connect(:url => ENV['REDISTOGO_URL']))
end
alias_method :redis, :connection

def enqueue(klass, *args)
job = Job.new(SimpleUUID::UUID.new.to_guid, klass, args)
Expand Down
12 changes: 12 additions & 0 deletions lib/qu/backend/spec.rb
Expand Up @@ -242,4 +242,16 @@ def timeout(count = 0.1, &block)
subject.workers.first.id.should == worker.id
end
end

describe 'connection=' do
it 'should allow setting the connection' do
connection = mock('a connection')
subject.connection = connection
subject.connection.should == connection
end

it 'should provide a default connection' do
subject.connection.should_not be_nil
end
end
end
17 changes: 17 additions & 0 deletions spec/qu/backend/mongo_spec.rb
Expand Up @@ -3,4 +3,21 @@

describe Qu::Backend::Mongo do
it_should_behave_like 'a backend'

describe 'connection' do
it 'should default the qu database' do
subject.connection.should be_instance_of(Mongo::DB)
subject.connection.name.should == 'qu'
end

it 'should use MONGOHQ_URL from heroku' do
Mongo::Connection.any_instance.stub(:connect)
ENV['MONGOHQ_URL'] = 'mongodb://user:pw@host:10060/quspec'
subject.connection.name.should == 'quspec'
# debugger
subject.connection.connection.host_to_try.should == ['host', 10060]
subject.connection.connection.auths.should == [{'db_name' => 'quspec', 'username' => 'user', 'password' => 'pw'}]
end

end
end
19 changes: 7 additions & 12 deletions spec/qu/backend/redis_spec.rb
Expand Up @@ -27,27 +27,22 @@
end

describe 'connection' do
it 'should use the qu connection' do
Qu.connection = mock('a connection')
subject.redis.should == Qu.connection
end

it 'should create default connection if one not provided' do
subject.redis.client.host.should == '127.0.0.1'
subject.redis.client.port.should == 6379
subject.redis.namespace.should == :qu
subject.connection.client.host.should == '127.0.0.1'
subject.connection.client.port.should == 6379
subject.connection.namespace.should == :qu
end

it 'should use REDISTOGO_URL from heroku with namespace' do
ENV['REDISTOGO_URL'] = 'redis://0.0.0.0:9876'
subject.redis.client.host.should == '0.0.0.0'
subject.redis.client.port.should == 9876
subject.redis.namespace.should == :qu
subject.connection.client.host.should == '0.0.0.0'
subject.connection.client.port.should == 9876
subject.connection.namespace.should == :qu
end

it 'should allow customizing the namespace' do
subject.namespace = :foobar
subject.redis.namespace.should == :foobar
subject.connection.namespace.should == :foobar
end
end
end
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -5,7 +5,6 @@

RSpec.configure do |config|
config.before do
Qu.connection = nil
Qu.backend = mock('a backend', :reserve => nil, :failed => nil, :completed => nil,
:register_worker => nil, :unregister_worker => nil)
end
Expand Down

0 comments on commit 07844c8

Please sign in to comment.