Fixed stubbing for Gitlab::Metrics specs

If the measure method uses Transaction.current directly the SQL
subscriber (Subscribers::ActiveRecord) will add timings of queries
triggered by DB cleaner.
This commit is contained in:
Yorick Peterse 2016-04-11 14:29:38 +02:00
parent d9110a7eca
commit 7eed4608fe
No known key found for this signature in database
GPG Key ID: EDD30D2BEB691AC9
2 changed files with 13 additions and 5 deletions

View File

@ -82,7 +82,9 @@ module Gitlab
#
# Returns the value yielded by the supplied block.
def self.measure(name)
return yield unless Transaction.current
trans = current_transaction
return yield unless trans
real_start = Time.now.to_f
cpu_start = System.cpu_time
@ -95,9 +97,9 @@ module Gitlab
real_time = (real_stop - real_start) * 1000.0
cpu_time = cpu_stop - cpu_start
Transaction.current.increment("#{name}_real_time", real_time)
Transaction.current.increment("#{name}_cpu_time", cpu_time)
Transaction.current.increment("#{name}_call_count", 1)
trans.increment("#{name}_real_time", real_time)
trans.increment("#{name}_cpu_time", cpu_time)
trans.increment("#{name}_call_count", 1)
retval
end
@ -113,5 +115,11 @@ module Gitlab
new(udp: { host: host, port: port })
end
end
private
def self.current_transaction
Transaction.current
end
end
end

View File

@ -74,7 +74,7 @@ describe Gitlab::Metrics do
let(:transaction) { Gitlab::Metrics::Transaction.new }
before do
allow(Gitlab::Metrics::Transaction).to receive(:current).
allow(Gitlab::Metrics).to receive(:current_transaction).
and_return(transaction)
end