From 41c81cf79d5318111ffa93e309bcafb149e6aa0e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 30 Oct 2013 01:50:09 +0000 Subject: stream_input: use thread-local rbuf to avoid some garbage While we're at it, add some additional tests for input handling. --- test/test_input.rb | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'test') diff --git a/test/test_input.rb b/test/test_input.rb index 608ff23..404263d 100644 --- a/test/test_input.rb +++ b/test/test_input.rb @@ -49,4 +49,79 @@ class TestInput < Testcase ensure quit_wait(pid) end + + def input_server(ru, ibtype) + err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1] + cfg.instance_eval do + GTL.synchronize do + app(:rack, ru) { listen "#{host}:#{port}"; input_buffering ibtype } + end + stderr_path err.path + end + pid = mkserver(cfg) + [ host, port, pid ] + end + + def test_read_negative_lazy; _read_neg(:lazy); end + def test_read_negative_nobuffer; _read_neg(false); end + + def _read_neg(ibtype) + ru = lambda do |env| + rv = [] + input = env["rack.input"] + begin + input.read(-1) + rescue => e + rv << e.class.to_s + end + rv << input.read + rv << input.read(1).nil? + rv = rv.join(",") + h = { "Content-Length" => rv.size.to_s } + [ 200, h, [ rv ] ] + end + host, port, pid = input_server(ru, ibtype) + c = get_tcp_client(host, port) + c.write "PUT / HTTP/1.0\r\nContent-Length: 5\r\n\r\nhello" + assert_equal c, c.wait(30) + head, body = c.read.split(/\r\n\r\n/) + assert_match %r{ 200 OK}, head + exc, full, final = body.split(/,/) + assert_equal "hello", full + assert_equal "ArgumentError", exc + assert_equal true.to_s, final + c.close + ensure + quit_wait(pid) + end + + def test_gets_lazy; _gets(:lazy); end + def test_gets_nobuffer; _gets(false); end + + def _gets(ibtype) + in_join = lambda do |input| + rv = [] + while line = input.gets + rv << line + end + rv.join(",") + end + ru = lambda do |env| + rv = in_join.call(env["rack.input"]) + h = { "Content-Length" => rv.size.to_s } + [ 200, h, [ rv ] ] + end + host, port, pid = input_server(ru, ibtype) + c = get_tcp_client(host, port) + buf = "a\nb\n\n" + c.write "PUT / HTTP/1.0\r\nContent-Length: 5\r\n\r\n#{buf}" + assert_equal c, c.wait(30) + head, body = c.read.split(/\r\n\r\n/) + assert_match %r{ 200 OK}, head + expect = in_join.call(StringIO.new(buf)) + assert_equal expect, body + c.close + ensure + quit_wait(pid) + end end -- cgit v1.2.3-24-ge0c7