Permalink
Browse files

Gives the pool the ability to be filled with existing resources.

  • Loading branch information...
1 parent 096dfee commit fad68a8227e484a19305ff1f378bb3845330f585 @righi righi committed Dec 2, 2012
Showing with 24 additions and 0 deletions.
  1. +10 −0 lib/innertube.rb
  2. +14 −0 spec/innertube_spec.rb
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

0 comments on commit fad68a8

Please sign in to comment.