PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_glob.rb

http://github.com/jordansissel/eventmachine-tail
Ruby | 146 lines | 110 code | 24 blank | 12 comment | 10 complexity | 05da1df03996354ac429e01eb635294e MD5 | raw file
  1. #!/usr/bin/env ruby
  2. require 'rubygems'
  3. $:.unshift "#{File.dirname(__FILE__)}/../lib"
  4. require 'eventmachine-tail'
  5. require 'tempfile'
  6. require 'test/unit'
  7. require 'timeout'
  8. require 'tmpdir'
  9. require 'testcase_helpers'
  10. class Watcher < EventMachine::FileGlobWatch
  11. def initialize(path, interval, data, testobj)
  12. super(path, interval)
  13. @data = data
  14. @testobj = testobj
  15. end # def initialize
  16. def file_found(path)
  17. # Use .include? here because files aren't going to be found in any
  18. # particular order.
  19. @testobj.assert(@data.include?(path), "Expected #{path} in \n#{@data.join("\n")}")
  20. @data.delete(path)
  21. @testobj.finish if @data.length == 0
  22. end
  23. def file_deleted(path)
  24. @testobj.assert(@data.include?(path), "Expected #{path} in \n#{@data.join("\n")}")
  25. @data.delete(path)
  26. @testobj.finish if @data.length == 0
  27. end
  28. end # class Reader
  29. class TestGlobWatcher < Test::Unit::TestCase
  30. include EventMachineTailTestHelpers
  31. SLEEPMAX = 1
  32. def setup
  33. @watchinterval = 0.2
  34. @dir = Dir.mktmpdir
  35. @data = []
  36. @data << "#{@dir}/#{rand}"
  37. @data << "#{@dir}/#{rand}"
  38. @data << "#{@dir}/#{rand}"
  39. @data << "#{@dir}/#{rand}"
  40. @data << "#{@dir}/#{rand}"
  41. @data << "#{@dir}/#{rand}"
  42. @data << "#{@dir}/#{rand}"
  43. @data << "#{@dir}/#{rand}.gz"
  44. @data << "#{@dir}/#{rand}.gz"
  45. @data << "#{@dir}/#{rand}.tar.gz"
  46. end # def setup
  47. def teardown
  48. @data.each do |file|
  49. #puts "Deleting #{file}"
  50. File.delete(file) rescue nil
  51. end
  52. Dir.delete(@dir)
  53. end # def teardown
  54. def finish
  55. EM.stop_event_loop
  56. end
  57. def test_glob_finds_existing_files
  58. EM.run do
  59. abort_after_timeout(SLEEPMAX * @data.length + 10)
  60. @data.each do |path|
  61. File.new(path, "w").close
  62. end
  63. EM::watch_glob("#{@dir}/*", Watcher, @watchinterval, @data.clone, self)
  64. end # EM.run
  65. end # def test_glob_finds_existing_files
  66. # This test should run slow. We are trying to ensure that
  67. # our file_tail correctly reads data slowly fed into the file
  68. # as 'tail -f' would.
  69. def test_glob_finds_newly_created_files_at_runtime
  70. EM.run do
  71. abort_after_timeout(SLEEPMAX * @data.length + 10)
  72. EM::watch_glob("#{@dir}/*", Watcher, @watchinterval, @data.clone, self)
  73. datacopy = @data.clone
  74. timer = EM::PeriodicTimer.new(0.2) do
  75. #puts "Creating: #{datacopy.first}"
  76. File.new(datacopy.shift, "w").close
  77. sleep(rand * SLEEPMAX)
  78. timer.cancel if datacopy.length == 0
  79. end
  80. end # EM.run
  81. end # def test_glob_finds_newly_created_files_at_runtime
  82. def test_glob_ignores_file_renames
  83. EM.run do
  84. abort_after_timeout(SLEEPMAX * @data.length + 10)
  85. EM::watch_glob("#{@dir}/*", Watcher, @watchinterval, @data.clone, self)
  86. datacopy = @data.clone
  87. timer = EM::PeriodicTimer.new(0.2) do
  88. filename = datacopy.shift
  89. File.new(filename, "w").close
  90. sleep(rand * SLEEPMAX)
  91. # This file rename should be ignored.
  92. EM::Timer.new(2) do
  93. newname = "#{filename}.renamed"
  94. File.rename(filename, newname)
  95. # Track the new filename so teardown removes it.
  96. @data << newname
  97. end
  98. timer.cancel if datacopy.length == 0
  99. end
  100. end
  101. end # def test_glob_ignores_file_renames
  102. def test_glob_ignores_duplicate_hardlinks
  103. EM.run do
  104. abort_after_timeout(SLEEPMAX * @data.length + 10)
  105. EM::watch_glob("#{@dir}/*", Watcher, @watchinterval, @data.clone, self)
  106. datacopy = @data.clone
  107. timer = EM::PeriodicTimer.new(0.2) do
  108. filename = datacopy.shift
  109. File.new(filename, "w").close
  110. sleep(rand * SLEEPMAX)
  111. # This file rename should be ignored.
  112. EM::Timer.new(2) do
  113. newname = "#{filename}.renamed"
  114. File.link(filename, newname)
  115. # Track the new filename so teardown removes it.
  116. @data << newname
  117. end
  118. timer.cancel if datacopy.length == 0
  119. end
  120. end
  121. end # def test_glob_ignores_file_renames
  122. end # class TestGlobWatcher