Skip to content

Commit

Permalink
1.8 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Leitch committed Jan 23, 2013
1 parent 1b54126 commit ed7b19e
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 36 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -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
5 changes: 5 additions & 0 deletions Gemfile.lock
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
19 changes: 10 additions & 9 deletions Rakefile
Expand Up @@ -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'
2 changes: 1 addition & 1 deletion lib/klomp.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/klomp/connection.rb
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions lib/klomp/frames.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance/acceptance_spec.rb
Expand Up @@ -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"
Expand Down
28 changes: 17 additions & 11 deletions spec/klomp/connection_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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) }

Expand All @@ -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) }

Expand All @@ -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 }
Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/klomp/frames_spec.rb
Expand Up @@ -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) }

Expand Down
4 changes: 2 additions & 2 deletions spec/klomp/sentinel_spec.rb
Expand Up @@ -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
Expand All @@ -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 }

Expand Down
8 changes: 4 additions & 4 deletions spec/klomp_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 } }

Expand Down Expand Up @@ -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") }

Expand Down Expand Up @@ -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 }

Expand Down
9 changes: 8 additions & 1 deletion 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 }

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/support/have_received.rb
Expand Up @@ -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)
Expand Down

0 comments on commit ed7b19e

Please sign in to comment.