Sha256: cefd177783282c7d34c6edab04ee339497318cd25069780e8a878aa5a029607a

Contents?: true

Size: 1.92 KB

Versions: 16

Compression:

Stored size: 1.92 KB

Contents

require 'thread'
require 'tmpdir'
require 'tempfile'
require 'fileutils'
require 'minitest/autorun'

require 'deep_merge'

require_relative '../lib/franz'

Thread.abort_on_exception = true

class TestFranzDiscover < MiniTest::Test
  def setup
    @tmpdir       = Dir.mktmpdir
    @discoveries  = Queue.new
    @deletions    = Queue.new
    @logger       = Logger.new STDERR
    @logger.level = Logger::WARN
  end

  def teardown
    FileUtils.rm_rf @tmpdir
  end

  def test_discovers_existing_file
    tmp = tempfile %w[ test1 .log ]
    start_discovery known: []
    sleep 2 # Time to discover
    known = stop_discovery
    assert known.include?(tmp.path)
  end

  def test_discovers_new_file
    start_discovery known: []
    tmp = tempfile %w[ test2 .log ]
    sleep 3 # Time to discover
    known = stop_discovery
    assert known.include?(tmp.path)
  end

  def test_deletes_deleted_file
    tmp = tempfile %w[ test3 .log ]
    start_discovery known: []
    # at this point, we know Discover has already picked up tmp
    delete tmp.path
    sleep 2
    known = stop_discovery
    assert !known.include?(tmp.path)
  end

  def test_deletes_unknown_file
    tmp = tempfile %w[ test4 .log ]
    delete tmp.path
    # tmp never exists as far as Discover is aware
    start_discovery known: []
    sleep 2
    known = stop_discovery
    assert !known.include?(tmp.path)
  end

private
  def tempfile prefix=nil
    Tempfile.new prefix, @tmpdir
  end

  def start_discovery opts={}
    @configs = [{
      includes: [ "#{@tmpdir}/*.log" ],
      excludes: [ "#{@tmpdir}/exclude*" ]
    }]

    @ic = Franz::InputConfig.new @configs

    @discover = Franz::Discover.new({
      input_config: @ic,
      discover_interval: 1,
      discoveries: @discoveries,
      deletions: @deletions,
      logger: @logger
    }.deep_merge!(opts))
  end

  def stop_discovery
    @discover.stop
  end

  def delete path
    FileUtils.rm_rf path
    @deletions.push path
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
franz-1.5.9 test/test_franz_discover.rb
franz-1.5.8 test/test_franz_discover.rb
franz-1.5.7 test/test_franz_discover.rb
franz-1.5.6 test/test_franz_discover.rb
franz-1.5.14 test/test_franz_discover.rb
franz-1.5.13 test/test_franz_discover.rb
franz-1.5.12 test/test_franz_discover.rb
franz-1.5.11 test/test_franz_discover.rb
franz-1.5.10 test/test_franz_discover.rb
franz-1.4.31 test/test_franz_discover.rb
franz-1.5.5 test/test_franz_discover.rb
franz-1.5.4 test/test_franz_discover.rb
franz-1.5.3 test/test_franz_discover.rb
franz-1.5.2 test/test_franz_discover.rb
franz-1.5.1 test/test_franz_discover.rb
franz-1.5.0 test/test_franz_discover.rb