6 require 'sleepy_penguin'
8 class TestInotify < Test::Unit::TestCase
13 ObjectSpace.each_object(Inotify) { |io| io.close unless io.closed? }
14 ObjectSpace.each_object(Tempfile) { |io| io.close unless io.closed? }
19 assert_kind_of(IO, ino)
23 (Inotify.constants - IO.constants).each do |const|
25 when :Event, :Enumerator
27 nr = Inotify.const_get(const)
28 assert nr <= 0xffffffff, "#{const}=#{nr}"
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
47 ino = Inotify.new Inotify::NONBLOCK
48 flags = ino.fcntl(Fcntl::F_GETFL) & Fcntl::O_NONBLOCK
49 assert_equal(Fcntl::O_NONBLOCK, flags)
53 ino = Inotify.new Inotify::CLOEXEC
54 flags = ino.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC
55 assert_equal(Fcntl::FD_CLOEXEC, flags)
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
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)
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
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)
105 ino = Inotify.new :CLOEXEC
106 tmp1 = Tempfile.new 'take'
107 wd = ino.add_watch tmp1.path, :OPEN
108 assert_kind_of Integer, wd
110 o = File.open(tmp1.path)
112 assert_equal [:OPEN], event.events
113 break if (nr -= 1) == 0
114 o = File.open(tmp1.path)