mog: config parser cleanup
[ruby-mogilefs-client.git] / test / test_pool.rb
blob9e631c5f30cc7fcbbaf283dbfb89ff43035cbadd
1 # -*- encoding: binary -*-
2 require 'test/unit'
4 $TESTING = true
5 require 'mogilefs'
6 require 'mogilefs/pool'
8 class MogileFS::Pool
10   attr_reader :objects, :queue
12 end
14 class Resource; end
16 class ResourceWithArgs
18   def initialize(args)
19   end
21 end
23 class TestPool < Test::Unit::TestCase
25   def setup
26     @pool = MogileFS::Pool.new Resource
27   end
29   def test_get
30     o1 = @pool.get
31     o2 = @pool.get
32     assert_kind_of Resource, o1
33     assert_kind_of Resource, o2
34     assert_not_equal o1, o2
35   end
37   def test_get_with_args
38     @pool = MogileFS::Pool.new ResourceWithArgs, 'my arg'
39     o = @pool.get
40     assert_kind_of ResourceWithArgs, o
41   end
43   def test_put
44     o = @pool.get
45     @pool.put o
47     assert_raises(MogileFS::Pool::BadObjectError) { @pool.put nil }
48     assert_raises(MogileFS::Pool::BadObjectError) { @pool.put Resource.new }
49   end
51   def test_put_destroy
52     objs = (0...7).map { @pool.get } # pool full
54     assert_equal 7, @pool.objects.length
55     assert_equal 0, @pool.queue.length
57     4.times { @pool.put objs.shift }
59     assert_equal 7, @pool.objects.length
60     assert_equal 4, @pool.queue.length
62     @pool.put objs.shift # trip threshold
64     assert_equal 4, @pool.objects.length
65     assert_equal 2, @pool.queue.length
67     @pool.put objs.shift # don't need to remove any more
69     assert_equal 4, @pool.objects.length
70     assert_equal 3, @pool.queue.length
72     @pool.put objs.shift until objs.empty?
74     assert_equal 4, @pool.objects.length
75     assert_equal 4, @pool.queue.length
76   end
78   def test_use
79     val = @pool.use { |o| assert_kind_of Resource, o }
80     assert_equal nil, val, "Don't return object from pool"
81   end
83   def test_use_with_exception
84     @pool.use { |o| raise } rescue nil
85     assert_equal 1, @pool.queue.length, "Resource not returned to pool"
86   end
88   def test_use_reuse
89     o1 = nil
90     o2 = nil
92     @pool.use { |o| o1 = o }
93     @pool.use { |o| o2 = o }
95     assert_equal o1, o2, "Objects must be reused"
96   end
98 end