Skip to content

Commit

Permalink
Gives the pool the ability to be filled with existing resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
righi committed Dec 2, 2012
1 parent 096dfee commit fad68a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/innertube.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/innertube_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fad68a8

Please sign in to comment.