File: file_tail_group_test.rb

package info (click to toggle)
ruby-file-tail 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 188 kB
  • sloc: ruby: 887; makefile: 5
file content (85 lines) | stat: -rw-r--r-- 1,909 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env ruby

require 'test_helper'
require 'file/tail'
require 'timeout'
require 'thread'
require 'tempfile'
Thread.abort_on_exception = true

class FileTailGroupTest < Test::Unit::TestCase
  include File::Tail

  def test_create_group
    t, = make_file
    g = Group[t]
    assert_equal t.path, g.each_tailer.first.file.path
    assert_equal t.path, g.each_file.first.path
  end

  def test_stop_group
    t, = make_file
    g = Group[t]
    assert_equal t.path, g.each_tailer.first.file.path
    assert_equal t.path, g.each_file.first.path
    g.stop
    assert_nil g.each_file.first
  end

  def test_add_file_to_group
    g = Group.new
    t, = make_file
    g.add_file t
    assert_equal t.path, g.each_tailer.first.file.path
    assert_equal t.path, g.each_file.first.path
  end

  def test_add_filename_to_group
    g = Group.new
    t, name = make_file
    t.close
    g.add_filename name
    assert_equal name, g.each_tailer.first.file.path
    assert_equal t.path, g.each_file.first.path
  end

  def test_add_generic_to_group
    g = Group.new
    t1, n1 = make_file
    t1.close
    t2, n1 = make_file
    g << n1
    g << t2
    assert g.each_tailer.any? { |t| t.file.path == n1 }
    assert g.each_tailer.any? { |t| t.file.path == t2.path }
    assert g.each_file.any? { |t| t.path == n1 }
    assert g.each_file.any? { |t| t.path == t2.path }
  end

  def test_tail_multiple_files
    t1, = make_file
    t1.max_interval = 0.1
    t2, = make_file
    t2.max_interval = 0.1
    g = Group[t1, t2]
    q = Queue.new
    t = Thread.new do
      g.tail { |l| q << l }
    end
    t1.puts "foo"
    assert_equal "foo\n", q.pop
    t2.puts "bar"
    assert_equal "bar\n", q.pop
  ensure
    t and t.exit
  end

  private

  def make_file
    name = File.expand_path(File.join(Dir.tmpdir, "tmp.#$$"))
    file = File.open(name, 'w+')
    file.extend File::Tail
    return file, name
  end
end