sleepy_penguin 3.0.0
[sleepy_penguin.git] / test / test_inotify.rb
blobdd2c7ad2d4a09e5e7cbc2a27e944350c6ca35fa3
1 require 'test/unit'
2 require 'fcntl'
3 require 'tempfile'
4 require 'set'
5 $-w = true
6 require 'sleepy_penguin'
8 class TestInotify < Test::Unit::TestCase
9   include SleepyPenguin
10   attr_reader :ino
12   def teardown
13     ObjectSpace.each_object(Inotify) { |io| io.close unless io.closed? }
14     ObjectSpace.each_object(Tempfile) { |io| io.close unless io.closed? }
15   end
17   def test_new
18     @ino = Inotify.new
19     assert_kind_of(IO, ino)
20   end
22   def test_constants
23     (Inotify.constants - IO.constants).each do |const|
24       case const.to_sym
25       when :Event, :Enumerator
26       else
27         nr = Inotify.const_get(const)
28         assert nr <= 0xffffffff, "#{const}=#{nr}"
29       end
30     end
31   end
33   def test_dup
34     a = Inotify.new
35     b = a.dup
36     assert a.fileno != b.fileno
37     abuf = a.instance_variable_get(:@inotify_buf)
38     bbuf = b.instance_variable_get(:@inotify_buf)
39     assert abuf.object_id != bbuf.object_id, "#{a.inspect} #{b.inspect}"
41     atmp = a.instance_variable_get(:@inotify_tmp)
42     btmp = b.instance_variable_get(:@inotify_tmp)
43     assert_equal atmp.object_id, btmp.object_id
44   end
46   def test_new_nonblock
47     ino = Inotify.new Inotify::NONBLOCK
48     flags = ino.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK
49     assert_equal(Fcntl::O_NONBLOCK, flags)
50   end
52   def test_new_cloeexec
53     ino = Inotify.new Inotify::CLOEXEC
54     flags = ino.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC
55     assert_equal(Fcntl::FD_CLOEXEC, flags)
56   end
58   def test_add_take
59     ino = Inotify.new Inotify::CLOEXEC
60     tmp1 = Tempfile.new 'take'
61     tmp2 = Tempfile.new 'take'
62     wd = ino.add_watch File.dirname(tmp1.path), Inotify::MOVE
63     assert_kind_of Integer, wd
64     File.rename tmp1.path, tmp2.path
65     event = ino.take
66     assert_equal wd, event.wd
67     assert_kind_of Inotify::Event, event
68     assert_equal File.basename(tmp1.path), event.name
69     others = ino.instance_variable_get(:@inotify_tmp)
70     assert_kind_of Array, others
71     assert_equal 1, others.size
72     assert_equal File.basename(tmp2.path), others[0].name
73     assert_equal [ :MOVED_FROM ], event.events
74     assert_equal [ :MOVED_TO ], others[0].events
75     assert_equal wd, others[0].wd
76     second_id = others[0].object_id
77     assert_equal second_id, ino.take.object_id
78     assert_nil ino.take(true)
79   end
81   def test_add_take_symbols
82     ino = Inotify.new :CLOEXEC
83     tmp1 = Tempfile.new 'take'
84     tmp2 = Tempfile.new 'take'
85     wd = ino.add_watch File.dirname(tmp1.path), :MOVE
86     assert_kind_of Integer, wd
87     File.rename tmp1.path, tmp2.path
88     event = ino.take
89     assert_equal wd, event.wd
90     assert_kind_of Inotify::Event, event
91     assert_equal File.basename(tmp1.path), event.name
92     others = ino.instance_variable_get(:@inotify_tmp)
93     assert_kind_of Array, others
94     assert_equal 1, others.size
95     assert_equal File.basename(tmp2.path), others[0].name
96     assert_equal [ :MOVED_FROM ], event.events
97     assert_equal [ :MOVED_TO ], others[0].events
98     assert_equal wd, others[0].wd
99     second_id = others[0].object_id
100     assert_equal second_id, ino.take.object_id
101     assert_nil ino.take(true)
102   end
104   def test_each
105     ino = Inotify.new :CLOEXEC
106     tmp1 = Tempfile.new 'take'
107     wd = ino.add_watch tmp1.path, :OPEN
108     assert_kind_of Integer, wd
109     nr = 5
110     o = File.open(tmp1.path)
111     ino.each do |event|
112       assert_equal [:OPEN], event.events
113       break if (nr -= 1) == 0
114       o = File.open(tmp1.path)
115     end
116     assert_equal 0, nr
117   end