Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6726194
commit 2b495d6
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# encoding: UTF-8 | ||
require "singleton" | ||
|
||
module Qusion | ||
class ChannelPool | ||
include Singleton | ||
|
||
class << self | ||
attr_writer :pool_size | ||
|
||
def pool_size | ||
@pool_size || 5 | ||
end | ||
|
||
def reset | ||
@pool_size = nil | ||
instance.reset | ||
end | ||
|
||
end | ||
|
||
def channel | ||
channel = @pool.shift | ||
@pool << channel | ||
channel | ||
end | ||
|
||
def pool | ||
@pool ||= initialize_pool | ||
end | ||
|
||
def initialize_pool | ||
@pool = [] | ||
self.class.pool_size.times do | ||
@pool << MQ.new | ||
end | ||
end | ||
|
||
def reset | ||
@pool = nil | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# encoding: UTF-8 | ||
require File.dirname(__FILE__) + "/../spec_helper" | ||
|
||
describe ChannelPool do | ||
MQ = Object.new | ||
|
||
before(:each) do | ||
ChannelPool.reset | ||
end | ||
|
||
it "should be singleton" do | ||
lambda { ChannelPool.new }.should raise_error | ||
end | ||
|
||
it "should adjust the pool size" do | ||
ChannelPool.pool_size = 5 | ||
ChannelPool.pool_size.should == 5 | ||
end | ||
|
||
it "should create a pool of AMQP channels" do | ||
ChannelPool.pool_size = 3 | ||
MQ.should_receive(:new).exactly(3).times | ||
ChannelPool.instance.pool | ||
end | ||
|
||
it "should default to a pool size of 5" do | ||
MQ.should_receive(:new).exactly(5).times | ||
ChannelPool.instance.pool | ||
end | ||
|
||
it "should return a channel in a round-robin" do | ||
ChannelPool.instance.instance_variable_set(:@pool, [1,2,3,4,5]) | ||
[1,2,3,4,5,1,2,3,4,5].each do |i| | ||
ChannelPool.instance.channel.should == i | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
describe Qusion do | ||
it "should provide a convenience method to get a channel from the pool" do | ||
channel_pool = mock("channel pool") | ||
ChannelPool.should_receive(:instance).and_return(channel_pool) | ||
channel_pool.should_receive(:channel) | ||
Qusion.channel | ||
end | ||
end |