yahns.git  about / heads / tags
sleepy, multi-threaded, non-blocking application server for Ruby
blob 1c6612ec02cfaeb39d4a53aeea2374254a789619 7540 bytes (raw)
$ git show v0.0.1:test/test_output_buffering.rb	# shows this blob on the CLI

  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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
 
# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require_relative 'server_helper'
require 'digest/md5'
require 'rack/file'

class TestOutputBuffering < Testcase
  parallelize_me!
  include ServerHelper
  alias setup server_helper_setup
  alias teardown server_helper_teardown

  GPLv3 = File.read("COPYING")
  RAND = IO.binread("/dev/urandom", 666) * 119
  dig = Digest::MD5.new
  NR = 1337
  MD5 = Thread.new do
    NR.times { dig << RAND }
    dig.hexdigest
  end

  class BigBody
    def each
      NR.times { yield RAND }
    end
  end

  def test_output_buffer_false_curl
    output_buffer(false, :curl)
  end

  def test_output_buffer_false_http09
    output_buffer(false, :http09)
  end

  def test_output_buffer_true_curl
    output_buffer(true, :curl)
  end

  def test_output_buffer_true_http09
    output_buffer(true, :http09)
  end

  def output_buffer(btype, check_type, delay = 4)
    err = @err
    cfg = Yahns::Config.new
    host, port = @srv.addr[3], @srv.addr[1]
    len = (RAND.size * NR).to_s
    cfg.instance_eval do
      ru = lambda do |e|
        [ 200, {'Content-Length'=>len}, BigBody.new ]
      end
      GTL.synchronize do
        app(:rack, ru) do
          listen "#{host}:#{port}"
          output_buffering btype
        end
      end
      logger(Logger.new(err.path))
    end
    srv = Yahns::Server.new(cfg)
    pid = fork do
      ENV["YAHNS_FD"] = @srv.fileno.to_s
      srv.start.join
    end

    case check_type
    when :curl
      # curl is faster for piping gigantic wads of data than Net::HTTP
      sh_sleep = delay ? "sleep #{delay} && " : ""
      md5 = `curl -sSf http://#{host}:#{port}/ | (#{sh_sleep} md5sum)`
      assert $?.success?, $?.inspect
      (md5 =~ /([a-f0-9]{32})/i) or raise "bad md5: #{md5.inspect}"
      md5 = $1
      assert_equal MD5.value, md5
    when :http09
      # HTTP/0.9
      c = TCPSocket.new(host, port)
      c.write("GET /\r\n\r\n")
      md5in = IO.pipe
      md5out = IO.pipe
      sleep(delay) if delay
      md5pid = Process.spawn("md5sum", :in => md5in[0], :out => md5out[1])
      md5in[0].close
      md5out[1].close
      assert_equal(NR * RAND.size, IO.copy_stream(c, md5in[1]))
      c.shutdown
      c.close
      md5in[1].close
      _, status = Timeout.timeout(10) { Process.waitpid2(md5pid) }
      assert status.success?, status.inspect
      md5 = md5out[0].read
      (md5 =~ /([a-f0-9]{32})/i) or raise "bad md5: #{md5.inspect}"
      md5 = $1
      assert_equal MD5.value, md5
      md5out[0].close
    else
      raise "TESTBUG"
    end
  rescue => e
    Yahns::Log.exception(Logger.new($stderr), "test", e)
    raise
  ensure
    quit_wait(pid)
  end

  class BigHeader
    A = "A" * 65536
    def initialize(h)
      @h = h
    end
    def each
      NR.times do |n|
        yield("X-#{n}", A)
      end
      @h.each { |k,v| yield(k,v) }
    end
  end

  def test_big_header
    err = @err
    cfg = Yahns::Config.new
    host, port = @srv.addr[3], @srv.addr[1]
    cfg.instance_eval do
      ru = lambda do |e|
        case e["PATH_INFO"]
        when "/COPYING"
          Rack::File.new(Dir.pwd).call(e)
          gplv3 = File.open("COPYING")
          def gplv3.each
            raise "SHOULD NOT BE CALLED"
          end
          size = gplv3.stat.size
          len = size.to_s
          ranges = Rack::Utils.byte_ranges(e, size)
          status = 200
          h = { "Content-Type" => "text/plain", "Content-Length" => len }
          if ranges && ranges.size == 1
            status = 206
            range = ranges[0]
            h["Content-Range"] = "bytes #{range.begin}-#{range.end}/#{size}"
            size = range.end - range.begin + 1
            len.replace(size.to_s)
          end
          [ status , BigHeader.new(h), gplv3 ]
        when "/"
          h = { "Content-Type" => "text/plain", "Content-Length" => "4" }
          [ 200, BigHeader.new(h), ["BIG\n"] ]
        else
          raise "WTF"
        end
      end
      GTL.synchronize do
        app(:rack, ru) do
          listen "#{host}:#{port}"
        end
      end
      logger(Logger.new(err.path))
    end
    srv = Yahns::Server.new(cfg)
    pid = fork do
      ENV["YAHNS_FD"] = @srv.fileno.to_s
      srv.start.join
    end
    threads = []

    # start with just a big header
    threads << Thread.new do
      c = TCPSocket.new(host, port)
      c.write "GET / HTTP/1.0\r\n\r\n"
      begin
        sleep 1
      end while c.nread == 0
      nr = 0
      last = nil
      c.each_line do |line|
        case line
        when %r{\AX-} then nr += 1
        else
          last = line
        end
      end
      assert_equal NR, nr
      assert_equal "BIG\n", last
      c.close
    end

    threads << Thread.new do
      c = TCPSocket.new(host, port)
      c.write "GET /COPYING HTTP/1.0\r\n\r\n"
      begin
        sleep 1
      end while c.nread == 0
      nr = 0
      c.each_line do |line|
        case line
        when %r{\AX-} then nr += 1
        else
          break if line == "\r\n"
        end
      end
      assert_equal NR, nr
      assert_equal GPLv3, c.read
      c.close
    end

    threads << Thread.new do
      c = TCPSocket.new(host, port)
      c.write "GET /COPYING HTTP/1.0\r\nRange: bytes=5-46\r\n\r\n"
      begin
        sleep 1
      end while c.nread == 0
      nr = 0
      c.each_line do |line|
        case line
        when %r{\AX-} then nr += 1
        else
          break if line == "\r\n"
        end
      end
      assert_equal NR, nr
      assert_equal GPLv3[5..46], c.read
      c.close
    end
    threads.each do |t|
      assert_equal t, t.join(30)
      assert_nil t.value
    end
  ensure
    quit_wait(pid)
  end

  def test_client_timeout
    err = @err
    apperr = tmpfile(%w(app .err))
    cfg = Yahns::Config.new
    size = RAND.size * NR
    host, port = @srv.addr[3], @srv.addr[1]
    cfg.instance_eval do
      ru = lambda do |e|
        if e["PATH_INFO"] == "/bh"
          h = { "Content-Type" => "text/plain", "Content-Length" => "4" }
          [ 200, BigHeader.new(h), ["BIG\n"] ]
        else
          [ 200, {'Content-Length' => size.to_s }, BigBody.new ]
        end
      end
      GTL.synchronize do
        app(:rack, ru) do
          listen "#{host}:#{port}"
          output_buffering false
          client_timeout 3
          logger(Logger.new(apperr.path))
        end
      end
      logger(Logger.new(err.path))
    end
    srv = Yahns::Server.new(cfg)
    pid = fork do
      ENV["YAHNS_FD"] = @srv.fileno.to_s
      srv.start.join
    end
    threads = []
    threads << Thread.new do
      c = TCPSocket.new(host, port)
      c.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
      sleep(5) # wait for timeout
      assert_operator c.nread, :>, 0
      c
    end

    threads << Thread.new do
      c = TCPSocket.new(host, port)
      c.write("GET /bh HTTP/1.1\r\nHost: example.com\r\n\r\n")
      sleep(5) # wait for timeout
      assert_operator c.nread, :>, 0
      c
    end
    threads.each { |t| t.join(10) }
    assert_operator size, :>, threads[0].value.read.size
    assert_operator size, :>, threads[1].value.read.size
    msg = File.readlines(apperr.path)
    msg = msg.grep(/timeout on :wait_writable after 3s$/)
    assert_equal 2, msg.size
    threads.each { |t| t.value.close }
  ensure
    apperr.close! if apperr
    quit_wait(pid)
  end
end if `which curl 2>/dev/null`.strip =~ /curl/ &&
       `which md5sum 2>/dev/null`.strip =~ /md5sum/

git clone git://yhbt.net/yahns.git
git clone https://yhbt.net/yahns.git