unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob 032f0be664da64dd352c1f830557c5ffcc3e03f7 3076 bytes (raw)
$ git show v0.8.3:test/unit/test_util.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
 
require 'test/test_helper'
require 'tempfile'

class TestUtil < Test::Unit::TestCase

  EXPECT_FLAGS = File::WRONLY | File::APPEND
  def test_reopen_logs_noop
    tmp = Tempfile.new(nil)
    tmp.reopen(tmp.path, 'a')
    tmp.sync = true
    ext = tmp.external_encoding rescue nil
    int = tmp.internal_encoding rescue nil
    before = tmp.stat.inspect
    Unicorn::Util.reopen_logs
    assert_equal before, File.stat(tmp.path).inspect
    assert_equal ext, (tmp.external_encoding rescue nil)
    assert_equal int, (tmp.internal_encoding rescue nil)
  end

  def test_reopen_logs_renamed
    tmp = Tempfile.new(nil)
    tmp_path = tmp.path.freeze
    tmp.reopen(tmp_path, 'a')
    tmp.sync = true
    ext = tmp.external_encoding rescue nil
    int = tmp.internal_encoding rescue nil
    before = tmp.stat.inspect
    to = Tempfile.new(nil)
    File.rename(tmp_path, to.path)
    assert ! File.exist?(tmp_path)
    Unicorn::Util.reopen_logs
    assert_equal tmp_path, tmp.path
    assert File.exist?(tmp_path)
    assert before != File.stat(tmp_path).inspect
    assert_equal tmp.stat.inspect, File.stat(tmp_path).inspect
    assert_equal ext, (tmp.external_encoding rescue nil)
    assert_equal int, (tmp.internal_encoding rescue nil)
    assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & tmp.fcntl(Fcntl::F_GETFL))
    assert tmp.sync
  end

  def test_reopen_logs_renamed_with_encoding
    tmp = Tempfile.new(nil)
    tmp_path = tmp.path.dup.freeze
    Encoding.list.each { |encoding|
      File.open(tmp_path, "a:#{encoding.to_s}") { |fp|
        fp.sync = true
        assert_equal encoding, fp.external_encoding
        assert_nil fp.internal_encoding
        File.unlink(tmp_path)
        assert ! File.exist?(tmp_path)
        Unicorn::Util.reopen_logs
        assert_equal tmp_path, fp.path
        assert File.exist?(tmp_path)
        assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
        assert_equal encoding, fp.external_encoding
        assert_nil fp.internal_encoding
        assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
        assert fp.sync
      }
    }
  end if STDIN.respond_to?(:external_encoding)

  def test_reopen_logs_renamed_with_internal_encoding
    tmp = Tempfile.new(nil)
    tmp_path = tmp.path.dup.freeze
    Encoding.list.each { |ext|
      Encoding.list.each { |int|
        next if ext == int
        File.open(tmp_path, "a:#{ext.to_s}:#{int.to_s}") { |fp|
          fp.sync = true
          assert_equal ext, fp.external_encoding
          assert_equal int, fp.internal_encoding
          File.unlink(tmp_path)
          assert ! File.exist?(tmp_path)
          Unicorn::Util.reopen_logs
          assert_equal tmp_path, fp.path
          assert File.exist?(tmp_path)
          assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
          assert_equal ext, fp.external_encoding
          assert_equal int, fp.internal_encoding
          assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
          assert fp.sync
        }
      }
    }
  end if STDIN.respond_to?(:external_encoding)

end

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