Introduce Gitlab::Utils.now so that it's easier to stub

This commit is contained in:
Lin Jen-Shin 2016-08-15 21:32:36 +08:00
parent 0ea81ae50a
commit ce64133516
4 changed files with 15 additions and 7 deletions

View file

@ -48,11 +48,11 @@ module Ci
end
before_transition [:created, :pending] => :running do |pipeline|
pipeline.started_at = Time.now
pipeline.started_at = Gitlab::Utils.now
end
before_transition any => [:success, :failed, :canceled] do |pipeline|
pipeline.finished_at = Time.now
pipeline.finished_at = Gitlab::Utils.now
end
before_transition do |pipeline|

View file

@ -87,7 +87,7 @@ module Statuseable
if started_at && finished_at
finished_at - started_at
elsif started_at
Time.now - started_at
Gitlab::Utils.now - started_at
end
end
end

View file

@ -7,11 +7,16 @@ module Gitlab
# @param cmd [Array<String>]
# @return [Boolean]
def system_silent(cmd)
Popen::popen(cmd).last.zero?
Popen.popen(cmd).last.zero?
end
def force_utf8(str)
str.force_encoding(Encoding::UTF_8)
end
# The same as Time.now but using this would make it easier to test
def now
Time.now
end
end
end

View file

@ -129,12 +129,15 @@ describe Ci::Pipeline, models: true do
describe '#duration' do
before do
build.skip
build2.skip
allow(Gitlab::Utils).to receive(:now).
and_return(current - 120, current)
pipeline.run
pipeline.succeed
end
it 'matches sum of builds duration' do
expect(pipeline.reload.duration).to eq(build.duration + build2.duration)
expect(pipeline.reload.duration).to eq(120)
end
end