Fix flakey EventedFileUpdateChecker GC test

Example failure: https://buildkite.com/rails/rails/builds/84109#4a72a57f-20d3-44e1-b0a1-61c6a0baf3b3/1191-1201

Similar to #42845, this should ensure that there are no transient
references to the `EventedFileUpdateChecker`, and that Ruby performs a
full garbage collection cycle.
This commit is contained in:
Jonathan Hefner 2022-01-20 17:39:15 -06:00
parent cd974d5616
commit 22d5dfb503
1 changed files with 15 additions and 6 deletions

View File

@ -85,14 +85,23 @@ class EventedFileUpdateCheckerTest < ActiveSupport::TestCase
end
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
# Use a separate thread to isolate objects and ensure they will be garbage collected.
checker_ref, listener_threads = Thread.new do
threads_before_checker = Thread.list
checker = ActiveSupport::EventedFileUpdateChecker.new([], tmpdir => ".rb") { }
wait # Wait for listener thread to start processing events.
GC.start
# Wait for listener thread to start processing events.
wait
assert_not_predicate checker_ref, :weakref_alive?
[WeakRef.new(checker), Thread.list - threads_before_checker]
end.value
# Calling `GC.start` 4 times should trigger a full GC run.
4.times do
GC.start
end
assert_not checker_ref.weakref_alive?, "EventedFileUpdateChecker was not garbage collected"
assert_empty Thread.list & listener_threads
end