1 root_dir = File.join(File.dirname(__FILE__), "../..")
2 require File.join(root_dir, "test/test_helper")
7 @@concurrent_threads = 0
10 def self.max_concurrent_threads
20 @@mutex.synchronize do
21 @@concurrent_threads += 1 # !!! same for += and -=
22 @@threads = [@@concurrent_threads, @@threads].max
26 [200, {'Content-Type' => 'text/plain'}, ['hello!']]
28 @@mutex.synchronize { @@concurrent_threads -= 1 }
32 class ThreadingTest < Test::Unit::TestCase
34 @valid_request = "GET / HTTP/1.1\r\nHost: www.google.com\r\nContent-Type: text/plain\r\n\r\n"
35 @port = process_based_port
36 @app = Rack::URLMap.new('/test' => FakeHandler.new)
38 redirect_test_io { @server = HttpServer.new(@app, :Host => "127.0.0.1", :Port => @port, :Max_concurrent_threads => @threads) }
39 redirect_test_io { @server.start }
43 redirect_test_io { @server.stop(true) }
46 def test_server_respects_max_concurrent_threads_option
48 (@threads * 3).times do
49 threads << Thread.new do
50 send_data_over_socket("GET /test HTTP/1.1\r\nHost: localhost\r\nContent-Type: text/plain\r\n\r\n")
53 while threads.any? { |thread| thread.alive? }
56 assert_equal @threads, FakeHandler.max_concurrent_threads
61 def send_data_over_socket(string)
62 socket = TCPSocket.new("127.0.0.1", @port)
63 request = StringIO.new(string)
65 while data = request.read(8)
71 socket.write(" ") # Some platforms only raise the exception on attempted write