mirror of
https://github.com/jimeh/amqp-failover.git
synced 2026-02-19 10:56:44 +00:00
lots of updates, and all specs PASS!! ^_^
This commit is contained in:
67
spec/unit/amqp/failover/config_spec.rb
Normal file
67
spec/unit/amqp/failover/config_spec.rb
Normal file
@@ -0,0 +1,67 @@
|
||||
# encoding: utf-8
|
||||
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe 'AMQP::Failover::Config' do
|
||||
|
||||
before(:each) do
|
||||
configs = [
|
||||
{:host => 'rabbit0.local'},
|
||||
{:host => 'rabbit1.local'},
|
||||
{:host => 'rabbit2.local', :port => 5673}
|
||||
]
|
||||
@configs = configs.map { |conf| AMQP.settings.merge(conf) }
|
||||
@fail = AMQP::Failover.new(@configs)
|
||||
end
|
||||
|
||||
it "should initialize" do
|
||||
fail = AMQP::Failover::Config.new(@configs[0])
|
||||
fail.should == @configs[0]
|
||||
fail.last_fail.should be_nil
|
||||
|
||||
now = Time.now
|
||||
fail = AMQP::Failover::Config.new(@configs[1], now)
|
||||
fail.should == @configs[1]
|
||||
fail.last_fail.should == now
|
||||
end
|
||||
|
||||
it "should order properly with #<=>" do
|
||||
one_hour_ago = (Time.now - 3600)
|
||||
two_hours_ago = (Time.now - 7200)
|
||||
|
||||
fail = [ AMQP::Failover::Config.new(@configs[0]),
|
||||
AMQP::Failover::Config.new(@configs[1], one_hour_ago),
|
||||
AMQP::Failover::Config.new(@configs[2], two_hours_ago) ]
|
||||
|
||||
(fail[1] <=> fail[0]).should == -1
|
||||
(fail[0] <=> fail[0]).should == 0
|
||||
(fail[0] <=> fail[1]).should == 1
|
||||
|
||||
(fail[1] <=> fail[2]).should == -1
|
||||
(fail[1] <=> fail[1]).should == 0
|
||||
(fail[2] <=> fail[1]).should == 1
|
||||
|
||||
fail.sort[0].last_fail.should == one_hour_ago
|
||||
fail.sort[1].last_fail.should == two_hours_ago
|
||||
fail.sort[2].last_fail.should == nil
|
||||
end
|
||||
|
||||
it "should be ordered by last_fail" do
|
||||
result = [ AMQP::Failover::Config.new(@configs[1], (Time.now - 60)),
|
||||
AMQP::Failover::Config.new(@configs[2], (Time.now - (60*25))),
|
||||
AMQP::Failover::Config.new(@configs[0], (Time.now - 3600)) ]
|
||||
|
||||
origin = [ AMQP::Failover::Config.new(@configs[0], (Time.now - 3600)),
|
||||
AMQP::Failover::Config.new(@configs[1], (Time.now - 60)),
|
||||
AMQP::Failover::Config.new(@configs[2], (Time.now - (60*25))) ]
|
||||
origin.sort.should == result
|
||||
|
||||
origin = [ AMQP::Failover::Config.new(@configs[0]),
|
||||
AMQP::Failover::Config.new(@configs[1], (Time.now - 60)),
|
||||
AMQP::Failover::Config.new(@configs[2], (Time.now - (60*25))) ]
|
||||
origin.sort.should == result
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -3,20 +3,28 @@ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe 'AMQP::Failover::Configs' do
|
||||
describe 'AMQP::Failover::Configurations' do
|
||||
|
||||
before(:each) do
|
||||
@conf = AMQP::Failover::Configs.new
|
||||
@conf = AMQP::Failover::Configurations.new
|
||||
@raw_configs = [
|
||||
{:host => 'rabbit3.local'},
|
||||
{:host => 'rabbit2.local'},
|
||||
{:host => 'rabbit0.local'},
|
||||
{:host => 'rabbit1.local'},
|
||||
{:host => 'rabbit2.local', :port => 5673}
|
||||
]
|
||||
@configs = @raw_configs.map { |conf| AMQP.settings.merge(conf) }
|
||||
end
|
||||
|
||||
it "should initialize" do
|
||||
confs = AMQP::Failover::Configurations.new(@raw_configs)
|
||||
confs.each_with_index do |conf, i|
|
||||
conf.should be_a(AMQP::Failover::Config)
|
||||
conf.should == @configs[i]
|
||||
end
|
||||
end
|
||||
|
||||
it "should set and get configs" do
|
||||
@conf.primary.should == 0
|
||||
@conf.primary_ref.should == 0
|
||||
@conf.should have(0).items
|
||||
|
||||
@conf.set(@raw_configs[0])
|
||||
@@ -29,6 +37,7 @@ describe 'AMQP::Failover::Configs' do
|
||||
@conf.get(1).should == @configs[1]
|
||||
@conf[1].should == @configs[1]
|
||||
|
||||
# should just create a ref, as config exists
|
||||
@conf.set(@raw_configs[1], :the_one)
|
||||
@conf.should have(2).items
|
||||
@conf.get(1).should == @configs[1]
|
||||
@@ -36,9 +45,9 @@ describe 'AMQP::Failover::Configs' do
|
||||
|
||||
@conf.load_array(@raw_configs)
|
||||
@conf.should have(3).items
|
||||
@conf.get_primary.should == @configs[0]
|
||||
@conf.primary = 1
|
||||
@conf.get_primary.should == @configs[1]
|
||||
@conf.primary.should == @configs[0]
|
||||
@conf.primary_ref = 1
|
||||
@conf.primary.should == @configs[1]
|
||||
@conf[:primary].should == @configs[1]
|
||||
end
|
||||
|
||||
@@ -50,5 +59,21 @@ describe 'AMQP::Failover::Configs' do
|
||||
@conf.find_next(@configs[2]).should == @configs[0]
|
||||
end
|
||||
|
||||
it "should #load_hash" do
|
||||
@conf.should have(0).items
|
||||
@conf.load_hash(@raw_configs[0])
|
||||
@conf.should have(1).items
|
||||
@conf.primary.should == @configs[0]
|
||||
end
|
||||
|
||||
it "should #load_array" do
|
||||
@conf.load_hash(:host => 'rabbid-rabbit')
|
||||
@conf.should have(1).items
|
||||
@conf.load_array(@raw_configs)
|
||||
@conf.should have(3).items
|
||||
@conf.should == @configs
|
||||
@conf.primary.should == @configs[0]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -13,6 +13,11 @@ describe 'AMQP::Failover::ServerDiscovery' do
|
||||
@retry_interval = 0.01
|
||||
end
|
||||
|
||||
after(:all) do
|
||||
$called = nil
|
||||
$start_count = nil
|
||||
end
|
||||
|
||||
it "should initialize" do
|
||||
EM.run {
|
||||
EM.start_server('127.0.0.1', 9999)
|
||||
|
||||
69
spec/unit/amqp/failover_spec.rb
Normal file
69
spec/unit/amqp/failover_spec.rb
Normal file
@@ -0,0 +1,69 @@
|
||||
# encoding: utf-8
|
||||
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe 'AMQP::Failover' do
|
||||
|
||||
before(:each) do
|
||||
configs = [
|
||||
{:host => 'rabbit0.local'},
|
||||
{:host => 'rabbit1.local'},
|
||||
{:host => 'rabbit2.local', :port => 5673}
|
||||
]
|
||||
@configs = configs.map { |conf| AMQP.settings.merge(conf) }
|
||||
@fail = AMQP::Failover.new(@configs)
|
||||
end
|
||||
|
||||
it "should initialize" do
|
||||
@fail.configs.should == @configs
|
||||
end
|
||||
|
||||
it "should #add_config" do
|
||||
@fail.instance_variable_set("@configs", nil)
|
||||
@fail.configs.should == []
|
||||
@fail.add_config(@configs[0])
|
||||
@fail.configs.should have(1).item
|
||||
@fail.configs.should == [@configs[0]]
|
||||
@fail.refs.should == {}
|
||||
@fail.add_config(@configs[1], :hello)
|
||||
@fail.configs.should have(2).items
|
||||
@fail.configs.should include(@configs[1])
|
||||
@fail.get_by_ref(:hello).should == @configs[1]
|
||||
end
|
||||
|
||||
it "should #get_by_conf" do
|
||||
fetched = @fail.get_by_conf(@configs[1])
|
||||
fetched.should == @configs[1]
|
||||
fetched.class.should == AMQP::Failover::Config
|
||||
fetched.last_fail.should be_nil
|
||||
end
|
||||
|
||||
it "should #fail_with" do
|
||||
fail = AMQP::Failover.new
|
||||
now = Time.now
|
||||
fail.failed_with(@configs[0], 0, now)
|
||||
fail.latest_failed.should == @configs[0]
|
||||
fail.last_fail_of(@configs[0]).should == now
|
||||
fail.last_fail_of(0).should == now
|
||||
end
|
||||
|
||||
it "should find #next_config" do
|
||||
@fail.failed_with(@configs[1])
|
||||
@fail.next_config.should == @configs[2]
|
||||
@fail.next_config.should == @configs[2]
|
||||
@fail.failed_with(@configs[2])
|
||||
@fail.next_config.should == @configs[0]
|
||||
@fail.failed_with(@configs[0])
|
||||
@fail.next_config.should be_nil
|
||||
end
|
||||
|
||||
it "should #failover_from" do
|
||||
now = Time.now
|
||||
@fail.failover_from(@configs[0], now).should == @configs[1]
|
||||
@fail.latest_failed.should == @configs[0]
|
||||
@fail.latest_failed.last_fail.should == now
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user