From f1f89749a0f678045171bbaacb1231009d6d0d39 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Thu, 27 Jan 2011 11:18:28 +0000 Subject: [PATCH] added specs for AMQP::Failover::Fallback --- spec/unit/amqp/failover/fallback_helper.rb | 31 +++++++++++++ spec/unit/amqp/failover/fallback_spec.rb | 51 ++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 spec/unit/amqp/failover/fallback_helper.rb create mode 100644 spec/unit/amqp/failover/fallback_spec.rb diff --git a/spec/unit/amqp/failover/fallback_helper.rb b/spec/unit/amqp/failover/fallback_helper.rb new file mode 100644 index 0000000..a7ab4da --- /dev/null +++ b/spec/unit/amqp/failover/fallback_helper.rb @@ -0,0 +1,31 @@ +class FallbackHelper < AMQP::Failover::Fallback + + class << self + alias :real_start_monitoring :start_monitoring + def start_monitoring(*args, &block) + $called << :start_monitoring + real_start_monitoring(*args, &block) + end + end + + alias :real_initialize :initialize + def initialize(*args) + $called << :initialize + EM.start_server('127.0.0.1', 9999) if $start_count == 2 + $start_count += 1 + real_initialize(*args) + end + + alias :real_connection_completed :connection_completed + def connection_completed + $called << :connection_completed + real_connection_completed + end + + alias :real_close_connection :close_connection + def close_connection + $called << :close_connection + real_close_connection + end + +end \ No newline at end of file diff --git a/spec/unit/amqp/failover/fallback_spec.rb b/spec/unit/amqp/failover/fallback_spec.rb new file mode 100644 index 0000000..7a9ea1f --- /dev/null +++ b/spec/unit/amqp/failover/fallback_spec.rb @@ -0,0 +1,51 @@ +# encoding: utf-8 +$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + +require 'spec_helper' +require 'fallback_helper' + +describe AMQP::Failover::Fallback do + + before(:each) do + $called = [] + $start_count = 0 + @args = { :host => 'localhost', :port => 9999, :retry_interval => 0.01 } + end + + it "should initialize" do + EM.run { + EM.start_server('127.0.0.1', 9999) + @mon = FallbackHelper.monitor(@args) do + $called << :done_block + EM.stop_event_loop + end + } + $start_count.should == 1 + $called.should have(5).items + $called.uniq.should have(5).items + $called.should include(:start_monitoring) + $called.should include(:initialize) + $called.should include(:connection_completed) + $called.should include(:close_connection) + $called.should include(:done_block) + end + + it "should retry on error" do + EM.run { + @mon = FallbackHelper.monitor(@args) do + $called << :done_block + EM.stop_event_loop + end + } + $start_count.should >= 3 + $called.should have($start_count + 4).items + $called.uniq.should have(5).items + $called.should include(:start_monitoring) + $called.should include(:initialize) + $called.should include(:connection_completed) + $called.should include(:close_connection) + $called.should include(:done_block) + end + +end +