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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
require 'spec_helper'
require 'tmpdir'
require 'concurrent'
describe INotify::Notifier do
describe "instance" do
around do |block|
Dir.mktmpdir do |dir|
@root = Pathname.new(dir)
@notifier = INotify::Notifier.new
begin
block.call
ensure
@notifier.close
end
end
end
let(:dir) do
@root.join("foo").tap(&:mkdir)
end
let(:another_dir) do
@root.join("bar").tap(&:mkdir)
end
it "stops" do
@notifier.stop
end
describe :process do
it "gets events" do
events = recording(dir, :create)
dir.join("test.txt").write("hello world")
@notifier.process
expect(events.size).to eq(1)
expect(events.first.name).to eq("test.txt")
expect(events.first.absolute_name).to eq(dir.join("test.txt").to_s)
end
it "gets simultaneous events" do
events = recording(dir, :create)
dir.join("one.txt").write("hello world")
dir.join("two.txt").write("hello world")
@notifier.process
expect(events.map(&:name)).to match_array(%w(one.txt two.txt))
end
it "separates events between watches" do
bar_events = nil
foo_events = recording(dir, :create)
bar_events = recording(another_dir, :create)
dir.join("test.txt").write("hello world")
another_dir.join("test_two.txt").write("hello world")
@notifier.process
expect(foo_events.size).to eq(1)
expect(foo_events.first.name).to eq("test.txt")
expect(foo_events.first.absolute_name).to eq(dir.join("test.txt").to_s)
expect(bar_events.size).to eq(1)
expect(bar_events.first.name).to eq("test_two.txt")
expect(bar_events.first.absolute_name).to eq(another_dir.join("test_two.txt").to_s)
end
end
describe :run do
it "processes repeatedly until stopped" do
barriers = Array.new(3) { Concurrent::Event.new }
barrier_queue = barriers.dup
events = recording(dir, :create) { barrier_queue.shift.set }
run_thread = Thread.new { @notifier.run }
dir.join("one.txt").write("hello world")
barriers.shift.wait(1) or raise "timeout"
expect(events.map(&:name)).to match_array(%w(one.txt))
dir.join("two.txt").write("hello world")
barriers.shift.wait(1) or raise "timeout"
expect(events.map(&:name)).to match_array(%w(one.txt two.txt))
@notifier.stop
dir.join("three.txt").write("hello world")
run_thread.join(1) or raise "timeout"
expect(events.map(&:name)).to match_array(%w(one.txt two.txt))
end
it "can be stopped from within a callback" do
recording(dir, :create) { @notifier.stop }
run_thread = Thread.new { @notifier.run }
dir.join("one.txt").write("hello world")
run_thread.join(1) or raise "timeout"
end
it "can be stopped before it starts processing" do
barrier = Concurrent::Event.new
run_thread = Thread.new do
barrier.wait
@notifier.run
end
@notifier.stop
barrier.set
run_thread.join(1) or raise "timeout"
end
end
describe :fd do
it "returns an integer" do
expect(@notifier.fd).to be_an(Integer)
end
end
describe :to_io do
it "returns a ruby IO" do
expect(@notifier.to_io).to be_an(::IO)
end
it "matches the fd" do
expect(@notifier.to_io.fileno).to eq(@notifier.fd)
end
it "caches its result" do
expect(@notifier.to_io).to be(@notifier.to_io)
end
it "is selectable" do
events = recording(dir, :create)
expect(select([@notifier.to_io], nil, nil, 0.2)).to be_nil
dir.join("test.txt").write("hello world")
expect(select([@notifier.to_io], nil, nil, 0.2)).to eq([[@notifier.to_io], [], []])
@notifier.process
expect(select([@notifier.to_io], nil, nil, 0.2)).to be_nil
end
end
private
def recording(dir, *flags, callback: nil)
events = []
@notifier.watch(dir.to_s, *flags) do |event|
events << event
yield if block_given?
end
events
end
end
describe "mixed instances" do
it "doesn't tangle fds" do
notifiers = Array.new(30) { INotify::Notifier.new }
notifiers.each(&:to_io)
one = Array.new(10) { IO.pipe.last }
notifiers.each(&:close)
two = Array.new(10) { IO.pipe.last }
notifiers = nil
GC.start
_, writable, _ = select(nil, one, nil, 1)
expect(writable).to match_array(one)
_, writable, _ = select(nil, two, nil, 1)
expect(writable).to match_array(two)
end
end
end
|