tee_input: update documentation for Rack 1.2
[unicorn.git] / test / unit / test_util.rb
blob377f6b86e58bf5e6079a29b102a58552f69ff9f1
1 # -*- encoding: binary -*-
3 require 'test/test_helper'
4 require 'tempfile'
6 class TestUtil < Test::Unit::TestCase
8   EXPECT_FLAGS = File::WRONLY | File::APPEND
9   def test_reopen_logs_noop
10     tmp = Tempfile.new('')
11     fp = File.open(tmp.path, 'ab')
12     fp.sync = true
13     ext = fp.external_encoding rescue nil
14     int = fp.internal_encoding rescue nil
15     before = fp.stat.inspect
16     Unicorn::Util.reopen_logs
17     assert_equal before, File.stat(fp.path).inspect
18     assert_equal ext, (fp.external_encoding rescue nil)
19     assert_equal int, (fp.internal_encoding rescue nil)
20     assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
21     assert_nothing_raised { tmp.close! }
22     assert_nothing_raised { fp.close }
23   end
25   def test_reopen_logs_renamed
26     tmp = Tempfile.new('')
27     tmp_path = tmp.path.freeze
28     fp = File.open(tmp_path, 'ab')
29     fp.sync = true
31     ext = fp.external_encoding rescue nil
32     int = fp.internal_encoding rescue nil
33     before = fp.stat.inspect
34     to = Tempfile.new('')
35     File.rename(tmp_path, to.path)
36     assert ! File.exist?(tmp_path)
37     Unicorn::Util.reopen_logs
38     assert_equal tmp_path, tmp.path
39     assert File.exist?(tmp_path)
40     assert before != File.stat(tmp_path).inspect
41     assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
42     assert_equal ext, (fp.external_encoding rescue nil)
43     assert_equal int, (fp.internal_encoding rescue nil)
44     assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
45     assert fp.sync
46     assert_nothing_raised { tmp.close! }
47     assert_nothing_raised { to.close! }
48     assert_nothing_raised { fp.close }
49   end
51   def test_reopen_logs_renamed_with_encoding
52     tmp = Tempfile.new('')
53     tmp_path = tmp.path.dup.freeze
54     Encoding.list.each { |encoding|
55       File.open(tmp_path, "a:#{encoding.to_s}") { |fp|
56         fp.sync = true
57         assert_equal encoding, fp.external_encoding
58         assert_nil fp.internal_encoding
59         File.unlink(tmp_path)
60         assert ! File.exist?(tmp_path)
61         Unicorn::Util.reopen_logs
62         assert_equal tmp_path, fp.path
63         assert File.exist?(tmp_path)
64         assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
65         assert_equal encoding, fp.external_encoding
66         assert_nil fp.internal_encoding
67         assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
68         assert fp.sync
69       }
70     }
71     assert_nothing_raised { tmp.close! }
72   end if STDIN.respond_to?(:external_encoding)
74   def test_reopen_logs_renamed_with_internal_encoding
75     tmp = Tempfile.new('')
76     tmp_path = tmp.path.dup.freeze
77     Encoding.list.each { |ext|
78       Encoding.list.each { |int|
79         next if ext == int
80         File.open(tmp_path, "a:#{ext.to_s}:#{int.to_s}") { |fp|
81           fp.sync = true
82           assert_equal ext, fp.external_encoding
83           assert_equal int, fp.internal_encoding
84           File.unlink(tmp_path)
85           assert ! File.exist?(tmp_path)
86           Unicorn::Util.reopen_logs
87           assert_equal tmp_path, fp.path
88           assert File.exist?(tmp_path)
89           assert_equal fp.stat.inspect, File.stat(tmp_path).inspect
90           assert_equal ext, fp.external_encoding
91           assert_equal int, fp.internal_encoding
92           assert_equal(EXPECT_FLAGS, EXPECT_FLAGS & fp.fcntl(Fcntl::F_GETFL))
93           assert fp.sync
94         }
95       }
96     }
97     assert_nothing_raised { tmp.close! }
98   end if STDIN.respond_to?(:external_encoding)