From 269e6ccb4d753e2514e80b558327972ff5ca1ae6 Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Thu, 3 Nov 2016 11:54:44 -0700 Subject: remove wait queue from ruby thread pool to avoid deadlock --- src/ruby/spec/generic/rpc_server_pool_spec.rb | 54 +++++++++++++++++---------- 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'src/ruby/spec') diff --git a/src/ruby/spec/generic/rpc_server_pool_spec.rb b/src/ruby/spec/generic/rpc_server_pool_spec.rb index b67008de48..a34b90594b 100644 --- a/src/ruby/spec/generic/rpc_server_pool_spec.rb +++ b/src/ruby/spec/generic/rpc_server_pool_spec.rb @@ -29,6 +29,8 @@ require 'grpc' +Thread.abort_on_exception = true + describe GRPC::Pool do Pool = GRPC::Pool @@ -44,32 +46,34 @@ describe GRPC::Pool do end end - describe '#jobs_waiting' do - it 'at start, it is zero' do + describe '#ready_for_work?' do + it 'before start it is not ready' do p = Pool.new(1) - expect(p.jobs_waiting).to be(0) + expect(p.ready_for_work?).to be(false) end - it 'it increases, with each scheduled job if the pool is not running' do - p = Pool.new(1) - job = proc {} - expect(p.jobs_waiting).to be(0) - 5.times do |i| + it 'it stops being ready after all workers jobs waiting or running' do + p = Pool.new(5) + p.start + job = proc { sleep(3) } # sleep so workers busy when done scheduling + 5.times do + expect(p.ready_for_work?).to be(true) p.schedule(&job) - expect(p.jobs_waiting).to be(i + 1) end + expect(p.ready_for_work?).to be(false) end it 'it decreases as jobs are run' do - p = Pool.new(1) + p = Pool.new(5) + p.start job = proc {} - expect(p.jobs_waiting).to be(0) - 3.times do + 5.times do + expect(p.ready_for_work?).to be(true) p.schedule(&job) end - p.start - sleep 2 - expect(p.jobs_waiting).to be(0) + expect(p.ready_for_work?).to be(false) + sleep 2 # give the pool time do get at least one task done + expect(p.ready_for_work?).to be(true) end end @@ -90,6 +94,18 @@ describe GRPC::Pool do expect(q.pop).to be(o) p.stop end + + it 'it throws an error if all opf the workers have tasks to do' do + p = Pool.new(5) + p.start + job = proc {} + 5.times do + expect(p.ready_for_work?).to be(true) + p.schedule(&job) + end + expect { p.schedule(&job) }.to raise_error + expect { p.schedule(&job) }.to raise_error + end end describe '#stop' do @@ -114,17 +130,17 @@ describe GRPC::Pool do describe '#start' do it 'runs pre-scheduled jobs' do - p = Pool.new(2) + p = Pool.new(5) + p.start o, q = Object.new, Queue.new n = 5 # arbitrary n.times { p.schedule(o, &q.method(:push)) } - p.start n.times { expect(q.pop).to be(o) } p.stop end - it 'runs jobs as they are scheduled ' do - p = Pool.new(2) + it 'runs jobs as they are scheduled' do + p = Pool.new(5) o, q = Object.new, Queue.new p.start n = 5 # arbitrary -- cgit v1.2.3 From 20457a1f09465254d8627300996e9fb90928c18b Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Fri, 4 Nov 2016 11:12:36 -0700 Subject: clean up pool unit tests --- src/ruby/spec/generic/rpc_server_pool_spec.rb | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) (limited to 'src/ruby/spec') diff --git a/src/ruby/spec/generic/rpc_server_pool_spec.rb b/src/ruby/spec/generic/rpc_server_pool_spec.rb index a34b90594b..8b2ab2dc4e 100644 --- a/src/ruby/spec/generic/rpc_server_pool_spec.rb +++ b/src/ruby/spec/generic/rpc_server_pool_spec.rb @@ -55,7 +55,7 @@ describe GRPC::Pool do it 'it stops being ready after all workers jobs waiting or running' do p = Pool.new(5) p.start - job = proc { sleep(3) } # sleep so workers busy when done scheduling + job = proc { sleep(5) } # sleep so workers busy when done scheduling 5.times do expect(p.ready_for_work?).to be(true) p.schedule(&job) @@ -63,7 +63,7 @@ describe GRPC::Pool do expect(p.ready_for_work?).to be(false) end - it 'it decreases as jobs are run' do + it 'it becomes ready again after jobs complete' do p = Pool.new(5) p.start job = proc {} @@ -72,7 +72,7 @@ describe GRPC::Pool do p.schedule(&job) end expect(p.ready_for_work?).to be(false) - sleep 2 # give the pool time do get at least one task done + sleep 5 # give the pool time do get at least one task done expect(p.ready_for_work?).to be(true) end end @@ -95,7 +95,7 @@ describe GRPC::Pool do p.stop end - it 'it throws an error if all opf the workers have tasks to do' do + it 'it throws an error if all of the workers have tasks to do' do p = Pool.new(5) p.start job = proc {} @@ -129,16 +129,6 @@ describe GRPC::Pool do end describe '#start' do - it 'runs pre-scheduled jobs' do - p = Pool.new(5) - p.start - o, q = Object.new, Queue.new - n = 5 # arbitrary - n.times { p.schedule(o, &q.method(:push)) } - n.times { expect(q.pop).to be(o) } - p.stop - end - it 'runs jobs as they are scheduled' do p = Pool.new(5) o, q = Object.new, Queue.new -- cgit v1.2.3 From a713b76d575e124cf2bc155ab54107930dad0fa8 Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Fri, 4 Nov 2016 11:41:07 -0700 Subject: change per job sleep of 5 seconds back to 3 in unit test --- src/ruby/spec/generic/rpc_server_pool_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ruby/spec') diff --git a/src/ruby/spec/generic/rpc_server_pool_spec.rb b/src/ruby/spec/generic/rpc_server_pool_spec.rb index 8b2ab2dc4e..48ccaee510 100644 --- a/src/ruby/spec/generic/rpc_server_pool_spec.rb +++ b/src/ruby/spec/generic/rpc_server_pool_spec.rb @@ -55,7 +55,7 @@ describe GRPC::Pool do it 'it stops being ready after all workers jobs waiting or running' do p = Pool.new(5) p.start - job = proc { sleep(5) } # sleep so workers busy when done scheduling + job = proc { sleep(3) } # sleep so workers busy when done scheduling 5.times do expect(p.ready_for_work?).to be(true) p.schedule(&job) -- cgit v1.2.3