Skip to content

Commit

Permalink
fixing stat_watcher spec to not fail randomly
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Ares committed Feb 9, 2011
1 parent 04110f9 commit 8ee4303
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions spec/stat_watcher_spec.rb
Expand Up @@ -2,11 +2,13 @@

TEMP_FILE_PATH = "./test.txt"

INTERVAL = 0.010

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

def initialize(path)
super path, 0.010
super path, INTERVAL
end

def on_change(previous, current)
Expand All @@ -18,15 +20,31 @@ def on_change(previous, current)

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

sw = MyStatWatcher.new(path)
sw.attach(reactor)
File.open(path, "a+") { |f| f.write(rand.to_s) }
reactor.run_once

tw = Cool.io::TimerWatcher.new(INTERVAL, true)
tw.on_timer do
reactor.stop if sw.accessed
File.open(path, "w+") { |f| f.write(rand.to_s) }
end
tw.attach(reactor)

reactor.run

tw.detach
sw.detach

sw
end

describe Cool.io::StatWatcher do

let :sw do
run_with_file_change(TEMP_FILE_PATH)
end

before :each do
`touch #{TEMP_FILE_PATH}`
end
Expand All @@ -36,14 +54,10 @@ def run_with_file_change(path)
end

it "fire on_change when the file it is watching is modified" do
sw = run_with_file_change(TEMP_FILE_PATH)

sw.accessed.should eql(true)
end

it "should pass previous and current file stat info given a stat watcher" do
sw = run_with_file_change(TEMP_FILE_PATH)

sw.previous.ino.should eql(sw.current.ino)
end

Expand Down

0 comments on commit 8ee4303

Please sign in to comment.