2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2016-08-06 12:03:25 -04:00
|
|
|
require "pathname"
|
2020-08-28 10:29:30 -04:00
|
|
|
require "weakref"
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "file_update_checker_shared_tests"
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2015-12-08 21:07:49 -05:00
|
|
|
class EventedFileUpdateCheckerTest < ActiveSupport::TestCase
|
2015-11-10 10:21:33 -05:00
|
|
|
include FileUpdateCheckerSharedTests
|
2015-10-12 14:41:14 -04:00
|
|
|
|
2015-11-27 04:29:53 -05:00
|
|
|
def setup
|
2016-08-06 12:03:25 -04:00
|
|
|
skip if ENV["LISTEN"] == "0"
|
2017-01-31 18:37:28 -05:00
|
|
|
require "listen"
|
2015-11-27 04:29:53 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-11-11 00:38:18 -05:00
|
|
|
def new_checker(files = [], dirs = {}, &block)
|
2016-06-06 12:26:22 -04:00
|
|
|
ActiveSupport::EventedFileUpdateChecker.new(files, dirs, &block).tap do |c|
|
2015-11-09 01:32:46 -05:00
|
|
|
wait
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
end
|
2015-11-08 21:14:08 -05:00
|
|
|
|
|
|
|
def teardown
|
|
|
|
super
|
|
|
|
Listen.stop
|
|
|
|
end
|
2015-11-09 00:01:38 -05:00
|
|
|
|
|
|
|
def wait
|
2015-11-09 01:32:46 -05:00
|
|
|
sleep 1
|
2015-11-09 00:01:38 -05:00
|
|
|
end
|
2015-11-09 12:24:41 -05:00
|
|
|
|
Avoid double wait in EventedFileUpdateCheckerTest
Waiting after touching the file system is a concern of
`EventedFileUpdateCheckerTest`. Therefore, only call `wait` inside
`EventedFileUpdateCheckerTest`. This avoids calling `wait` an extra
time when calling `touch`.
Before:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 43.357019s, 0.9918 runs/s, 2.5371 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
After:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 34.351007s, 1.2518 runs/s, 3.2022 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
2021-07-22 12:48:33 -04:00
|
|
|
def mkdir(dirs)
|
|
|
|
super
|
|
|
|
wait # wait for the events to fire
|
|
|
|
end
|
|
|
|
|
2015-11-09 12:24:41 -05:00
|
|
|
def touch(files)
|
|
|
|
super
|
|
|
|
wait # wait for the events to fire
|
|
|
|
end
|
|
|
|
|
Avoid double wait in EventedFileUpdateCheckerTest
Waiting after touching the file system is a concern of
`EventedFileUpdateCheckerTest`. Therefore, only call `wait` inside
`EventedFileUpdateCheckerTest`. This avoids calling `wait` an extra
time when calling `touch`.
Before:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 43.357019s, 0.9918 runs/s, 2.5371 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
After:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 34.351007s, 1.2518 runs/s, 3.2022 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
2021-07-22 12:48:33 -04:00
|
|
|
def rm_f(files)
|
|
|
|
super
|
|
|
|
wait # wait for the events to fire
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
test "notifies forked processes" do
|
2019-09-26 18:27:00 -04:00
|
|
|
skip "Forking not available" unless Process.respond_to?(:fork)
|
2016-10-20 09:32:25 -04:00
|
|
|
|
2016-06-08 16:52:02 -04:00
|
|
|
FileUtils.touch(tmpfiles)
|
|
|
|
|
2018-09-25 13:18:20 -04:00
|
|
|
checker = new_checker(tmpfiles) { }
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate checker, :updated?
|
2016-06-08 16:52:02 -04:00
|
|
|
|
2017-02-05 20:00:18 -05:00
|
|
|
# Pipes used for flow control across fork.
|
2016-06-08 16:52:02 -04:00
|
|
|
boot_reader, boot_writer = IO.pipe
|
|
|
|
touch_reader, touch_writer = IO.pipe
|
|
|
|
|
|
|
|
pid = fork do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate checker, :updated?
|
2016-06-08 16:52:02 -04:00
|
|
|
|
|
|
|
# Fork is booted, ready for file to be touched
|
|
|
|
# notify parent process.
|
|
|
|
boot_writer.write("booted")
|
|
|
|
|
|
|
|
# Wait for parent process to signal that file
|
|
|
|
# has been touched.
|
|
|
|
IO.select([touch_reader])
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate checker, :updated?
|
2016-06-08 16:52:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert pid
|
|
|
|
|
|
|
|
# Wait for fork to be booted before touching files.
|
|
|
|
IO.select([boot_reader])
|
|
|
|
touch(tmpfiles)
|
|
|
|
|
|
|
|
# Notify fork that files have been touched.
|
|
|
|
touch_writer.write("touched")
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate checker, :updated?
|
2016-06-08 16:52:02 -04:00
|
|
|
|
|
|
|
Process.wait(pid)
|
|
|
|
end
|
2018-09-07 08:54:23 -04:00
|
|
|
|
2020-08-28 10:29:30 -04:00
|
|
|
test "can be garbage collected" do
|
|
|
|
previous_threads = Thread.list
|
|
|
|
checker_ref = WeakRef.new(ActiveSupport::EventedFileUpdateChecker.new([], tmpdir => ".rb") { })
|
|
|
|
listener_threads = Thread.list - previous_threads
|
|
|
|
|
|
|
|
wait # Wait for listener thread to start processing events.
|
|
|
|
GC.start
|
|
|
|
|
|
|
|
assert_not_predicate checker_ref, :weakref_alive?
|
|
|
|
assert_empty Thread.list & listener_threads
|
|
|
|
end
|
|
|
|
|
2019-05-22 19:42:34 -04:00
|
|
|
test "should detect changes through symlink" do
|
|
|
|
actual_dir = File.join(tmpdir, "actual")
|
|
|
|
linked_dir = File.join(tmpdir, "linked")
|
|
|
|
|
|
|
|
Dir.mkdir(actual_dir)
|
|
|
|
FileUtils.ln_s(actual_dir, linked_dir)
|
|
|
|
|
|
|
|
checker = new_checker([], linked_dir => ".rb") { }
|
|
|
|
|
|
|
|
assert_not_predicate checker, :updated?
|
|
|
|
|
Avoid double wait in EventedFileUpdateCheckerTest
Waiting after touching the file system is a concern of
`EventedFileUpdateCheckerTest`. Therefore, only call `wait` inside
`EventedFileUpdateCheckerTest`. This avoids calling `wait` an extra
time when calling `touch`.
Before:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 43.357019s, 0.9918 runs/s, 2.5371 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
After:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 34.351007s, 1.2518 runs/s, 3.2022 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
2021-07-22 12:48:33 -04:00
|
|
|
touch(File.join(actual_dir, "a.rb"))
|
2019-05-22 19:42:34 -04:00
|
|
|
|
|
|
|
assert_predicate checker, :updated?
|
|
|
|
assert checker.execute_if_updated
|
|
|
|
end
|
|
|
|
|
2018-09-07 08:54:23 -04:00
|
|
|
test "updated should become true when nonexistent directory is added later" do
|
2019-05-22 19:58:01 -04:00
|
|
|
watched_dir = File.join(tmpdir, "app")
|
|
|
|
unwatched_dir = File.join(tmpdir, "node_modules")
|
|
|
|
not_exist_watched_dir = File.join(tmpdir, "test")
|
2018-09-07 08:54:23 -04:00
|
|
|
|
2019-05-22 19:58:01 -04:00
|
|
|
Dir.mkdir(watched_dir)
|
|
|
|
Dir.mkdir(unwatched_dir)
|
2018-09-07 08:54:23 -04:00
|
|
|
|
2019-05-22 19:58:01 -04:00
|
|
|
checker = new_checker([], watched_dir => ".rb", not_exist_watched_dir => ".rb") { }
|
2018-09-07 08:54:23 -04:00
|
|
|
|
Avoid double wait in EventedFileUpdateCheckerTest
Waiting after touching the file system is a concern of
`EventedFileUpdateCheckerTest`. Therefore, only call `wait` inside
`EventedFileUpdateCheckerTest`. This avoids calling `wait` an extra
time when calling `touch`.
Before:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 43.357019s, 0.9918 runs/s, 2.5371 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
After:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 34.351007s, 1.2518 runs/s, 3.2022 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
2021-07-22 12:48:33 -04:00
|
|
|
touch(File.join(watched_dir, "a.rb"))
|
2019-05-22 19:58:01 -04:00
|
|
|
assert_predicate checker, :updated?
|
|
|
|
assert checker.execute_if_updated
|
2018-09-07 08:54:23 -04:00
|
|
|
|
2019-05-22 19:58:01 -04:00
|
|
|
Dir.mkdir(not_exist_watched_dir)
|
|
|
|
wait
|
|
|
|
assert_predicate checker, :updated?
|
|
|
|
assert checker.execute_if_updated
|
2018-09-07 08:54:23 -04:00
|
|
|
|
Avoid double wait in EventedFileUpdateCheckerTest
Waiting after touching the file system is a concern of
`EventedFileUpdateCheckerTest`. Therefore, only call `wait` inside
`EventedFileUpdateCheckerTest`. This avoids calling `wait` an extra
time when calling `touch`.
Before:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 43.357019s, 0.9918 runs/s, 2.5371 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
After:
$ bin/test test/evented_file_update_checker_test.rb test/file_update_checker_test.rb
Finished in 34.351007s, 1.2518 runs/s, 3.2022 assertions/s.
43 runs, 110 assertions, 0 failures, 0 errors, 0 skips
2021-07-22 12:48:33 -04:00
|
|
|
touch(File.join(unwatched_dir, "a.rb"))
|
2019-05-22 19:58:01 -04:00
|
|
|
assert_not_predicate checker, :updated?
|
|
|
|
assert_not checker.execute_if_updated
|
2018-09-07 08:54:23 -04:00
|
|
|
end
|
2020-08-24 20:56:40 -04:00
|
|
|
|
|
|
|
test "does not stop other checkers when nonexistent directory is added later" do
|
|
|
|
dir1 = File.join(tmpdir, "app")
|
|
|
|
dir2 = File.join(tmpdir, "test")
|
|
|
|
|
|
|
|
Dir.mkdir(dir2)
|
|
|
|
|
|
|
|
checker1 = new_checker([], dir1 => ".rb") { }
|
|
|
|
checker2 = new_checker([], dir2 => ".rb") { }
|
|
|
|
|
|
|
|
Dir.mkdir(dir1)
|
|
|
|
|
|
|
|
touch(File.join(dir1, "a.rb"))
|
|
|
|
assert_predicate checker1, :updated?
|
|
|
|
|
|
|
|
assert_not_predicate checker2, :updated?
|
|
|
|
|
|
|
|
touch(File.join(dir2, "a.rb"))
|
|
|
|
assert_predicate checker2, :updated?
|
|
|
|
end
|
2015-10-12 14:41:14 -04:00
|
|
|
end
|