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:
parent
cd974d5616
commit
22d5dfb503
|
@ -85,14 +85,23 @@ class EventedFileUpdateCheckerTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "can be garbage collected" do
|
test "can be garbage collected" do
|
||||||
previous_threads = Thread.list
|
# Use a separate thread to isolate objects and ensure they will be garbage collected.
|
||||||
checker_ref = WeakRef.new(ActiveSupport::EventedFileUpdateChecker.new([], tmpdir => ".rb") { })
|
checker_ref, listener_threads = Thread.new do
|
||||||
listener_threads = Thread.list - previous_threads
|
threads_before_checker = Thread.list
|
||||||
|
checker = ActiveSupport::EventedFileUpdateChecker.new([], tmpdir => ".rb") { }
|
||||||
|
|
||||||
wait # Wait for listener thread to start processing events.
|
# Wait for listener thread to start processing events.
|
||||||
GC.start
|
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
|
assert_empty Thread.list & listener_threads
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue