Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add tests for an FSSM backend for the file system watcher using the F…

…SSM polling support.
  • Loading branch information...
commit 89e6b193564075c89b1e0fc24b8184240ea6d324 1 parent 511bf2a
Yehuda Katz wycats authored

Showing 2 changed files with 154 additions and 0 deletions. Show diff stats Hide diff stats

  1. +1 0  Gemfile
  2. +153 0 activesupport/test/file_watcher_test.rb
1  Gemfile
@@ -22,6 +22,7 @@ end
22 22
23 23 # AS
24 24 gem "memcache-client", ">= 1.8.5"
  25 +gem "fssm", "~> 0.2.5"
25 26
26 27 platforms :mri_18 do
27 28 gem "system_timer"
153 activesupport/test/file_watcher_test.rb
... ... @@ -1,4 +1,7 @@
1 1 require 'abstract_unit'
  2 +require 'fssm'
  3 +require "fileutils"
  4 +
2 5
3 6 class FileWatcherTest < ActiveSupport::TestCase
4 7 class DumbBackend < ActiveSupport::FileWatcher::Backend
@@ -73,3 +76,153 @@ def test_overlapping_watchers
73 76 assert_equal [:changed => ["app/assets/main.scss", "app/assets/print.scss"]], @payload
74 77 end
75 78 end
  79 +
  80 +module FSSM::Backends
  81 + class Polling
  82 + def initialize(options={})
  83 + @handlers = []
  84 + @latency = options[:latency] || 0.1
  85 + end
  86 +
  87 + def add_handler(handler, preload=true)
  88 + handler.refresh(nil, true) if preload
  89 + @handlers << handler
  90 + end
  91 +
  92 + def run
  93 + begin
  94 + loop do
  95 + start = Time.now.to_f
  96 + @handlers.each { |handler| handler.refresh }
  97 + nap_time = @latency - (Time.now.to_f - start)
  98 + sleep nap_time if nap_time > 0
  99 + end
  100 + rescue Interrupt
  101 + end
  102 + end
  103 + end
  104 +end
  105 +
  106 +class FSSMFileWatcherTest < ActiveSupport::TestCase
  107 + class FSSMBackend < ActiveSupport::FileWatcher::Backend
  108 + def initialize(path, watcher)
  109 + super
  110 +
  111 + monitor = FSSM::Monitor.new
  112 + monitor.path(path, '**/*') do |monitor|
  113 + monitor.update { |base, relative| trigger relative => :changed }
  114 + monitor.delete { |base, relative| trigger relative => :deleted }
  115 + monitor.create { |base, relative| trigger relative => :created }
  116 + end
  117 +
  118 + @thread = Thread.new do
  119 + monitor.run
  120 + end
  121 + end
  122 +
  123 + def stop
  124 + @thread.kill
  125 + end
  126 + end
  127 +
  128 + def setup
  129 + Thread.abort_on_exception = true
  130 +
  131 + @watcher = ActiveSupport::FileWatcher.new
  132 +
  133 + @path = path = File.expand_path("../tmp", __FILE__)
  134 + FileUtils.rm_rf path
  135 +
  136 + create "app/assets/main.scss", true
  137 + create "app/assets/javascripts/foo.coffee", true
  138 + create "app/assets/print.scss", true
  139 + create "app/assets/videos.scss", true
  140 +
  141 + @backend = FSSMBackend.new(path, @watcher)
  142 +
  143 + @payload = []
  144 +
  145 + @watcher.watch %r{^app/assets/.*\.scss$} do |pay|
  146 + pay.each do |status, files|
  147 + files.sort!
  148 + end
  149 + @payload << pay
  150 + end
  151 + end
  152 +
  153 + def teardown
  154 + @backend.stop
  155 + Thread.abort_on_exception = false
  156 + end
  157 +
  158 + def create(path, past = false)
  159 + path = File.join(@path, path)
  160 + FileUtils.mkdir_p(File.dirname(path))
  161 +
  162 + FileUtils.touch(path)
  163 + File.utime(Time.now - 100, Time.now - 100, path) if past
  164 + sleep 0.1 unless past
  165 + end
  166 +
  167 + def change(path)
  168 + FileUtils.touch(File.join(@path, path))
  169 + sleep 0.1
  170 + end
  171 +
  172 + def delete(path)
  173 + FileUtils.rm(File.join(@path, path))
  174 + sleep 0.1
  175 + end
  176 +
  177 + def test_one_change
  178 + change "app/assets/main.scss"
  179 + assert_equal({:changed => ["app/assets/main.scss"]}, @payload.first)
  180 + end
  181 +
  182 + def test_multiple_changes
  183 + change "app/assets/main.scss"
  184 + change "app/assets/javascripts/foo.coffee"
  185 + assert_equal([{:changed => ["app/assets/main.scss"]}], @payload)
  186 + end
  187 +
  188 + def test_multiple_changes_match
  189 + change "app/assets/main.scss"
  190 + change "app/assets/print.scss"
  191 + change "app/assets/javascripts/foo.coffee"
  192 + assert_equal([{:changed => ["app/assets/main.scss"]}, {:changed => ["app/assets/print.scss"]}], @payload)
  193 + end
  194 +
  195 + def test_multiple_state_changes
  196 + create "app/assets/new.scss"
  197 + change "app/assets/print.scss"
  198 + delete "app/assets/videos.scss"
  199 + assert_equal([{:created => ["app/assets/new.scss"]}, {:changed => ["app/assets/print.scss"]}, {:deleted => ["app/assets/videos.scss"]}], @payload)
  200 + end
  201 +
  202 + def test_delete
  203 +
  204 + end
  205 +
  206 + def test_more_blocks
  207 + payload = []
  208 + @watcher.watch %r{^config/routes\.rb$} do |pay|
  209 + payload << pay
  210 + end
  211 +
  212 + create "config/routes.rb"
  213 + assert_equal [{:created => ["config/routes.rb"]}], payload
  214 + assert_equal [], @payload
  215 + end
  216 +
  217 + def test_overlapping_watchers
  218 + payload = []
  219 + @watcher.watch %r{^app/assets/main\.scss$} do |pay|
  220 + payload << pay
  221 + end
  222 +
  223 + change "app/assets/main.scss"
  224 + change "app/assets/print.scss"
  225 + assert_equal [{:changed => ["app/assets/main.scss"]}], payload
  226 + assert_equal [{:changed => ["app/assets/main.scss"]}, {:changed => ["app/assets/print.scss"]}], @payload
  227 + end
  228 +end

0 comments on commit 89e6b19

Please sign in to comment.
Something went wrong with that request. Please try again.