mirror of
https://github.com/discourse/discourse.git
synced 2025-06-07 09:14:48 +08:00
DEV: fix flaky spec (#29972)
Spec was flaky cause work could still be in pipeline after the defer length is 0. Our length denotes the backlog, not the in progress count. This adds a mechanism for gracefully stopping the queue and avoids wait_for callse
This commit is contained in:
@ -24,6 +24,7 @@ module Scheduler
|
|||||||
@reactor = nil
|
@reactor = nil
|
||||||
@timeout = DEFAULT_TIMEOUT
|
@timeout = DEFAULT_TIMEOUT
|
||||||
@stats = LruRedux::ThreadSafeCache.new(STATS_CACHE_SIZE)
|
@stats = LruRedux::ThreadSafeCache.new(STATS_CACHE_SIZE)
|
||||||
|
@finish = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def timeout=(t)
|
def timeout=(t)
|
||||||
@ -72,7 +73,11 @@ module Scheduler
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def stop!
|
def stop!(finish_work: false)
|
||||||
|
if finish_work
|
||||||
|
@finish = true
|
||||||
|
@thread&.join
|
||||||
|
end
|
||||||
@thread.kill if @thread&.alive?
|
@thread.kill if @thread&.alive?
|
||||||
@thread = nil
|
@thread = nil
|
||||||
@reactor&.stop
|
@reactor&.stop
|
||||||
@ -96,7 +101,7 @@ module Scheduler
|
|||||||
@thread =
|
@thread =
|
||||||
Thread.new do
|
Thread.new do
|
||||||
@thread.abort_on_exception = true if Rails.env.test?
|
@thread.abort_on_exception = true if Rails.env.test?
|
||||||
do_work while true
|
do_work while (!@finish || !@queue.empty?)
|
||||||
end if !@thread&.alive?
|
end if !@thread&.alive?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -28,7 +28,7 @@ RSpec.describe Scheduler::Defer do
|
|||||||
@defer.later("second") {}
|
@defer.later("second") {}
|
||||||
@defer.later("bad") { raise "boom" }
|
@defer.later("bad") { raise "boom" }
|
||||||
|
|
||||||
wait_for(200) { @defer.length == 0 }
|
@defer.stop!(finish_work: true)
|
||||||
|
|
||||||
stats = Hash[@defer.stats]
|
stats = Hash[@defer.stats]
|
||||||
|
|
||||||
@ -105,11 +105,8 @@ RSpec.describe Scheduler::Defer do
|
|||||||
|
|
||||||
it "can queue jobs properly" do
|
it "can queue jobs properly" do
|
||||||
s = nil
|
s = nil
|
||||||
|
|
||||||
@defer.later { s = "good" }
|
@defer.later { s = "good" }
|
||||||
|
@defer.stop!(finish_work: true)
|
||||||
wait_for(1000) { s == "good" }
|
|
||||||
|
|
||||||
expect(s).to eq("good")
|
expect(s).to eq("good")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user