about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-12-08 18:09:38 -0800
committerEric Wong <normalperson@yhbt.net>2011-12-08 18:19:24 -0800
commit01c200204400c4373c5b5edeb11d1c70a8119795 (patch)
treeeddc75bb91c852695266c9f72a3710dbfdf58960
parent00d7a3cca62b19c34df66b337582a7c15c4b4848 (diff)
downloadmogilefs-client-01c200204400c4373c5b5edeb11d1c70a8119795.tar.gz
If we started a connection, make sure it's stopped and the file
descriptor closed.  Relying on GC to close file descriptors is
unreliable with some GC implementations.
-rw-r--r--lib/mogilefs/pool.rb1
-rw-r--r--test/test_pool.rb26
2 files changed, 27 insertions, 0 deletions
diff --git a/lib/mogilefs/pool.rb b/lib/mogilefs/pool.rb
index 21503ba..e7c4009 100644
--- a/lib/mogilefs/pool.rb
+++ b/lib/mogilefs/pool.rb
@@ -50,6 +50,7 @@ class MogileFS::Pool
       until @queue.length <= @purge_keep
         obj = @queue.pop true
         @objects.delete obj
+        obj.backend.shutdown if MogileFS::Client === obj
       end
     rescue ThreadError
     end
diff --git a/test/test_pool.rb b/test/test_pool.rb
index 9e631c5..dc40e85 100644
--- a/test/test_pool.rb
+++ b/test/test_pool.rb
@@ -19,6 +19,22 @@ class ResourceWithArgs
   end
 
 end
+class PoolClient < MogileFS::Client
+  attr_reader :alive
+
+  def initialize(*args)
+    @args = args
+    @alive = true
+  end
+
+  def backend
+    self
+  end
+
+  def shutdown
+    @alive = false
+  end
+end
 
 class TestPool < Test::Unit::TestCase
 
@@ -95,5 +111,15 @@ class TestPool < Test::Unit::TestCase
     assert_equal o1, o2, "Objects must be reused"
   end
 
+  def test_auto_shutdown
+    pool = MogileFS::Pool.new(PoolClient, 666)
+    tmp = []
+    6.times { tmp << pool.get }
+    tmp.each { |obj| pool.put(obj) }
+    alive = Hash.new { |h,k| h[k] = 0 }
+    tmp.each { |obj| alive[obj.alive] += 1 }
+    assert_equal 3, alive[true]
+    assert_equal 3, alive[false]
+  end
 end