Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3 from righi/fill-pool

Allows the pool to be populated with resources created outside the pool.
  • Loading branch information...
commit 00ec25ba2203600fe4128bb5bb4f151a8aab1d5a 2 parents e3a9fe6 + 8a709f0
@seancribbs seancribbs authored
Showing with 27 additions and 0 deletions.
  1. +3 −0  README.md
  2. +10 −0 lib/innertube.rb
  3. +14 −0 spec/innertube_spec.rb
View
3  README.md
@@ -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
10 lib/innertube.rb
@@ -62,6 +62,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
14 spec/innertube_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.