Permalink
Browse files

added 'pool_status' method

  • Loading branch information...
1 parent d775d56 commit 055bf3f2f4092a5b15b8f6dcd9214d797bfc400a Dirk Bolte committed Oct 22, 2013
Showing with 57 additions and 1 deletion.
  1. +11 −0 lib/em-synchrony/connection_pool.rb
  2. +46 −1 spec/connection_pool_spec.rb
@@ -27,6 +27,17 @@ def execute(async)
release(f) if not async
end
end
+
+ # Returns current pool utilization.
+ #
+ # @return [Hash] Current utilization.
+ def pool_status
+ {
+ available: @available.size,
+ reserved: @reserved.size,
+ pending: @pending.size
+ }
+ end
private
@@ -126,4 +126,49 @@
end
end
-end
+ describe '#pool_status' do
+ it 'should return right initial size' do
+ (1..10).each do |count|
+ pool = EventMachine::Synchrony::ConnectionPool.new(size: count) { }
+ status = pool.pool_status
+ expect(status).to include available: count
+ expect(status).to include reserved: 0
+ expect(status).to include pending: 0
+ end
+ end
+ it 'should return up-to-date statusrmation' do
+ sleep = 0.5
+ count = 5
+ EM.run do
+ pool = EM::Synchrony::ConnectionPool.new(size: count) do
+ -> { EM::Synchrony.sleep(sleep) }
+ end
+ (1..count).each do |used|
+ Fiber.new { pool.call }.resume
+ status = pool.pool_status
+ expect(status).to include available: count - used
+ expect(status).to include reserved: used
+ expect(status).to include pending: 0
+ end
+ (1..count).each do |used|
+ Fiber.new { pool.call }.resume
+ status = pool.pool_status
+ expect(status).to include available: 0
+ expect(status).to include reserved: count
+ expect(status).to include pending: used
+ end
+ Fiber.new {
+ EM::Synchrony.sleep(sleep + 0.1)
+ expect(pool.pool_status).to include pending: 0
+
+ EM::Synchrony.sleep(sleep + 0.1)
+ status = pool.pool_status
+ expect(status).to include available: count
+ expect(status).to include reserved: 0
+ expect(status).to include pending: 0
+ EM.stop
+ }.resume
+ end
+ end
+ end
+end

0 comments on commit 055bf3f

Please sign in to comment.