Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

1.8 support.

  • Loading branch information...
commit ed7b19e50a7670d68df1aefee9f05bbba4dddf23 1 parent 1b54126
authored
1  Gemfile
@@ -15,6 +15,7 @@ gem "rspec-given", "~>1.0", :group => [:development, :test]
15 15
 gem "simplecov", "~>0.6.0", :group => [:development, :test]
16 16
 gem "em-proxy", "~>0.1.0", :group => [:development, :test]
17 17
 gem "ci_reporter", "~>1.7.0", :group => [:development, :test]
  18
+gem "activesupport", "~>3.0", :group => [:development, :test]
18 19
 gem "hoe", "~>3.0", :group => [:development, :test]
19 20
 
20 21
 # vim: syntax=ruby
5  Gemfile.lock
@@ -2,6 +2,9 @@ GEM
2 2
   remote: http://rubygems.org/
3 3
   specs:
4 4
     ZenTest (4.8.0)
  5
+    activesupport (3.2.11)
  6
+      i18n (~> 0.6)
  7
+      multi_json (~> 1.0)
5 8
     builder (3.1.4)
6 9
     ci_reporter (1.7.3)
7 10
       builder (>= 2.1.2)
@@ -17,6 +20,7 @@ GEM
17 20
       hoe (>= 2.2.0)
18 21
     hoe-git (1.5.0)
19 22
       hoe (>= 2.2.0)
  23
+    i18n (0.6.1)
20 24
     json (1.6.6)
21 25
     multi_json (1.3.6)
22 26
     rake (0.9.2.2)
@@ -42,6 +46,7 @@ PLATFORMS
42 46
 
43 47
 DEPENDENCIES
44 48
   ZenTest (~> 4.8.0)
  49
+  activesupport (~> 3.0)
45 50
   ci_reporter (~> 1.7.0)
46 51
   em-proxy (~> 0.1.0)
47 52
   hoe (~> 3.0)
19  Rakefile
@@ -13,15 +13,16 @@ Hoe.spec 'klomp' do
13 13
   self.clean_globs << 'spec/reports'
14 14
 
15 15
   ### dependencies!
16  
-  self.extra_dev_deps << [ 'hoe-bundler',  '~> 1.1.0'  ]
17  
-  self.extra_dev_deps << [ 'hoe-gemspec',  '~> 1.0.0'  ]
18  
-  self.extra_dev_deps << [ 'hoe-git',      '~> 1.5.0'  ]
19  
-  self.extra_dev_deps << [ 'rspec',        '~> 2.11.0' ]
20  
-  self.extra_dev_deps << [ 'ZenTest',      '~> 4.8.0'  ]
21  
-  self.extra_dev_deps << [ 'rspec-given',  '~> 1.0'    ]
22  
-  self.extra_dev_deps << [ 'simplecov',    '~> 0.6.0'  ]
23  
-  self.extra_dev_deps << [ 'em-proxy',     '~> 0.1.0'  ]
24  
-  self.extra_dev_deps << [ 'ci_reporter',  '~> 1.7.0'  ]
  16
+  self.extra_dev_deps << [ 'hoe-bundler',   '~> 1.1.0'  ]
  17
+  self.extra_dev_deps << [ 'hoe-gemspec',   '~> 1.0.0'  ]
  18
+  self.extra_dev_deps << [ 'hoe-git',       '~> 1.5.0'  ]
  19
+  self.extra_dev_deps << [ 'rspec',         '~> 2.11.0' ]
  20
+  self.extra_dev_deps << [ 'ZenTest',       '~> 4.8.0'  ]
  21
+  self.extra_dev_deps << [ 'rspec-given',   '~> 1.0'    ]
  22
+  self.extra_dev_deps << [ 'simplecov',     '~> 0.6.0'  ]
  23
+  self.extra_dev_deps << [ 'em-proxy',      '~> 0.1.0'  ]
  24
+  self.extra_dev_deps << [ 'ci_reporter',   '~> 1.7.0'  ]
  25
+  self.extra_dev_deps << [ 'activesupport', '~> 3.0'    ]
25 26
 end
26 27
 
27 28
 require 'ci/reporter/rake/rspec'
2  lib/klomp.rb
@@ -14,7 +14,7 @@ def initialize(servers, options = {})
14 14
   def publish(queue, body, headers = {})
15 15
     connections_remaining = connections.dup
16 16
     begin
17  
-      conn = connections_remaining.sample
  17
+      conn = connections_remaining[rand(connections_remaining.size)]
18 18
       conn.publish(queue, body, headers)
19 19
     rescue
20 20
       connections_remaining.delete conn
2  lib/klomp/connection.rb
@@ -84,7 +84,7 @@ def reconnect
84 84
     private
85 85
     def connect
86 86
       @socket  = TCPSocket.new *options['server']
87  
-      @socket.set_encoding 'UTF-8'
  87
+      @socket.set_encoding 'UTF-8' if @socket.respond_to?(:set_encoding)
88 88
       write Frames::Connect.new(options)
89 89
       frame = read Frames::Connected, @select_timeout
90 90
       log_frame frame if logger
13  lib/klomp/frames.rb
@@ -5,9 +5,14 @@ module Frames
5 5
     class Frame
6 6
       def name; @name ||= self.class.name.split('::').last.upcase; end
7 7
 
8  
-      def headers;         @headers ||= {};       end
9  
-      def [](key);          headers[key];         end
10  
-      def []=(key, value);  headers[key] = value; end
  8
+      def new_headers
  9
+        # Dependency injection point for tests.
  10
+        {}
  11
+      end
  12
+
  13
+      def headers;          @headers ||= new_headers; end
  14
+      def [](key);          headers[key];             end
  15
+      def []=(key, value);  headers[key] = value;     end
11 16
 
12 17
       def body; @body ||= ""; end
13 18
       def body=(b); @body = b; end
@@ -44,7 +49,7 @@ def parse(data)
44 49
 
45 50
       def parse_headers(data)
46 51
         frame = nil
47  
-        {}.tap do |headers|
  52
+        new_headers.tap do |headers|
48 53
           data.lines.each do |line|
49 54
             next if line == "\n"
50 55
             unless frame
2  spec/acceptance/acceptance_spec.rb
@@ -16,7 +16,7 @@
16 16
 
17 17
   context "publish" do
18 18
 
19  
-    When { klomp.publish "/queue/greeting", "hello" }
  19
+    When { klomp.publish("/queue/greeting", "hello") }
20 20
 
21 21
     Then do
22 22
       vhosts = apollo_api_get_json "/broker/virtual-hosts.json"
28  spec/klomp/connection_spec.rb
@@ -4,18 +4,24 @@
4 4
 
5 5
   Given(:data)       { frame(:connected) }
6 6
   Given(:server)     { "127.0.0.1:61613" }
7  
-  Given(:options)    { { "login" => "admin", "passcode" => "password", "logger" => logger } }
8  
-  Given(:socket)     { double(TCPSocket, gets:data, write:nil, set_encoding:nil, close:nil) }
9  
-  Given(:logger)     { double("Logger", error:nil, warn:nil, info:nil, debug:nil).as_null_object }
10  
-  Given(:subscriber) { double "subscriber", call:nil }
  7
+  Given(:socket)     { double(TCPSocket, :gets => data, :write => nil, :set_encoding => nil, :close => nil) }
  8
+  Given(:logger)     { double("Logger", :error => nil, :warn => nil, :info => nil, :debug => nil).as_null_object }
  9
+  Given(:subscriber) { double "subscriber", :call => nil }
11 10
   Given(:thread)     { double Thread }
12  
-  Given(:sentinel)   { double Klomp::Sentinel, alive?:true }
  11
+  Given(:sentinel)   { double Klomp::Sentinel, :alive? => true }
  12
+  Given(:options)    {
  13
+    hsh = ActiveSupport::OrderedHash.new
  14
+    hsh['login']    = 'admin'
  15
+    hsh['passcode'] = 'password'
  16
+    hsh['logger']   = logger
  17
+    hsh
  18
+  }
13 19
 
14 20
   Given do
15 21
     IO.stub!(:select).and_return([[socket], [socket]])
16 22
     TCPSocket.stub!(:new).and_return socket
17 23
     Thread.stub!(:new).and_return {|*args,&blk| thread.stub!(:block => blk); thread }
18  
-    Klomp::Sentinel.stub!(new: sentinel)
  24
+    Klomp::Sentinel.stub!(:new => sentinel)
19 25
   end
20 26
 
21 27
   context "new" do
@@ -78,7 +84,7 @@
78 84
 
79 85
     context "logs when logger level is debug" do
80 86
 
81  
-      Given(:logger) { double("Logger").as_null_object.tap {|l| l.stub!(debug?: true) } }
  87
+      Given(:logger) { double("Logger").as_null_object.tap {|l| l.stub!(:debug? => true) } }
82 88
 
83 89
       Then { logger.should have_received(:debug) }
84 90
 
@@ -100,7 +106,7 @@
100 106
 
101 107
       context "and logs when logger level is debug" do
102 108
 
103  
-        Given(:logger) { double("Logger").as_null_object.tap {|l| l.stub!(debug?: true) } }
  109
+        Given(:logger) { double("Logger").as_null_object.tap {|l| l.stub!(:debug? => true) } }
104 110
 
105 111
         Then { logger.should have_received(:debug) }
106 112
 
@@ -112,7 +118,7 @@
112 118
 
113 119
       When do
114 120
         connection.subscribe "/queue/greeting", subscriber
115  
-        connection.subscribe "/queue/greeting", double("another subscriber that replaces the first", call:nil)
  121
+        connection.subscribe "/queue/greeting", double("another subscriber that replaces the first", :call => nil)
116 122
       end
117 123
 
118 124
       Then { socket.should have_received(:write).with(frame(:subscribe)).once }
@@ -225,7 +231,7 @@
225 231
 
226 232
   context "disconnect" do
227 233
 
228  
-    Given!(:connection) { Klomp::Connection.new server, options }
  234
+    Given!(:connection) { Klomp::Connection.new server, options}
229 235
 
230 236
     When(:result) { connection.disconnect }
231 237
 
@@ -293,7 +299,7 @@
293 299
     Given!(:connection) { Klomp::Connection.new server, options }
294 300
 
295 301
     Given do
296  
-      thread.stub!(:raise).and_return {|e| raise e }
  302
+      thread.stub!(:raise).and_return {|e, _| raise e }
297 303
       socket.stub!(:gets).and_raise SystemCallError.new("some socket error")
298 304
     end
299 305
 
2  spec/klomp/frames_spec.rb
@@ -22,7 +22,7 @@
22 22
 
23 23
   context "stringifies all header keys and values" do
24 24
 
25  
-    Given(:headers) { { timeout:42 } }
  25
+    Given(:headers) { { :timeout => 42 } }
26 26
 
27 27
     When(:send_frame) { Klomp::Frames::Send.new("/queue/q", "", headers) }
28 28
 
4  spec/klomp/sentinel_spec.rb
@@ -2,7 +2,7 @@
2 2
 
3 3
 describe Klomp::Sentinel do
4 4
 
5  
-  Given(:connection) { double "Connection", connected?:false, reconnect:nil }
  5
+  Given(:connection) { double "Connection", :connected? => false, :reconnect => nil }
6 6
   Given(:sentinel) { Klomp::Sentinel.new connection }
7 7
   Given(:thread) { double Thread }
8 8
   Given do
@@ -11,7 +11,7 @@
11 11
 
12 12
   context "does nothing if the connection is already connected" do
13 13
 
14  
-    Given { connection.stub!(connected?: true) }
  14
+    Given { connection.stub!(:connected? => true) }
15 15
 
16 16
     When { sentinel }
17 17
 
8  spec/klomp_spec.rb
@@ -4,7 +4,7 @@
4 4
 
5 5
   Given(:servers) { ["127.0.0.1:61613", "127.0.0.1:67673"] }
6 6
   Given(:connections) { Hash[*servers.map {|s| [s, double("connection #{s}")] }.flatten] }
7  
-  Given { Klomp::Connection.stub!(:new).and_return {|s| connections[s] } }
  7
+  Given { Klomp::Connection.stub!(:new).and_return {|s, _| connections[s] } }
8 8
   Given(:klomp) { Klomp.new servers }
9 9
 
10 10
   context "new" do
@@ -113,7 +113,7 @@
113 113
 
114 114
     context "calls subscribe on all of the servers" do
115 115
 
116  
-      Given { connections.values.each {|conn| conn.stub!(subscribe: 42) } }
  116
+      Given { connections.values.each {|conn| conn.stub!(:subscribe => 42) } }
117 117
 
118 118
       When(:result) { klomp.subscribe("/queue/greeting") { true } }
119 119
 
@@ -143,7 +143,7 @@
143 143
 
144 144
     context "calls unsubscribe on all the servers" do
145 145
 
146  
-      Given { connections.values.each {|conn| conn.stub!(unsubscribe: 42) } }
  146
+      Given { connections.values.each {|conn| conn.stub!(:unsubscribe => 42) } }
147 147
 
148 148
       When(:result) { klomp.unsubscribe("/queue/greeting") }
149 149
 
@@ -197,7 +197,7 @@
197 197
 
198 198
     context "disconnects all the servers" do
199 199
 
200  
-      Given { connections.values.each {|conn| conn.stub!(disconnect: 42) } }
  200
+      Given { connections.values.each {|conn| conn.stub!(:disconnect => 42) } }
201 201
 
202 202
       When(:result) { klomp.disconnect }
203 203
 
9  spec/spec_helper.rb
... ...
@@ -1,7 +1,8 @@
1  
-require 'simplecov'
  1
+require 'simplecov' if RUBY_VERSION >= '1.9'
2 2
 require 'klomp'
3 3
 require 'rspec'
4 4
 require 'rspec-given'
  5
+require 'active_support'
5 6
 
6 7
 Dir[File.expand_path('../support/', __FILE__) + '/*.rb'].each {|f| require f }
7 8
 
@@ -32,5 +33,11 @@ def queue_available?
32 33
     config.filter_run_excluding :performance
33 34
   end
34 35
 
  36
+  config.before :each do
  37
+    Klomp::Frames::Frame.any_instance.stub(:new_headers) do
  38
+      ActiveSupport::OrderedHash.new
  39
+    end
  40
+  end
  41
+
35 42
   config.include Frames
36 43
 end
2  spec/support/have_received.rb
@@ -54,7 +54,7 @@ class Matcher
54 54
     def initialize(message, expected_from)
55 55
       @message, @mock = message, RSpec::Mocks::Mock.new
56 56
       @mock.stub!(message)
57  
-      @expectation = @mock.should_receive(message, expected_from: expected_from)
  57
+      @expectation = @mock.should_receive(message, expected_from => expected_from)
58 58
     end
59 59
 
60 60
     def matches?(actual)

0 notes on commit ed7b19e

Please sign in to comment.
Something went wrong with that request. Please try again.