提交 e984fd43 编写于 作者: W wycats

Allow tests to proceed as soon as the payload changes, instead of always...

Allow tests to proceed as soon as the payload changes, instead of always having to wait for a timeout
上级 89e6b193
require 'abstract_unit' require 'abstract_unit'
require 'fssm' require 'fssm'
require "fileutils" require "fileutils"
require "timeout"
class FileWatcherTest < ActiveSupport::TestCase class FileWatcherTest < ActiveSupport::TestCase
...@@ -128,6 +129,9 @@ def stop ...@@ -128,6 +129,9 @@ def stop
def setup def setup
Thread.abort_on_exception = true Thread.abort_on_exception = true
@payload = []
@triggered = false
@watcher = ActiveSupport::FileWatcher.new @watcher = ActiveSupport::FileWatcher.new
@path = path = File.expand_path("../tmp", __FILE__) @path = path = File.expand_path("../tmp", __FILE__)
...@@ -140,13 +144,12 @@ def setup ...@@ -140,13 +144,12 @@ def setup
@backend = FSSMBackend.new(path, @watcher) @backend = FSSMBackend.new(path, @watcher)
@payload = []
@watcher.watch %r{^app/assets/.*\.scss$} do |pay| @watcher.watch %r{^app/assets/.*\.scss$} do |pay|
pay.each do |status, files| pay.each do |status, files|
files.sort! files.sort!
end end
@payload << pay @payload << pay
trigger
end end
end end
...@@ -156,22 +159,43 @@ def teardown ...@@ -156,22 +159,43 @@ def teardown
end end
def create(path, past = false) def create(path, past = false)
wait(past) do
path = File.join(@path, path) path = File.join(@path, path)
FileUtils.mkdir_p(File.dirname(path)) FileUtils.mkdir_p(File.dirname(path))
FileUtils.touch(path) FileUtils.touch(path)
File.utime(Time.now - 100, Time.now - 100, path) if past File.utime(Time.now - 100, Time.now - 100, path) if past
sleep 0.1 unless past end
end end
def change(path) def change(path)
wait do
FileUtils.touch(File.join(@path, path)) FileUtils.touch(File.join(@path, path))
sleep 0.1 end
end end
def delete(path) def delete(path)
wait do
FileUtils.rm(File.join(@path, path)) FileUtils.rm(File.join(@path, path))
sleep 0.1 end
end
def wait(past = false)
yield
return if past
begin
Timeout.timeout(1) do
sleep 0.05 until @triggered
end
rescue Timeout::Error
end
@triggered = false
end
def trigger
@triggered = true
end end
def test_one_change def test_one_change
...@@ -199,14 +223,11 @@ def test_multiple_state_changes ...@@ -199,14 +223,11 @@ def test_multiple_state_changes
assert_equal([{:created => ["app/assets/new.scss"]}, {:changed => ["app/assets/print.scss"]}, {:deleted => ["app/assets/videos.scss"]}], @payload) assert_equal([{:created => ["app/assets/new.scss"]}, {:changed => ["app/assets/print.scss"]}, {:deleted => ["app/assets/videos.scss"]}], @payload)
end end
def test_delete
end
def test_more_blocks def test_more_blocks
payload = [] payload = []
@watcher.watch %r{^config/routes\.rb$} do |pay| @watcher.watch %r{^config/routes\.rb$} do |pay|
payload << pay payload << pay
trigger
end end
create "config/routes.rb" create "config/routes.rb"
...@@ -218,6 +239,7 @@ def test_overlapping_watchers ...@@ -218,6 +239,7 @@ def test_overlapping_watchers
payload = [] payload = []
@watcher.watch %r{^app/assets/main\.scss$} do |pay| @watcher.watch %r{^app/assets/main\.scss$} do |pay|
payload << pay payload << pay
trigger
end end
change "app/assets/main.scss" change "app/assets/main.scss"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册