yahns Ruby server user/dev discussion
 help / color / mirror / code / Atom feed
blob c2022894df27e79c8fe9e17ac1dee5868ee24f09 2982 bytes (raw)
name: test/test_rack_proxy.rb 	 # note: path name is non-authoritative(*)

 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
 
# Copyright (C) 2017 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'
require 'rbconfig'
begin
  require 'kcar'
rescue LoadError
end

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

  def setup
    server_helper_setup
    skip "kcar missing yahns/proxy_pass" unless defined?(Kcar)
  end

  def test_shorthand_systemd_emulation
    RUBY_VERSION.to_f > 2.3 or skip 'Ruby 2.3+ needed to test passing fd=3'
    ru = tmpfile(%w(test_rack_proxy .ru))
    ru.write("require 'rack/lobster'; run Rack::Lobster.new\n")
    cmd = %W(#{RbConfig.ruby} -I lib bin/yahns-rackup -E none
            -O listen=inherit #{ru.path})
    err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
    pid = mkserver(cfg) do
      cfg.instance_eval do
        app(:rack_proxy, { :pass => cmd }) { listen "#{host}:#{port}" }
        stderr_path err.path
      end
    end

    Net::HTTP.start(host, port) do |http|
      req = Net::HTTP::Get.new("/")
      res = http.request(req)
      assert_equal 200, res.code.to_i
    end
  ensure
    return unless pid
    Process.kill(:QUIT, pid)
    _, status = Process.waitpid2(pid)
    assert status.success?, status.inspect
  end

  def test_respawn
    ru = tmpfile(%w(test_rack_proxy .ru))
    ru.write('run(lambda { |env| ' \
        '[ 200, [ %w(Content-Type text/plain)], [ "#$$" ]]})')
    _, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
    Dir.mktmpdir do |dir|
      upath = "#{dir}/u.sock"
      cmd = %W(#{RbConfig.ruby} -I lib bin/yahns-rackup -E none
            -O listen=#{upath} #{ru.path})
      mapping = { [ :pass, "unix:#{upath}" ] => cmd }
      begin
        r, w = IO.pipe
        pid = mkserver(cfg) do
          cfg.instance_eval do
            app(:rack_proxy, mapping) { listen "#{host}:#{port}" }
          end
          $stderr.reopen(w)
          r.close
        end
        Timeout.timeout(10) do
          Thread.pass until r.gets =~ %r{listening on addr=.*/u\.sock}
        end
        u = UNIXSocket.new(upath)
        u.write("GET /\r\n\r\n")
        res = u.read
        backend = res.to_i
        assert_operator backend, :>, 0
        u.close
        assert_equal 1, Process.kill(0, backend)
        Process.kill(:QUIT, backend)

        Timeout.timeout(10) do
          Thread.pass until r.gets =~ %r{listening on addr=.*/u\.sock}
        end
        u = UNIXSocket.new(upath)
        u.write("GET /\r\n\r\n")
        res = u.read
        respawned = res.to_i
        assert_operator respawned, :>, 0
        assert_operator respawned, :!=, backend
      ensure
        [ r, w ].compact.map(&:close)
        return unless pid
        Process.kill(:QUIT, pid)
        _, status = Process.waitpid2(pid)
        assert status.success?, status.inspect
      end
    end
  end
end

debug log:

solving c202289 ...
found c202289 in https://yhbt.net/yahns-public/20170407034215.25377-1-yahns-public@yhbt.net/

applying [1/1] https://yhbt.net/yahns-public/20170407034215.25377-1-yahns-public@yhbt.net/
diff --git a/test/test_rack_proxy.rb b/test/test_rack_proxy.rb
new file mode 100644
index 0000000..c202289

Checking patch test/test_rack_proxy.rb...
Applied patch test/test_rack_proxy.rb cleanly.

index at:
100644 c2022894df27e79c8fe9e17ac1dee5868ee24f09	test/test_rack_proxy.rb

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

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

	https://yhbt.net/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).