yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
34a4c90cc934a619f0448d7c50f7a96f475eeaab blob 3222 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 
# Copyright (C) 2013-2016 all contributors <yahns-public@yhbt.net>
# License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
# frozen_string_literal: true
require_relative 'server_helper'

class TestResponse < Testcase
  ENV["N"].to_i > 1 and parallelize_me!
  include ServerHelper
  alias setup server_helper_setup
  alias teardown server_helper_teardown

  def test_auto_head
    err = @err
    cfg = Yahns::Config.new
    host, port = @srv.addr[3], @srv.addr[1]
    str = "HELLO WORLD\n"
    cfg.instance_eval do
      GTL.synchronize do
        app = Rack::Builder.new do
          use Rack::ContentLength
          use Rack::ContentType, "text/plain"
          run(lambda do |env|
            case env['PATH_INFO']
            when '/'; return [ 200, {}, [ str ] ]
            when '/304'; return [ 304, {}, [ str ] ]
            else
              abort 'unsupported'
            end
          end)
        end
        app(:rack, app) { listen "#{host}:#{port}" }
      end
      logger(Logger.new(err.path))
    end
    pid = mkserver(cfg)
    s = TCPSocket.new(host, port)
    s.write("HEAD / HTTP/1.0\r\n\r\n")
    assert s.wait(30), "IO wait failed"
    buf = s.read
    assert_match %r{\r\n\r\n\z}, buf
    s.close

    s = TCPSocket.new(host, port)
    s.write("GET /304 HTTP/1.0\r\n\r\n")
    assert s.wait(30), "IO wait failed"
    buf = s.read
    assert_match %r{\r\n\r\n\z}, buf
    assert_match %r{\b304\b}, buf
    s.close
  ensure
    quit_wait(pid)
  end

  def test_response_time_empty_body
    err = @err
    cfg = Yahns::Config.new
    host, port = @srv.addr[3], @srv.addr[1]
    cfg.instance_eval do
      GTL.synchronize do
        app = Rack::Builder.new do
          use Rack::ContentLength
          use Rack::ContentType, "text/plain"
          run lambda { |_| [ 200, {}, [] ] }
        end
        app(:rack, app) do
          listen "#{host}:#{port}"
        end
      end
      logger(Logger.new(err.path))
    end
    pid = mkserver(cfg)
    Net::HTTP.start(host, port) { |h|
      # warmup request
      res = h.get("/")
      assert_empty res.body

      t0 = Time.now
      nr = 10
      nr.times do
        res = h.get("/")
        assert_empty res.body
      end
      diff = Time.now - t0
      assert_operator diff, :<, (0.200 * nr)
    }
  ensure
    quit_wait(pid)
  end

  def test_response_time_head
    err = @err
    cfg = Yahns::Config.new
    host, port = @srv.addr[3], @srv.addr[1]
    cfg.instance_eval do
      GTL.synchronize do
        require 'rack/lobster'
        app = Rack::Builder.new do
          use Rack::Head
          use Rack::ContentLength
          use Rack::ContentType, "text/plain"
          run Rack::Lobster.new
        end
        app(:rack, app) do
          listen "#{host}:#{port}"
        end
      end
      logger(Logger.new(err.path))
    end
    pid = mkserver(cfg)
    Net::HTTP.start(host, port) { |h|
      # warmup request
      res = h.head("/")
      assert_equal 200, res.code.to_i

      t0 = Time.now
      nr = 10
      nr.times do
        res = h.head("/")
        assert_equal 200, res.code.to_i
      end
      diff = Time.now - t0
      assert_operator diff, :<, (0.200 * nr)
    }
  ensure
    quit_wait(pid)
  end
end
debug log:

solving 34a4c90 ...
found 34a4c90 in https://yhbt.net/yahns.git

Code repositories for project(s) associated with this inbox:

	../../../yahns.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).