Class: FileTailTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Includes:
File::Tail, FileUtils
Defined in:
tests/file_tail_test.rb

Constant Summary

Constants included from File::Tail

File::Tail::VERSION, File::Tail::VERSION_ARRAY, File::Tail::VERSION_BUILD, File::Tail::VERSION_MAJOR, File::Tail::VERSION_MINOR

Instance Attribute Summary

Attributes included from File::Tail

#break_if_eof, #default_bufsize, #interval, #line_separator, #max_interval, #reopen_deleted, #reopen_suspicious, #return_if_eof, #suspicious_interval

Instance Method Summary collapse

Methods included from File::Tail

#after_reopen, #backward, #debug?, #forward, #output_debug_information, #preset_attributes, #read_line, #reopen_file, #restat, #sleep_interval, #tail

Instance Method Details

#append(file, n, size = 70) ⇒ Object (private)



382
383
384
385
# File 'tests/file_tail_test.rb', line 382

def append(file, n, size = 70)
  (1..n).each { |x| file << "#{x} #{"A" * size}\n" }
  file.flush
end

#count(file) ⇒ Object (private)



373
374
375
376
377
378
379
380
# File 'tests/file_tail_test.rb', line 373

def count(file)
  n = 0
  until file.eof?
    file.readline
    n += 1
  end
  return n
end

#setupObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'tests/file_tail_test.rb', line 14

def setup
  @out = File.new(File.join('tmp', "test.#$$"), "wb")
  at_exit { path = @out&.path and rm_f File.expand_path(path) }
  append(@out, 100)
  @in = File.new(@out.path, "rb")
  @in.extend(File::Tail)
  @in.interval            = 0.4
  @in.max_interval        = 0.8
  @in.reopen_deleted      = true # is default
  @in.reopen_suspicious   = true # is default
  @in.suspicious_interval  = 60
end

#teardownObject



365
366
367
368
369
# File 'tests/file_tail_test.rb', line 365

def teardown
  @in.close
  @out.close
  File.unlink(@out.path)
end

#test_backwardObject



36
37
38
39
40
41
42
43
# File 'tests/file_tail_test.rb', line 36

def test_backward
  [ 0, 1, 2, 10, 100 ].each do |lines|
    @in.backward(lines)
    assert_equal(lines, count(@in))
  end
  @in.backward(101)
  assert_equal(100, count(@in))
end

#test_backward_small_bufferObject



45
46
47
48
49
50
51
52
# File 'tests/file_tail_test.rb', line 45

def test_backward_small_buffer
  [ 0, 1, 2, 10, 100 ].each do |lines|
    @in.backward(lines, 100)
    assert_equal(lines, count(@in))
  end
  @in.backward(101, 100)
  assert_equal(100, count(@in))
end

#test_backward_small_buffer2Object



54
55
56
57
58
59
60
61
62
# File 'tests/file_tail_test.rb', line 54

def test_backward_small_buffer2
  @in.default_bufsize = 100
  [ 0, 1, 2, 10, 100 ].each do |lines|
    @in.backward(lines)
    assert_equal(lines, count(@in))
  end
  @in.backward(101)
  assert_equal(100, count(@in))
end

#test_forwardObject



27
28
29
30
31
32
33
34
# File 'tests/file_tail_test.rb', line 27

def test_forward
  [ 0, 1, 2, 10, 100 ].each do |lines|
    @in.forward(lines)
    assert_equal(100 - lines, count(@in))
  end
  @in.forward(101)
  assert_equal(0, count(@in))
end

#test_tail_changeObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'tests/file_tail_test.rb', line 295

def test_tail_change
  return if File::PATH_SEPARATOR == ';' # Grmpf! Windows...
  @in.forward
  reopened = false
  assert_equal 0, @in.lineno
  @in.after_reopen { |f| reopened = true }
  lines = []
  logger = Thread.new do
    begin
      Timeout::timeout(10) do
        @in.tail(110) do |l|
          lines << l
        end
      end
    rescue Timeout::Error
    end
  end
  appender = Thread.new do
    Timeout::timeout(10) do
      until lines.size == 100
        sleep 0.1
      end
    end
    @out.close
    File.unlink(@out.path)
    @out = File.new(@in.path, "wb")
    append(@out, 10)
  end
  appender.join
  logger.join
  assert_equal(110, lines.size)
  assert reopened
  assert_equal 10, @in.lineno
end

#test_tail_change2Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'tests/file_tail_test.rb', line 330

def test_tail_change2
  return if File::PATH_SEPARATOR == ';' # Grmpf! Windows...
  @in.forward
  reopened = false
  assert_equal 0, @in.lineno
  @in.after_reopen { |f| reopened = true }
  lines = []
  logger = Thread.new do
    begin
      Timeout::timeout(10) do
        @in.tail(110) do |l|
          lines << l
        end
      end
    rescue Timeout::Error
    end
  end
  appender = Thread.new do
    Timeout::timeout(10) do
      until lines.size == 100
        sleep 0.1
      end
    end
    @out.truncate 0
    @out.close
    @out = File.new(@in.path, "wb")
    append(@out, 10)
  end
  appender.join
  logger.join
  assert_equal(110, lines.size)
  assert reopened
  assert_equal 10, @in.lineno
end

#test_tail_removeObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'tests/file_tail_test.rb', line 192

def test_tail_remove
  return if File::PATH_SEPARATOR == ';' # Grmpf! Windows...
  @in.backward
  reopened = false
  @in.after_reopen { |f| reopened = true }
  lines = []
  logger = Thread.new do
    begin
      Timeout::timeout(10) do
        @in.tail do |l|
          lines << l
        end
      end
    rescue Timeout::Error
    end
  end
  appender = Thread.new do
    until logger.stop?
      sleep 0.1
    end
    @out.close
    File.unlink(@out.path)
    @out = File.new(@in.path, "wb")
    append(@out, 10)
  end
  appender.join
  logger.join
  assert_equal(10, lines.size)
  assert reopened
end

#test_tail_remove2Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'tests/file_tail_test.rb', line 223

def test_tail_remove2
  return if File::PATH_SEPARATOR == ';' # Grmpf! Windows...
  @in.backward
  reopened = false
  @in.after_reopen { |f| reopened = true }
  lines = []
  logger = Thread.new do
    begin
      Timeout::timeout(10) do
        @in.tail do |l|
          lines << l
        end
      end
    rescue Timeout::Error
    end
  end
  appender = Thread.new do
    until logger.stop?
      sleep 0.1
    end
    @out.close
    File.unlink(@out.path)
    @out = File.new(@in.path, "wb")
    append(@out, 10)
    sleep 1
    append(@out, 10)
    File.unlink(@out.path)
    @out = File.new(@in.path, "wb")
    append(@out, 10)
  end
  appender.join
  logger.join
  assert_equal(30, lines.size)
  assert reopened
end

#test_tail_remove3Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'tests/file_tail_test.rb', line 259

def test_tail_remove3
  return if File::PATH_SEPARATOR == ';' # Grmpf! Windows...
  @in.backward
  reopened = false
  @in.after_reopen { |f| reopened = true }
  lines = []
  logger = Thread.new do
    begin
      Timeout::timeout(10) do
        @in.tail(15) do |l|
          lines << l
        end
      end
    rescue Timeout::Error
    end
  end
  appender = Thread.new do
    until logger.stop?
      sleep 0.1
    end
    @out.close
    File.unlink(@out.path)
    @out = File.new(@in.path, "wb")
    append(@out, 10)
    sleep 1
    append(@out, 10)
    File.unlink(@out.path)
    @out = File.new(@in.path, "wb")
    append(@out, 10)
  end
  appender.join
  logger.join
  assert_equal(15, lines.size)
  assert reopened
end

#test_tail_truncatedObject



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
# File 'tests/file_tail_test.rb', line 165

def test_tail_truncated
  @in.backward
  lines = []
  logger = Thread.new do
    begin
      Timeout::timeout(10) do
        @in.tail do |l|
          lines << l
        end
      end
    rescue Timeout::Error
    end
  end
  appender = Thread.new do
    until logger.stop?
      sleep 0.1
    end
    @out.close
    File.truncate(@out.path, 0)
    @out = File.new(@in.path, "ab")
    append(@out, 10)
  end
  appender.join
  logger.join
  assert_equal(10, lines.size)
end

#test_tail_with_block_with_nObject



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
# File 'tests/file_tail_test.rb', line 95

def test_tail_with_block_with_n
  Timeout::timeout(10) do
    @in.backward(1)
    lines = []
    Timeout::timeout(1) { @in.tail(0) { |l| lines << l } }
    assert_equal(0, lines.size)
    #
    @in.backward(1)
    lines = []
    Timeout::timeout(1) { @in.tail(1) { |l| lines << l } }
    assert_equal(1, lines.size)
    #
    @in.backward(10)
    lines = []
    Timeout::timeout(1) { @in.tail(10) { |l| lines << l } }
    assert_equal(10, lines.size)
    #
    @in.backward(100)
    lines = []
    @in.backward(1)
    assert_raises(Timeout::Error) do
      Timeout::timeout(1) { @in.tail(2) { |l| lines << l } }
    end
    assert_equal(1, lines.size)
    #
  end
end

#test_tail_with_block_without_nObject



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
# File 'tests/file_tail_test.rb', line 64

def test_tail_with_block_without_n
  Timeout::timeout(10) do
    lines = []
    @in.backward(1)
    assert_raises(Timeout::Error) do
      Timeout::timeout(1) { @in.tail { |l| lines << l } }
    end
    assert_equal(1, lines.size)
    #
    lines = []
    @in.backward(10)
    assert_raises(Timeout::Error) do
      Timeout::timeout(1) { @in.tail { |l| lines << l } }
    end
    assert_equal(10, lines.size)
    #
    lines = []
    @in.backward(100)
    assert_raises(Timeout::Error) do
      Timeout::timeout(1) { @in.tail { |l| lines << l } }
    end
    assert_equal(100, lines.size)
    #
    lines = []
    @in.backward(101)
    assert_raises(Timeout::Error) do
      Timeout::timeout(1) { @in.tail { |l| lines << l } }
    end
  end
end

#test_tail_withappendObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'tests/file_tail_test.rb', line 150

def test_tail_withappend
  @in.backward
  lines = []
  logger = Thread.new do
    begin
      Timeout::timeout(1) { @in.tail { |l| lines << l } }
    rescue Timeout::Error
    end
  end
  appender = Thread.new { append(@out, 10) }
  appender.join
  logger.join
  assert_equal(10, lines.size)
end

#test_tail_without_block_with_nObject



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
# File 'tests/file_tail_test.rb', line 123

def test_tail_without_block_with_n
  Timeout::timeout(10) do
    @in.backward(1)
    lines = []
    Timeout::timeout(1) { lines += @in.tail(0) }
    assert_equal(0, lines.size)
    #
    @in.backward(1)
    lines = []
    Timeout::timeout(1) { lines += @in.tail(1) }
    assert_equal(1, lines.size)
    #
    @in.backward(10)
    lines = []
    Timeout::timeout(1) { lines += @in.tail(10) }
    assert_equal(10, lines.size)
    #
    @in.backward(100)
    lines = []
    @in.backward(1)
    assert_raises(Timeout::Error) do
      Timeout::timeout(1) { lines += @in.tail(2) }
    end
    assert_equal(0, lines.size)
  end
end