Browse files

New method shutdown_in_all_threads

  • Loading branch information...
1 parent 45ff003 commit ab9a9244d81f274142261290fada190bfbaaa328 Torsten Schönebaum committed Sep 30, 2010
Showing with 60 additions and 6 deletions.
  1. +15 −6 lib/net/http/persistent.rb
  2. +45 −0 test/test_net_http_persistent.rb
View
21 lib/net/http/persistent.rb
@@ -388,13 +388,15 @@ def request uri, req = nil, &block
end
##
- # Shuts down all connections in this thread.
+ # Shuts down all connections in a thread.
+ #
+ # Uses the current thread by default.
#
# If you've used Net::HTTP::Persistent across multiple threads you must call
- # this in each thread.
+ # this for each thread.
- def shutdown
- connections = Thread.current[@connection_key]
+ def shutdown thread = Thread.current
+ connections = thread[@connection_key]
connections.each do |_, connection|
begin
@@ -403,8 +405,15 @@ def shutdown
end
end if connections
- Thread.current[@connection_key] = nil
- Thread.current[@request_key] = nil
+ thread[@connection_key] = nil
+ thread[@request_key] = nil
+ end
+
+ ##
+ # Shuts down all connections in all threads
+
+ def shutdown_in_all_threads
+ Thread.list.each { |t| shutdown t }
end
##
View
45 test/test_net_http_persistent.rb
@@ -575,6 +575,51 @@ def test_shutdown_no_connections
assert_nil Thread.current[@http.request_key]
end
+ def test_shutdown_for_another_thread
+ c = connection
+
+ t = Thread.new do
+ @http = Net::HTTP::Persistent.new
+ Thread.current[:threaded_conn] = connection
+ sleep 1000
+ end
+
+ @http.shutdown t
+
+ assert t[:threaded_conn].finished?
+ refute c.finished?
+
+ t.kill # cleanup
+ end
+
+ def test_shutdown_in_all_threads
+ require 'ruby-debug'
+
+ c = connection
+
+ t1 = Thread.new do
+ http = Net::HTTP::Persistent.new
+ Thread.current[:threaded_conn] = connection
+ sleep 1000
+ end
+
+ t2 = Thread.new do
+ http = Net::HTTP::Persistent.new
+ Thread.current[:threaded_conn] = connection
+ sleep 1000
+ end
+
+ @http.shutdown_in_all_threads
+
+ assert c.finished?
+ assert t1[:threaded_conn].finished?
+ assert t2[:threaded_conn].finished?
+
+ # cleanup
+ t1.kill
+ t2.kill
+ end
+
def test_ssl
@http.verify_callback = :callback
c = Net::HTTP.new 'localhost', 80

0 comments on commit ab9a924

Please sign in to comment.