Sha256: 548ee01785a3cd8d5dfe8a36d3ec3ad210def7922def9664bbb80c5c1b0ff84d

Contents?: true

Size: 1.47 KB

Versions: 7

Compression:

Stored size: 1.47 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; def on_change; end; end
    expect { watcher.accessed }.to raise_error(ArgumentError)
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cool.io-1.4.3 spec/stat_watcher_spec.rb
cool.io-1.4.2 spec/stat_watcher_spec.rb
cool.io-1.4.1-x86-mingw32 spec/stat_watcher_spec.rb
cool.io-1.4.1-x64-mingw32 spec/stat_watcher_spec.rb
cool.io-1.4.1 spec/stat_watcher_spec.rb
cool.io-1.4.0 spec/stat_watcher_spec.rb
cool.io-1.3.1 spec/stat_watcher_spec.rb