Sha256: a640100b828fc0052796da510adda88fed9399de0e67973851309085b5e2daed

Contents?: true

Size: 1.51 KB

Versions: 38

Compression:

Stored size: 1.51 KB

Contents

require File.expand_path('../spec_helper', __FILE__)

TEMP_FILE_PATH = "./test.txt"

INTERVAL = 0.010

class MyStatWatcher < Cool.io::StatWatcher
  attr_accessor :accessed, :previous, :current

  def initialize(path)
    super path, INTERVAL
  end

  def on_change(previous, current)
    self.accessed = true
    self.previous = previous
    self.current  = current
  end
end

def run_with_file_change(path)
  reactor = Cool.io::Loop.new

  sw = MyStatWatcher.new(path)
  sw.attach(reactor)

  tw = Cool.io::TimerWatcher.new(INTERVAL, true)
  tw.on_timer do
    reactor.stop if sw.accessed
    write_file(path)
  end
  tw.attach(reactor)

  reactor.run

  tw.detach
  sw.detach

  sw
end

def write_file(path)
  File.open(path, "w+") { |f| f.write(rand.to_s) }
end

def delete_file(path)
  File.delete(TEMP_FILE_PATH)
end

describe Cool.io::StatWatcher do

  let :watcher do
    run_with_file_change(TEMP_FILE_PATH)
  end

  before :each do
    write_file(TEMP_FILE_PATH)
  end

  after :each do
    delete_file(TEMP_FILE_PATH)
  end

  it "fire on_change when the file it is watching is modified" do
    expect(watcher.accessed).to eq(true)
  end

  it "should pass previous and current file stat info given a stat watcher" do
    expect(watcher.previous.ino).to eq(watcher.current.ino)
  end

  it "should raise when the handler does not take 2 parameters" do
    class MyStatWatcher < Cool.io::StatWatcher
      remove_method :on_change
      def on_change
      end
    end
    expect { watcher.accessed }.to raise_error(ArgumentError)
  end

end

Version data entries

38 entries across 38 versions & 6 rubygems

Version Path
cool.io-1.9.0 spec/stat_watcher_spec.rb
fluent-plugin-nuopenlineage-light-0.1.0 vendor/bundle/ruby/3.3.0/gems/cool.io-1.8.1/spec/stat_watcher_spec.rb
fluent-plugin-openlineage-light-0.1.4 vendor/bundle/ruby/3.3.0/gems/cool.io-1.8.1/spec/stat_watcher_spec.rb
fluent-plugin-openlineage-light-0.1.3 vendor/bundle/ruby/3.3.0/gems/cool.io-1.8.1/spec/stat_watcher_spec.rb
fluent-plugin-openlineage-0.1.0 vendor/bundle/ruby/3.3.0/gems/cool.io-1.8.1/spec/stat_watcher_spec.rb
cool.io-1.8.0 spec/stat_watcher_spec.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/cool.io-1.7.1/spec/stat_watcher_spec.rb
cool.io-1.7.1-x86-mingw32 spec/stat_watcher_spec.rb
cool.io-1.7.1-x64-mingw32 spec/stat_watcher_spec.rb
cool.io-1.7.1 spec/stat_watcher_spec.rb
cool.io-1.7.0 spec/stat_watcher_spec.rb
cool.io-1.6.1 spec/stat_watcher_spec.rb
cool.io-1.6.0-x64-mingw32 spec/stat_watcher_spec.rb
cool.io-1.6.0-x86-mingw32 spec/stat_watcher_spec.rb
cool.io-1.6.0 spec/stat_watcher_spec.rb
cool.io-1.5.4-x64-mingw32 spec/stat_watcher_spec.rb
cool.io-1.5.4-x86-mingw32 spec/stat_watcher_spec.rb
cool.io-1.5.4 spec/stat_watcher_spec.rb
cool.io-1.5.3-x86-mingw32 spec/stat_watcher_spec.rb
cool.io-1.5.3-x64-mingw32 spec/stat_watcher_spec.rb