Skip to content

Commit

Permalink
Stub SHUTDOWN_GRACE_TIME const in thread pool test
Browse files Browse the repository at this point in the history
  • Loading branch information
wjordan committed Apr 14, 2020
1 parent e8e4283 commit 09702d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
12 changes: 6 additions & 6 deletions lib/puma/thread_pool.rb
Expand Up @@ -285,7 +285,7 @@ def auto_reap!(timeout=5)
# Next, wait an extra +grace+ seconds then force-kill remaining threads.
# Finally, wait +kill_grace+ seconds for remaining threads to exit.
#
def shutdown(timeout=-1, grace: SHUTDOWN_GRACE_TIME, kill_grace: 1)
def shutdown(timeout=-1)
threads = @mutex.synchronize do
@shutdown = true
@not_empty.broadcast
Expand All @@ -309,18 +309,18 @@ def shutdown(timeout=-1, grace: SHUTDOWN_GRACE_TIME, kill_grace: 1)
end
end

# Wait for threads to finish after +timeout+ seconds.
# Wait +timeout+ seconds for threads to finish.
join.call(timeout)

# If threads are still running, raise ForceShutdown.
# If threads are still running, raise ForceShutdown and wait to finish.
threads.each do |t|
t.raise ForceShutdown
end
join.call(grace)
join.call(SHUTDOWN_GRACE_TIME)

# If threads are _still_ running, forcefully kill them.
# If threads are _still_ running, forcefully kill them and wait to finish.
threads.each(&:kill)
join.call(kill_grace)
join.call(1)
end

@spawned = 0
Expand Down
13 changes: 13 additions & 0 deletions test/helper.rb
Expand Up @@ -143,3 +143,16 @@ def full_name
end
end
end

module StubConst
# Stub constant for the duration of a block
def stub_const(name, val, &block)
orig = const_defined?(name) && remove_const(name)
const_set(name, val)
yield
ensure
remove_const(name)
const_set(name, orig) if orig
end
end
Module.include StubConst
4 changes: 3 additions & 1 deletion test/test_thread_pool.rb
Expand Up @@ -290,7 +290,9 @@ def test_shutdown_with_grace

@work_mutex.synchronize do
@work_done.wait(@work_mutex, 5)
pool.shutdown(timeout, grace: grace)
Puma::ThreadPool.stub_const(:SHUTDOWN_GRACE_TIME, grace) do
pool.shutdown(timeout)
end
finish = true
assert_equal 0, pool.spawned
assert_equal 2, rescued.length
Expand Down

0 comments on commit 09702d1

Please sign in to comment.