Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Allows the pool to be populated with resources created outside the pool. #3

Merged
merged 2 commits into from Feb 17, 2013
Jump to file or symbol
Failed to load files and symbols.
+27 −0
Split
View
@@ -16,6 +16,9 @@ licensed under the Apache 2.0 License.
pool = Innertube::Pool.new(proc { Connection.new },
proc {|c| c.disconnect })
+# Optionally, fill the pool with existing resources
+pool.fill([conn1, conn2, conn3])
+
# Grab a connection from the pool, returns the same value
# as the block
pool.take {|conn| conn.ping } # => true
View
@@ -61,6 +61,16 @@ def initialize(open, close)
@pool = Set.new
end
+ # Populate the pool with existing, open resources.
+ # @param [Array] An array of resources.
+ def fill(resources)
+ @lock.synchronize do
+ resources.each do |r|
+ @pool << Element.new(r)
+ end
+ end
+ end
+
# On each element of the pool, calls close(element) and removes it.
# @private
def clear
View
@@ -51,6 +51,20 @@ def wait_all(threads)
pool_members.map { |e| e.object.first }.sort.should == [1,2,3,4]
end
+ it 'should be fillable with existing resources' do
+ pool.fill(["Apple", "Banana", "Kiwi"])
+ pool_members.size.should == 3
+
+ pool.take do |x|
+ x.should eq('Apple')
+ pool.take do |y|
+ y.should eq('Banana')
+ pool.take do |z|
+ z.should eq('Kiwi')
+ end
+ end
+ end
+ end
it 'should unlock when exceptions are raised' do
begin