From ed7b19e50a7670d68df1aefee9f05bbba4dddf23 Mon Sep 17 00:00:00 2001 From: Ian Leitch Date: Wed, 23 Jan 2013 12:12:56 -0800 Subject: [PATCH] 1.8 support. --- Gemfile | 1 + Gemfile.lock | 5 +++++ Rakefile | 19 ++++++++++--------- lib/klomp.rb | 2 +- lib/klomp/connection.rb | 2 +- lib/klomp/frames.rb | 13 +++++++++---- spec/acceptance/acceptance_spec.rb | 2 +- spec/klomp/connection_spec.rb | 28 +++++++++++++++++----------- spec/klomp/frames_spec.rb | 2 +- spec/klomp/sentinel_spec.rb | 4 ++-- spec/klomp_spec.rb | 8 ++++---- spec/spec_helper.rb | 9 ++++++++- spec/support/have_received.rb | 2 +- 13 files changed, 61 insertions(+), 36 deletions(-) diff --git a/Gemfile b/Gemfile index f0ce42a..d88cd7f 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,7 @@ gem "rspec-given", "~>1.0", :group => [:development, :test] gem "simplecov", "~>0.6.0", :group => [:development, :test] gem "em-proxy", "~>0.1.0", :group => [:development, :test] gem "ci_reporter", "~>1.7.0", :group => [:development, :test] +gem "activesupport", "~>3.0", :group => [:development, :test] gem "hoe", "~>3.0", :group => [:development, :test] # vim: syntax=ruby diff --git a/Gemfile.lock b/Gemfile.lock index 642a0ee..ee41a80 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,9 @@ GEM remote: http://rubygems.org/ specs: ZenTest (4.8.0) + activesupport (3.2.11) + i18n (~> 0.6) + multi_json (~> 1.0) builder (3.1.4) ci_reporter (1.7.3) builder (>= 2.1.2) @@ -17,6 +20,7 @@ GEM hoe (>= 2.2.0) hoe-git (1.5.0) hoe (>= 2.2.0) + i18n (0.6.1) json (1.6.6) multi_json (1.3.6) rake (0.9.2.2) @@ -42,6 +46,7 @@ PLATFORMS DEPENDENCIES ZenTest (~> 4.8.0) + activesupport (~> 3.0) ci_reporter (~> 1.7.0) em-proxy (~> 0.1.0) hoe (~> 3.0) diff --git a/Rakefile b/Rakefile index 0657804..200f2f5 100644 --- a/Rakefile +++ b/Rakefile @@ -13,15 +13,16 @@ Hoe.spec 'klomp' do self.clean_globs << 'spec/reports' ### dependencies! - self.extra_dev_deps << [ 'hoe-bundler', '~> 1.1.0' ] - self.extra_dev_deps << [ 'hoe-gemspec', '~> 1.0.0' ] - self.extra_dev_deps << [ 'hoe-git', '~> 1.5.0' ] - self.extra_dev_deps << [ 'rspec', '~> 2.11.0' ] - self.extra_dev_deps << [ 'ZenTest', '~> 4.8.0' ] - self.extra_dev_deps << [ 'rspec-given', '~> 1.0' ] - self.extra_dev_deps << [ 'simplecov', '~> 0.6.0' ] - self.extra_dev_deps << [ 'em-proxy', '~> 0.1.0' ] - self.extra_dev_deps << [ 'ci_reporter', '~> 1.7.0' ] + self.extra_dev_deps << [ 'hoe-bundler', '~> 1.1.0' ] + self.extra_dev_deps << [ 'hoe-gemspec', '~> 1.0.0' ] + self.extra_dev_deps << [ 'hoe-git', '~> 1.5.0' ] + self.extra_dev_deps << [ 'rspec', '~> 2.11.0' ] + self.extra_dev_deps << [ 'ZenTest', '~> 4.8.0' ] + self.extra_dev_deps << [ 'rspec-given', '~> 1.0' ] + self.extra_dev_deps << [ 'simplecov', '~> 0.6.0' ] + self.extra_dev_deps << [ 'em-proxy', '~> 0.1.0' ] + self.extra_dev_deps << [ 'ci_reporter', '~> 1.7.0' ] + self.extra_dev_deps << [ 'activesupport', '~> 3.0' ] end require 'ci/reporter/rake/rspec' diff --git a/lib/klomp.rb b/lib/klomp.rb index d2a6dbd..72a032e 100644 --- a/lib/klomp.rb +++ b/lib/klomp.rb @@ -14,7 +14,7 @@ def initialize(servers, options = {}) def publish(queue, body, headers = {}) connections_remaining = connections.dup begin - conn = connections_remaining.sample + conn = connections_remaining[rand(connections_remaining.size)] conn.publish(queue, body, headers) rescue connections_remaining.delete conn diff --git a/lib/klomp/connection.rb b/lib/klomp/connection.rb index 581e448..f9658ba 100644 --- a/lib/klomp/connection.rb +++ b/lib/klomp/connection.rb @@ -84,7 +84,7 @@ def reconnect private def connect @socket = TCPSocket.new *options['server'] - @socket.set_encoding 'UTF-8' + @socket.set_encoding 'UTF-8' if @socket.respond_to?(:set_encoding) write Frames::Connect.new(options) frame = read Frames::Connected, @select_timeout log_frame frame if logger diff --git a/lib/klomp/frames.rb b/lib/klomp/frames.rb index 9b6d042..db96e05 100644 --- a/lib/klomp/frames.rb +++ b/lib/klomp/frames.rb @@ -5,9 +5,14 @@ module Frames class Frame def name; @name ||= self.class.name.split('::').last.upcase; end - def headers; @headers ||= {}; end - def [](key); headers[key]; end - def []=(key, value); headers[key] = value; end + def new_headers + # Dependency injection point for tests. + {} + end + + def headers; @headers ||= new_headers; end + def [](key); headers[key]; end + def []=(key, value); headers[key] = value; end def body; @body ||= ""; end def body=(b); @body = b; end @@ -44,7 +49,7 @@ def parse(data) def parse_headers(data) frame = nil - {}.tap do |headers| + new_headers.tap do |headers| data.lines.each do |line| next if line == "\n" unless frame diff --git a/spec/acceptance/acceptance_spec.rb b/spec/acceptance/acceptance_spec.rb index e0a80f4..dabe4aa 100644 --- a/spec/acceptance/acceptance_spec.rb +++ b/spec/acceptance/acceptance_spec.rb @@ -16,7 +16,7 @@ context "publish" do - When { klomp.publish "/queue/greeting", "hello" } + When { klomp.publish("/queue/greeting", "hello") } Then do vhosts = apollo_api_get_json "/broker/virtual-hosts.json" diff --git a/spec/klomp/connection_spec.rb b/spec/klomp/connection_spec.rb index eaa87bf..ba21346 100644 --- a/spec/klomp/connection_spec.rb +++ b/spec/klomp/connection_spec.rb @@ -4,18 +4,24 @@ Given(:data) { frame(:connected) } Given(:server) { "127.0.0.1:61613" } - Given(:options) { { "login" => "admin", "passcode" => "password", "logger" => logger } } - Given(:socket) { double(TCPSocket, gets:data, write:nil, set_encoding:nil, close:nil) } - Given(:logger) { double("Logger", error:nil, warn:nil, info:nil, debug:nil).as_null_object } - Given(:subscriber) { double "subscriber", call:nil } + Given(:socket) { double(TCPSocket, :gets => data, :write => nil, :set_encoding => nil, :close => nil) } + Given(:logger) { double("Logger", :error => nil, :warn => nil, :info => nil, :debug => nil).as_null_object } + Given(:subscriber) { double "subscriber", :call => nil } Given(:thread) { double Thread } - Given(:sentinel) { double Klomp::Sentinel, alive?:true } + Given(:sentinel) { double Klomp::Sentinel, :alive? => true } + Given(:options) { + hsh = ActiveSupport::OrderedHash.new + hsh['login'] = 'admin' + hsh['passcode'] = 'password' + hsh['logger'] = logger + hsh + } Given do IO.stub!(:select).and_return([[socket], [socket]]) TCPSocket.stub!(:new).and_return socket Thread.stub!(:new).and_return {|*args,&blk| thread.stub!(:block => blk); thread } - Klomp::Sentinel.stub!(new: sentinel) + Klomp::Sentinel.stub!(:new => sentinel) end context "new" do @@ -78,7 +84,7 @@ context "logs when logger level is debug" do - Given(:logger) { double("Logger").as_null_object.tap {|l| l.stub!(debug?: true) } } + Given(:logger) { double("Logger").as_null_object.tap {|l| l.stub!(:debug? => true) } } Then { logger.should have_received(:debug) } @@ -100,7 +106,7 @@ context "and logs when logger level is debug" do - Given(:logger) { double("Logger").as_null_object.tap {|l| l.stub!(debug?: true) } } + Given(:logger) { double("Logger").as_null_object.tap {|l| l.stub!(:debug? => true) } } Then { logger.should have_received(:debug) } @@ -112,7 +118,7 @@ When do connection.subscribe "/queue/greeting", subscriber - connection.subscribe "/queue/greeting", double("another subscriber that replaces the first", call:nil) + connection.subscribe "/queue/greeting", double("another subscriber that replaces the first", :call => nil) end Then { socket.should have_received(:write).with(frame(:subscribe)).once } @@ -225,7 +231,7 @@ context "disconnect" do - Given!(:connection) { Klomp::Connection.new server, options } + Given!(:connection) { Klomp::Connection.new server, options} When(:result) { connection.disconnect } @@ -293,7 +299,7 @@ Given!(:connection) { Klomp::Connection.new server, options } Given do - thread.stub!(:raise).and_return {|e| raise e } + thread.stub!(:raise).and_return {|e, _| raise e } socket.stub!(:gets).and_raise SystemCallError.new("some socket error") end diff --git a/spec/klomp/frames_spec.rb b/spec/klomp/frames_spec.rb index b105163..f377f02 100644 --- a/spec/klomp/frames_spec.rb +++ b/spec/klomp/frames_spec.rb @@ -22,7 +22,7 @@ context "stringifies all header keys and values" do - Given(:headers) { { timeout:42 } } + Given(:headers) { { :timeout => 42 } } When(:send_frame) { Klomp::Frames::Send.new("/queue/q", "", headers) } diff --git a/spec/klomp/sentinel_spec.rb b/spec/klomp/sentinel_spec.rb index 2f3e867..23a739f 100644 --- a/spec/klomp/sentinel_spec.rb +++ b/spec/klomp/sentinel_spec.rb @@ -2,7 +2,7 @@ describe Klomp::Sentinel do - Given(:connection) { double "Connection", connected?:false, reconnect:nil } + Given(:connection) { double "Connection", :connected? => false, :reconnect => nil } Given(:sentinel) { Klomp::Sentinel.new connection } Given(:thread) { double Thread } Given do @@ -11,7 +11,7 @@ context "does nothing if the connection is already connected" do - Given { connection.stub!(connected?: true) } + Given { connection.stub!(:connected? => true) } When { sentinel } diff --git a/spec/klomp_spec.rb b/spec/klomp_spec.rb index f7d7ac6..544bcd0 100644 --- a/spec/klomp_spec.rb +++ b/spec/klomp_spec.rb @@ -4,7 +4,7 @@ Given(:servers) { ["127.0.0.1:61613", "127.0.0.1:67673"] } Given(:connections) { Hash[*servers.map {|s| [s, double("connection #{s}")] }.flatten] } - Given { Klomp::Connection.stub!(:new).and_return {|s| connections[s] } } + Given { Klomp::Connection.stub!(:new).and_return {|s, _| connections[s] } } Given(:klomp) { Klomp.new servers } context "new" do @@ -113,7 +113,7 @@ context "calls subscribe on all of the servers" do - Given { connections.values.each {|conn| conn.stub!(subscribe: 42) } } + Given { connections.values.each {|conn| conn.stub!(:subscribe => 42) } } When(:result) { klomp.subscribe("/queue/greeting") { true } } @@ -143,7 +143,7 @@ context "calls unsubscribe on all the servers" do - Given { connections.values.each {|conn| conn.stub!(unsubscribe: 42) } } + Given { connections.values.each {|conn| conn.stub!(:unsubscribe => 42) } } When(:result) { klomp.unsubscribe("/queue/greeting") } @@ -197,7 +197,7 @@ context "disconnects all the servers" do - Given { connections.values.each {|conn| conn.stub!(disconnect: 42) } } + Given { connections.values.each {|conn| conn.stub!(:disconnect => 42) } } When(:result) { klomp.disconnect } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 919cd6b..e972b6e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,8 @@ -require 'simplecov' +require 'simplecov' if RUBY_VERSION >= '1.9' require 'klomp' require 'rspec' require 'rspec-given' +require 'active_support' Dir[File.expand_path('../support/', __FILE__) + '/*.rb'].each {|f| require f } @@ -32,5 +33,11 @@ def queue_available? config.filter_run_excluding :performance end + config.before :each do + Klomp::Frames::Frame.any_instance.stub(:new_headers) do + ActiveSupport::OrderedHash.new + end + end + config.include Frames end diff --git a/spec/support/have_received.rb b/spec/support/have_received.rb index 255890d..bde1577 100644 --- a/spec/support/have_received.rb +++ b/spec/support/have_received.rb @@ -54,7 +54,7 @@ class Matcher def initialize(message, expected_from) @message, @mock = message, RSpec::Mocks::Mock.new @mock.stub!(message) - @expectation = @mock.should_receive(message, expected_from: expected_from) + @expectation = @mock.should_receive(message, expected_from => expected_from) end def matches?(actual)