diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7770aab..5088d4d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -37,6 +37,7 @@ easiest way to contribute. * [Jeremy Hinegardner](https://github.com/copiousfreetime) * [Kevin Barnes](https://github.com/vinbarnes) +* [Bruno Antunes](https://github.com/sardaukar) [GitHub Account]: https://github.com/signup/free "GitHub Signup" [GitHub Issues]: https://github.com/copiousfreetime/fixme/issues "Fixme Issues" diff --git a/lib/qup/adapter/redis.rb b/lib/qup/adapter/redis.rb index b42dafb..2d01ed5 100644 --- a/lib/qup/adapter/redis.rb +++ b/lib/qup/adapter/redis.rb @@ -15,6 +15,7 @@ class Redis < ::Qup::Adapter def initialize( uri, options = {} ) @uri = uri @options = options + @client = ::Redis.new host: @uri.host, port: @uri.port @closed = false end @@ -24,7 +25,7 @@ def initialize( uri, options = {} ) # # Returns a Qup::Queue def queue( name ) - Qup::Adapter::Redis::Queue.new( @uri, name ) + Qup::Adapter::Redis::Queue.new( @client, name ) end # Internal: Create a new Topic from this Adapter @@ -33,7 +34,7 @@ def queue( name ) # # Returns a Qup::Topic def topic( name ) - Qup::Adapter::Redis::Topic.new( @uri, name ) + Qup::Adapter::Redis::Topic.new( @client, name ) end # Internal: Close the Redis adapter diff --git a/lib/qup/adapter/redis/connection.rb b/lib/qup/adapter/redis/connection.rb index b0d214d..ac8265c 100644 --- a/lib/qup/adapter/redis/connection.rb +++ b/lib/qup/adapter/redis/connection.rb @@ -13,9 +13,8 @@ class Connection # name - the String name of the Connection # # Returns a new Connection. - def initialize( uri, name ) - @uri = uri - @client = Redis.new :host => @uri.host, :port => @uri.port + def initialize( client, name ) + @client = client @name = name end diff --git a/lib/qup/adapter/redis/queue.rb b/lib/qup/adapter/redis/queue.rb index 7ce3412..a8ce1dd 100644 --- a/lib/qup/adapter/redis/queue.rb +++ b/lib/qup/adapter/redis/queue.rb @@ -9,13 +9,13 @@ class Queue < Connection # Internal: create a new Queue # - # uri - the connection uri for the Redis Client + # client - the Redis client # name - the String name of the Queue # topic_name - (optional) the String name of a parent topic # # Returns a new Queue. - def initialize( uri, name, topic_name = nil ) - super uri, name + def initialize( client, name, topic_name = nil ) + super( client, name ) @topic_name = topic_name @open_messages = {} end diff --git a/lib/qup/adapter/redis/topic.rb b/lib/qup/adapter/redis/topic.rb index 3fdb061..3a3428e 100644 --- a/lib/qup/adapter/redis/topic.rb +++ b/lib/qup/adapter/redis/topic.rb @@ -29,7 +29,7 @@ def publisher def subscriber(name) subscriber_name = "#{@name}.#{name}" @client.sadd @name, subscriber_name - queue = ::Qup::Adapter::Redis::Queue.new(@uri, subscriber_name, @name) + queue = ::Qup::Adapter::Redis::Queue.new(@client, subscriber_name, @name) ::Qup::Subscriber.new( self, queue ) end @@ -77,7 +77,7 @@ def subscriber_names def subscribers subs = {} subscriber_names.each do |sname| - subs[sname] = ::Qup::Adapter::Redis::Queue.new(@uri, sname, @name) + subs[sname] = ::Qup::Adapter::Redis::Queue.new(@client, sname, @name) end return subs end diff --git a/spec/qup/adapter/redis/queue_spec.rb b/spec/qup/adapter/redis/queue_spec.rb index ce26135..1cb03a1 100644 --- a/spec/qup/adapter/redis/queue_spec.rb +++ b/spec/qup/adapter/redis/queue_spec.rb @@ -23,9 +23,9 @@ describe "#destroy" do it "removes its name from the parent topic's subscriber set" do - redis.smembers("parent").should be == ["test"] + expect( redis.smembers("parent") ).to eq ["test"] queue.destroy - redis.smembers("parent").should be == [] + expect( redis.smembers("parent") ).to eq [] end end diff --git a/spec/qup/adapter/redis/topic_spec.rb b/spec/qup/adapter/redis/topic_spec.rb index 80fbc03..3f9546a 100644 --- a/spec/qup/adapter/redis/topic_spec.rb +++ b/spec/qup/adapter/redis/topic_spec.rb @@ -21,9 +21,9 @@ end it "unregisters itself from the Topic when unsubscribed" do - lambda do + expect { subscriber.unsubscribe - end.should change(topic, :subscriber_count).by(-1) + }.to change(topic, :subscriber_count).by(-1) end end end diff --git a/spec/qup/adapter_spec.rb b/spec/qup/adapter_spec.rb index 332033e..e1b607e 100644 --- a/spec/qup/adapter_spec.rb +++ b/spec/qup/adapter_spec.rb @@ -6,7 +6,7 @@ class Qup::AdapterTest < Qup::Adapter describe 'Adapter Registration' do it 'registers an adapter' do - Qup::Adapters['quptest'].should eq Qup::AdapterTest + expect( Qup::Adapters['quptest'] ).to eq Qup::AdapterTest end end @@ -15,13 +15,17 @@ class Qup::AdapterTest < Qup::Adapter %w[ close closed? ].each do |method| it "##{method} kaboom!" do - lambda { api.send( method ) }.should raise_error( NotImplementedError, "please implement '#{method}'" ) + expect { + api.send( method ) + }.to raise_error( NotImplementedError, "please implement '#{method}'" ) end end %w[ queue topic ].each do |method| it "##{method} kaboom!" do - lambda { api.send( method, 'foo' ) }.should raise_error( NotImplementedError, "please implement '#{method}'" ) + expect { + api.send( method, 'foo' ) + }.to raise_error( NotImplementedError, "please implement '#{method}'" ) end end diff --git a/spec/qup/backoff_sleeper_sleeper_spec.rb b/spec/qup/backoff_sleeper_sleeper_spec.rb index c6c2b8d..cfb8cb8 100644 --- a/spec/qup/backoff_sleeper_sleeper_spec.rb +++ b/spec/qup/backoff_sleeper_sleeper_spec.rb @@ -2,43 +2,43 @@ module Qup describe BackoffSleeper do - before { Kernel.stub(:sleep) } + before { allow(Kernel).to receive_messages(sleep: nil) } describe "#length" do it "it returns the multiplier averaged with the multiplier multiplied by rand" do - Kernel.stub(:rand).with().and_return(0.5) + expect(Kernel).to receive(:rand).with(no_args).and_return(0.5) sleeper = BackoffSleeper.new - sleeper.stub(:multiplier => 1) + allow(sleeper).to receive_messages(multiplier: 1) - sleeper.length.should be == ((1 + (1 * 0.5)) / 2) + expect( sleeper.length ).to eq( (1 + (1 * 0.5) ) / 2) end end describe "#tick" do it "starts count at 0" do - subject.count.should be == 0 + expect( subject.count ).to eq 0 end it "increments count by 1 everytime it's called" do subject.tick - subject.count.should be == 1 + expect( subject.count ).to eq 1 subject.tick - subject.count.should be == 2 + expect( subject.count ).to eq 2 end it "sleeps for #length if length is > 0" do - Kernel.should_receive(:sleep).with(0.123) + expect( Kernel ).to receive(:sleep).with(0.123) - subject.stub(:length => 0.123) + allow( subject ).to receive_messages(length: 0.123) subject.tick end it "doesn't call sleep if the length is 0" do - Kernel.should_not_receive(:sleep).with(0) + expect( Kernel ).to_not receive(:sleep).with(0) - subject.stub(:length => 0) + allow( subject).to receive_messages(length: 0) subject.tick end end @@ -52,7 +52,7 @@ module Qup describe "#multiplier" do it "starts at 0" do - subject.multiplier.should be == 0 + expect( subject.multiplier ).to eq 0 end it "backs off exponentially" do @@ -61,12 +61,12 @@ module Qup subject.multiplier end - multipliers.should be == [0.01, 0.1, 1] + expect( multipliers ).to eq [0.01, 0.1, 1] end it "maxes out at the last value of MULTIPLIERS" do 100.times { subject.tick } - subject.multiplier.should == BackoffSleeper::MULTIPLIERS.last + expect( subject.multiplier ).to eq BackoffSleeper::MULTIPLIERS.last end end end diff --git a/spec/qup/batch_consumer_spec.rb b/spec/qup/batch_consumer_spec.rb index 0bd1e17..03790cd 100644 --- a/spec/qup/batch_consumer_spec.rb +++ b/spec/qup/batch_consumer_spec.rb @@ -23,7 +23,7 @@ def messages :session_options => session_options, :queue_uri => "maildir://#{Dir.mktmpdir}" }) - batch_consumer.session.options.should == session_options + expect( batch_consumer.session.options ).to eq session_options end end @@ -81,7 +81,7 @@ def process(*) }) batch_consumer.run - client.messages.should == ["A", "B"] + expect( client.messages ).to eq ["A", "B"] end it "returns when max_age is met" do @@ -112,7 +112,7 @@ def process(*) }) batch_consumer.run - client.messages.should == ["A"] + expect( client.messages ).to eq ["A"] end @@ -129,9 +129,9 @@ def process(*) :queue_name => queue_name }) - client.should_receive(:setup).once.ordered - client.should_receive(:process).once.ordered - client.should_receive(:teardown).once.ordered + expect( client ).to receive(:setup).once.ordered + expect( client ).to receive(:process).once.ordered + expect( client ).to receive(:teardown).once.ordered batch_consumer.run end diff --git a/spec/qup/consumer_spec.rb b/spec/qup/consumer_spec.rb index a8dab95..d57bc44 100644 --- a/spec/qup/consumer_spec.rb +++ b/spec/qup/consumer_spec.rb @@ -17,31 +17,31 @@ it "consumes an item from the queue" do msg = consumer.consume - msg.data.should eq 'consumption' + expect( msg.data ).to eq 'consumption' queue.acknowledge msg - queue.depth.should eq 0 + expect( queue.depth ).to eq 0 end it "acknowledges messages it has consumed" do msg = consumer.consume - msg.data.should eq 'consumption' - queue.depth.should eq 1 + expect( msg.data ).to eq 'consumption' + expect( queue.depth ).to eq 1 consumer.acknowledge( msg ) - queue.depth.should eq 0 + expect( queue.depth ).to eq 0 end it "consumes auto-acknowledges msgs in a block" do consumer.consume do |msg| - msg.data.should eq 'consumption' + expect( msg.data ).to eq 'consumption' end - queue.depth.should eq 0 + expect( queue.depth ).to eq 0 end it "knows how deep the consumer's queue is" do - consumer.depth.should eq 1 + expect( consumer.depth ).to eq 1 consumer.consume do |msg| - msg.data.should eq 'consumption' + expect( msg.data ).to eq 'consumption' end - queue.depth.should eq 0 + expect( queue.depth ).to eq 0 end end diff --git a/spec/qup/message_spec.rb b/spec/qup/message_spec.rb index d3d393a..092680c 100644 --- a/spec/qup/message_spec.rb +++ b/spec/qup/message_spec.rb @@ -4,10 +4,10 @@ let( :message ) { Qup::Message.new( "my unique key", "some data" ) } it "has a key" do - message.key.should == 'my unique key' + expect( message.key ).to eq 'my unique key' end it 'has data' do - message.data.should == 'some data' + expect( message.data ).to eq 'some data' end end diff --git a/spec/qup/producer_spec.rb b/spec/qup/producer_spec.rb index 0b2d545..e704a1b 100644 --- a/spec/qup/producer_spec.rb +++ b/spec/qup/producer_spec.rb @@ -11,8 +11,8 @@ end it "produces items onto the queue" do - queue.depth.should eq 0 + expect( queue.depth ).to eq 0 producer.produce( 'production' ) - queue.depth.should eq 1 + expect( queue.depth ).to eq 1 end end diff --git a/spec/qup/queue_api_spec.rb b/spec/qup/queue_api_spec.rb index 08778ab..26e5e31 100644 --- a/spec/qup/queue_api_spec.rb +++ b/spec/qup/queue_api_spec.rb @@ -9,13 +9,17 @@ class Qup::QueueAPITest %w[ name depth flush destroy consume ].each do |method| it "##{method} kaboom!" do - lambda { api.send( method ) }.should raise_error( NotImplementedError, "please implement '#{method}'" ) + expect { + api.send( method ) + }.to raise_error( NotImplementedError, "please implement '#{method}'" ) end end %w[ produce acknowledge ].each do |method| it "##{method} kaboom!" do - lambda { api.send( method, nil ) }.should raise_error( NotImplementedError, "please implement '#{method}'" ) + expect { + api.send( method, nil ) + }.to raise_error( NotImplementedError, "please implement '#{method}'" ) end end end diff --git a/spec/qup/session_spec.rb b/spec/qup/session_spec.rb index 4fc4725..dfa87fa 100644 --- a/spec/qup/session_spec.rb +++ b/spec/qup/session_spec.rb @@ -11,52 +11,54 @@ end it "has a uri" do - session.uri.to_s.should == "maildir:#{path}" + expect( session.uri.to_s ).to eq "maildir:#{path}" end it 'can be closed' do - session.closed?.should be_false + expect( session.closed? ).to be_falsey session.close - session.closed?.should be_true + expect( session.closed? ).to be_truthy end describe '#open' do it 'returns a new session' do s = Qup::Session.open( uri ) - s.closed?.should be_false + expect( s.closed? ).to be_falsey end it 'yields a new session' do Qup::Session.open( uri ) do |s| - s.closed?.should be_false + expect( s.closed? ).to be_falsey end end it 'closes a session at the end of the block' do save_s = nil Qup::Session.open( uri ) do |s| - s.closed?.should be_false + expect( s.closed? ).to be_falsey save_s = s end - save_s.closed?.should be_true + expect( save_s.closed? ).to be_truthy end end describe '#queue' do it "can return a Queue" do q = session.queue( 'foo' ) - q.name.should == 'foo' + expect( q.name ).to eq 'foo' end it 'can yield a Queue' do session.queue( 'foo' ) do |q| - q.name.should == 'foo' + expect( q.name ).to eq 'foo' end end it 'raises an error if accessing a closed Session' do session.close - lambda { session.queue( 'boom' ) }.should raise_error( Qup::Session::ClosedError, /Session (.*) is closed/ ) + expect { + session.queue( 'boom' ) + }.to raise_error( Qup::Session::ClosedError, /Session (.*) is closed/ ) end end @@ -64,25 +66,27 @@ describe '#topic' do it "can return a Topic" do t = session.topic('t') - t.name.should == 't' + expect( t.name ).to eq 't' end it 'can yiled a Topic' do session.topic('t') do |t| - t.name.should == 't' + expect( t.name ).to eq 't' end end it 'raises an error if accessing a closed Session' do session.close - lambda { session.topic( 'boom' ) }.should raise_error( Qup::Session::ClosedError, /Session (.*) is closed/ ) + expect { + session.topic( 'boom' ) + }.to raise_error( Qup::Session::ClosedError, /Session (.*) is closed/ ) end end describe '#options' do it "holds the options that are used to initialize the session" do - s = Qup::Session.open( uri, { :the => 'Option' } ) - s.options[:the].should == 'Option' + s = Qup::Session.open( uri, { the: 'Option' } ) + expect( s.options[:the] ).to eq 'Option' end end end diff --git a/spec/qup/shared_adapter_examples.rb b/spec/qup/shared_adapter_examples.rb index b033553..3d7f64d 100644 --- a/spec/qup/shared_adapter_examples.rb +++ b/spec/qup/shared_adapter_examples.rb @@ -6,26 +6,26 @@ # shared_examples Qup::Adapter do it 'is registered as an adapter' do - Qup::Adapters[uri.scheme].should eq adapter.class + expect( Qup::Adapters[uri.scheme] ).to eq adapter.class end it 'can be closed' do - adapter.closed?.should be_false + expect( adapter.closed? ).to be_falsey adapter.close - adapter.closed?.should be_true + expect( adapter.closed? ).to be_truthy end it 'can create a QueueAPI-like object' do q = adapter.queue( 'q' ) - q.should be_kind_of( Qup::QueueAPI ) - q.name.should eq 'q' + expect( q ).to be_kind_of( Qup::QueueAPI ) + expect( q.name ).to eq 'q' q.destroy end it 'can create a QueueAPI-like object' do t = adapter.topic( 't' ) - t.should be_kind_of( Qup::TopicAPI ) - t.name.should eq 't' + expect( t ).to be_kind_of( Qup::TopicAPI ) + expect( t.name ).to eq 't' t.destroy end end diff --git a/spec/qup/shared_queue_examples.rb b/spec/qup/shared_queue_examples.rb index d576a54..282e230 100644 --- a/spec/qup/shared_queue_examples.rb +++ b/spec/qup/shared_queue_examples.rb @@ -17,67 +17,67 @@ shared_examples Qup::QueueAPI do it "has a name" do - queue.name.should eq 'foo' + expect( queue.name ).to eq 'foo' end describe "#produce" do it "produces an item on the queue" do - queue.depth.should eq 0 + expect( queue.depth ).to eq 0 queue.produce( "a new message" ) - queue.depth.should eq 1 + expect( queue.depth ).to eq 1 end it "does not create multiple messages for newlines" do queue.produce( "one\nsingle\nmessage" ) - queue.depth.should eq 1 + expect( queue.depth ).to eq 1 end end it "#flush" do 10.times { |x| queue.produce( "message #{x}" ) } - queue.depth.should eq 10 + expect(queue.depth).to eq 10 queue.flush - queue.depth.should eq 0 + expect(queue.depth).to eq 0 end describe '#consume' do before do queue.produce( "consumeable message" ) - queue.depth.should eq 1 + expect(queue.depth).to eq 1 end it 'normally' do msg = queue.consume - msg.data.should eq "consumeable message" + expect( msg.data ).to eq "consumeable message" end it 'with block it auto acknowledges' do queue.consume do |msg| - msg.data.should eq 'consumeable message' + expect( msg.data ).to eq 'consumeable message' end end it 'returns nil if the queue is empty (it is non-blocking)' do queue.consume - queue.consume.should == nil + expect( queue.consume).to be_nil end end describe "#acknowledge" do it "acks a message" do queue.produce( "acknowledgeable message" ) - queue.depth.should eq 1 + expect( queue.depth ).to eq 1 msg = queue.consume - msg.data.should eq "acknowledgeable message" + expect( msg.data ).to eq "acknowledgeable message" queue.acknowledge( msg ) - queue.depth.should eq 0 + expect( queue.depth ).to eq 0 end it "raises an error if you attempt to to acknowledge an unconsumed message" do msg = queue.produce( 'unconsumed' ) - lambda { queue.acknowledge( msg ) }.should raise_error(Qup::Error) + expect { queue.acknowledge( msg ) }.to raise_error(Qup::Error) end end end diff --git a/spec/qup/shared_topic_examples.rb b/spec/qup/shared_topic_examples.rb index c678489..d5edd7a 100644 --- a/spec/qup/shared_topic_examples.rb +++ b/spec/qup/shared_topic_examples.rb @@ -19,12 +19,12 @@ shared_examples Qup::TopicAPI do it "has a name" do - @topic.name.should == 'topic' + expect( @topic.name ).to eq 'topic' end it "creates publisher" do p = @topic.publisher - p.topic.should eq @topic + expect( p.topic ).to eq @topic end @@ -40,19 +40,19 @@ after do @topic2.destroy - @topic.subscriber_count.should eq 0 + expect( @topic.subscriber_count ).to eq 0 end it "updates the publisher with the number of subscribers" do start_count = @topic.subscriber_count - @topic2.subscriber_count.should eq start_count + expect( @topic2.subscriber_count ).to eq start_count current_count = start_count 3.times do |x| current_count += 1 @topic2.subscriber( "sub2-#{x}" ) - @topic.subscriber_count.should eq current_count - @topic2.subscriber_count.should eq current_count + expect( @topic.subscriber_count ).to eq current_count + expect( @topic2.subscriber_count ).to eq current_count end end @@ -62,7 +62,7 @@ @subs.each do |sub| msg = sub.consume - msg.data.should eq 'hi all' + expect( msg.data ).to eq 'hi all' end end @@ -70,7 +70,7 @@ p = @topic.publisher p.publish( "one\nsingle\nmessage" ) @subs.each do |sub| - sub.depth.should eq 1 + expect( sub.depth ).to eq 1 end end end diff --git a/spec/qup/topic_api_spec.rb b/spec/qup/topic_api_spec.rb index 94d7bbf..0a42015 100644 --- a/spec/qup/topic_api_spec.rb +++ b/spec/qup/topic_api_spec.rb @@ -9,13 +9,17 @@ class Qup::TopicAPITest %w[ name destroy publisher subscriber_count ].each do |method| it "##{method} kaboom!" do - lambda { api.send( method ) }.should raise_error( NotImplementedError, /please implement '#{method}'/ ) + expect { + api.send( method ) + }.to raise_error( NotImplementedError, /please implement '#{method}'/ ) end end %w[ publish subscriber ].each do |method| it "##{method} kaboom!" do - lambda { api.send( method, nil ) }.should raise_error( NotImplementedError, /please implement '#{method}'/ ) + expect { + api.send( method, nil ) + }.to raise_error( NotImplementedError, /please implement '#{method}'/ ) end end end diff --git a/spec/qup_spec.rb b/spec/qup_spec.rb index 9184c56..eb829f3 100644 --- a/spec/qup_spec.rb +++ b/spec/qup_spec.rb @@ -6,28 +6,28 @@ let( :uri ) { "maildir://#{path}" } it "should have a version" do - Qup::VERSION.should =~ ( %r[\A\d+\.\d+\.\d+\Z] ) + expect( Qup::VERSION ).to match( %r[\A\d+\.\d+\.\d+\Z] ) end describe '#open' do it 'returns a new session' do s = Qup.open( uri ) - s.closed?.should be_false + expect( s.closed? ).to be_falsey end it 'yields a new session' do Qup.open( uri ) do |s| - s.closed?.should be_false + expect( s.closed? ).to be_falsey end end it 'closes a session at the end of the block' do save_s = nil Qup.open( uri ) do |s| - s.closed?.should be_false + expect( s.closed? ).to be_falsey save_s = s end - save_s.closed?.should be_true + expect( save_s.closed? ).to be_truthy end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 552a254..40415fa 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,7 +3,6 @@ SimpleCov.start if ENV['COVERAGE'] end -require "rspec/autorun" require 'qup' require 'tmpdir'