From 01c200204400c4373c5b5edeb11d1c70a8119795 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 8 Dec 2011 18:09:38 -0800 Subject: pool: shutdown backend when purging 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. --- lib/mogilefs/pool.rb | 1 + test/test_pool.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) 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 -- cgit v1.2.3-24-ge0c7